zoukankan      html  css  js  c++  java
  • POJ2103 Jackpot

    Description

    The Great Dodgers company has recently developed a brand-new playing machine.
    You put a coin into the machine and pull the handle. After that it chooses some integer number. If the chosen number is zero you win a jackpot. In the other case the machine tries to divide the chosen number by the lucky numbers (p_1 , p_2 , cdots , p_n) . If at least one of the remainders is zero --- you win.
    Great Dodgers want to calculate the probability of winning on their machine. They tried to do it, but failed. So Great Dodgers hired you to write a program that calculates the corresponding probability.
    Unfortunately, probability theory does not allow you to assume that all integer numbers have equal probability. But one mathematician hinted you that the required probability can be approximated as the following limit:

    [lim_{k o infty}frac{S_k}{2k+1} ]

    Here (S_k) is the number of integers between (-k) and (k) that are divisible by at least one of the lucky numbers.

    Input

    Input file contains (n) --- the number of lucky numbers ((1 le n le 16)), followed by (n) lucky numbers ((1 le p_i le 10^9)).

    Output

    It is clear that the requested probability is rational. Output it as an irreducible fraction.
    On the first line of the output file print the numerator of the winning probability. On the second line print its denominator. Both numerator and denominator must be printed without leading zeroes. Remember that the fraction must be irreducible.

    Sample Input

    2
    4 6

    Sample Output

    1
    3

    第一次用java写程序,为了不打高精度(因为必须涉及高精除),代码几乎是蒯的。(2^N)枚举,相互求(lcm),贡献即为(frac{1}{lcm}),然后用容斥原理合答案即可。
    ACMjava1H速成戳这里

    import java.math.*;
    import java.util.*;
    public class Main
    {
    	static BigInteger d,ret,temp,yy;
    	static int n,dd;
    	static boolean mark = true;
    	static BigInteger[] a = new BigInteger[20];
    	public static void main(String[] args)
    	{
    		Scanner in = new Scanner (System.in);
    		n = in.nextInt();
    		temp = BigInteger.ONE;
    		ret = BigInteger.ZERO;
    		for (int i = 0;i < n;++i)
    		{
    			int k = in.nextInt();
    			a[i] = BigInteger.valueOf(k);
    			d = temp.gcd(a[i]);
    			temp = temp.multiply(a[i]).divide(d);
    		}
    		for (int i = 1;i < (1<<n);++i)
    		{
    			mark = false; yy = BigInteger.ONE;
    			for (int j = 0;j < n;++j) if (((1 << j) & i) > 0) { mark = !mark; d = a[j].gcd(yy); yy = yy.multiply(a[j]).divide(d); }
    			if (mark) ret = ret.add(temp.divide(yy));
    			else ret = ret.subtract(temp.divide(yy));
    		}
    		d = ret.gcd(temp);
    		System.out.println(ret.divide(d));
    		System.out.println(temp.divide(d));
    	}
    }
    
  • 相关阅读:
    hadoop配置支持LZO压缩格式并支持分片
    CSS 图片:如何使用 CSS 来布局图片
    CSS3 用户界面:用户界面特性来调整元素尺寸,框尺寸和外边框
    CSS3 多列:如何将文本内容设计成像报纸一样的多列布局?
    mac Pycharm:如何使用anaconda安装jieba
    pycharm如何安装jieba词频统计器?
    CSS3 动画:使元素从一种样式逐渐变化为另一种样式的效果
    CSS3 过渡:用鼠标移过逐渐改变它原有样式
    CSS3 3D 转换:使用 3D 转换来对元素进行格式化
    CSS3 2D 转换:对元素进行移动、缩放、转动、拉长或拉伸
  • 原文地址:https://www.cnblogs.com/mmlz/p/6160916.html
Copyright © 2011-2022 走看看