Poj2103 jackpot (rejection + high precision)

Source: Internet
Author: User
Jackpot
Time limit:20000 Ms   Memory limit:64000 K
Total submissions:1044   Accepted:216
Case time limit:2000 ms

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 P1, P2 ,..., pn. 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:
Limk → ∞ (sk/2 k + 1 ).
Here SK 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 <= n <= 16), followed by N lucky numbers (1 <= pI <= 109 ).

Output

It is clear that the requested probability is rational. Output it as an irrecoverable cible 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

24 6

Sample output

13
Given n numbers, input a coin into the slot machine. The machine returns a value of X. If X can be divisible by at least one Pi, the machine wins. K→∞ (SK/2 k + 1). sk indicates the number of X that can be won from-K to K. The answer is expressed by the simplest score. Train of Thought: simply refresh the code and use Java to handle large numbers, but pay attention to constant-level optimization of the Code. Tle spent one night...
import java.util.*;import java.math.*;public class Main {public static BigInteger gcd(BigInteger a, BigInteger b){ if(b.equals(BigInteger.valueOf(0))) return a; else return gcd(b,a.mod(b)); }private static Scanner in;public static void main(String[] args) {// TODO Auto-generated method stubBigInteger []num = new BigInteger[20];in = new Scanner(System.in);while(in.hasNextInt()){int n = in.nextInt();BigInteger fz = BigInteger.ZERO;BigInteger fm = BigInteger.ONE;BigInteger LCM,tmp;for(int i = 0; i < n; i++) {int k = in.nextInt();num[i] = BigInteger.valueOf(k);tmp = fm.gcd(num[i]);fm = fm.divide(tmp).multiply(num[i]);}for(int i = 1; i < (1<<n); i++) {int cnt = 0;LCM = BigInteger.ONE;for(int j = 0; j < n; j++) {if((i&(1<<j))>0) {tmp = num[j].gcd(LCM);LCM = LCM.divide(tmp).multiply(num[j]);cnt++;}}LCM = fm.divide(LCM);if((cnt&1)!=0) {fz = fz.add(LCM);}else{fz = fz.subtract(LCM);}}BigInteger GCD = gcd(fz,fm);fm = fm.divide(GCD);fz = fz.divide(GCD);System.out.println(fz);System.out.println(fm);}}}


Poj2103 jackpot (rejection + high precision)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.