POJ2103 Jackpot(容斥+高精度)
来源:互联网 发布:极进网络 编辑:程序博客网 时间:2024/05/22 15:44
Jackpot
Time Limit: 20000MS Memory Limit: 64000KTotal Submissions: 1044 Accepted: 216Case Time Limit: 2000MS
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/2k+1).
Here Sk is the number of integers between -k and k that are divisible by at least one of the lucky numbers.
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:
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 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.
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
题意:给定n个数,向老虎机中投入一枚硬币,机器返回一个值X,如果X能被至少一个Pi整除,则获胜,求limk→∞(Sk/2k+1). Sk表示-k到k中能获胜的X的个数,答案用最简分数表示。
思路:简单的容斥,用JAVA大数搞定,只是要注意代码的常数级优化。TLE了一晚。。。
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);}}}
1 0
- POJ2103 Jackpot(容斥+高精度)
- uva 10458 - Cricket Ranking(容斥+高精度)
- BZOJ 2024 SHOI2009 舞会 动态规划+容斥原理+高精度
- Gym 100345B - Signed Derangements-高精度+容斥原理
- ACdream 1420 High Speed Trains(容斥原理+高精度)
- POJ 1091 - 跳蚤 - 容斥原理 +扩展欧几里德+高精度
- [BZOJ2024][SHOI2009] 舞会(dp+容斥原理+高精度)
- [BZOJ]4563: [Haoi2016]放棋子 容斥+高精度
- SGU 476 Coach's Trouble(高精度,dfs,容斥)
- HDU 3208 Integer's Power(容斥原理、指数和、高精度求开根)
- SGU 476 Coach's Trouble(集合独立性、容斥、高精度)
- 幸运数字(容斥原理+高精度与低精度的加 减 除)
- HDU1920 Jackpot
- 容斥
- 容斥
- 容斥
- 容斥
- 容斥
- Failed to install xxx.apk on device 'emulator-5554': timeout
- 如何不浪费网站流量 网站做好二次营销的几个方案
- java访问MySql数据库
- [Ai工程文件] 豆瓣FM页面的Ai设计图分享
- Qt入门(5)——用Qt控件创建一个电话本界面
- POJ2103 Jackpot(容斥+高精度)
- memorymanagerment
- SVN服务器搭建和使用(一)
- FreeRADIUS RADSQLRELAY说明
- write gpio reg
- 浅谈 C++ 中的 new/delete 和 new[]/delete[]
- Python 分布式计算模块 Parallel
- jdk8 Remove PermGen,Metaspace
- Oracle 修改表