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));
    	}
    }
    
  • 相关阅读:
    数据库(六):多表关系
    数据库(五):约束关系
    数据库(四):数据类型
    数据库(三):存储引擎
    数据库(二):初识sql语句
    数据库(一):初识数据库
    番外:socketserver用法
    ~~并发编程(十六):协程理论~~
    ~~并发编程(十五):进程池线程池~~
    ~~并发编程(十四):Queue~~
  • 原文地址:https://www.cnblogs.com/mmlz/p/6160916.html
Copyright © 2011-2022 走看看