uva 10375 分解质因数算指数
2016-07-11 20:29
309 查看
The binomial coefficient C(m, n) is defined as
C(m, n) = m!/(m − n)! n!
Given four natural numbers p, q, r, and s, compute the the result of dividing C(p, q) by C(r, s).
Input
Input consists of a sequence of lines. Each line contains four non-negative integer numbers giving values
for p, q, r, and s, respectively, separated by a single space. All the numbers will be smaller than 10,000
with p ≥ q and r ≥ s.
Output
For each line of input, print a single line containing a real number with 5 digits of precision in the fraction,
giving the number as described above. You may assume the result is not greater than 100,000,000.
Sample Input
10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998
Sample Output
0.12587
505606.46055
1.28223
0.48996
2.00000
3.99960
题目大意:
给出p,q,r,s
算 C(p, q) / C(r, s)
因为最大值是10000,直接算阶乘会爆
所以对阶乘分解质因数,先打素数表,算出每个素数的指数,相乘即可。
注意下 p可能为0,
C(m, n) = m!/(m − n)! n!
Given four natural numbers p, q, r, and s, compute the the result of dividing C(p, q) by C(r, s).
Input
Input consists of a sequence of lines. Each line contains four non-negative integer numbers giving values
for p, q, r, and s, respectively, separated by a single space. All the numbers will be smaller than 10,000
with p ≥ q and r ≥ s.
Output
For each line of input, print a single line containing a real number with 5 digits of precision in the fraction,
giving the number as described above. You may assume the result is not greater than 100,000,000.
Sample Input
10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998
Sample Output
0.12587
505606.46055
1.28223
0.48996
2.00000
3.99960
题目大意:
给出p,q,r,s
算 C(p, q) / C(r, s)
因为最大值是10000,直接算阶乘会爆
所以对阶乘分解质因数,先打素数表,算出每个素数的指数,相乘即可。
注意下 p可能为0,
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; #define MAX_N 10000 int prime[MAX_N],e[MAX_N],tot=0; int check[MAX_N]; void IsPrime(){ memset(check,0,sizeof(check)); for(int i=2;i<MAX_N;i++){ if(!check[i])prime[tot++]=i; for(int j=0;j<tot;j++){ if(prime[j]*i>=MAX_N)break; check[prime[j]*i]=1; if(i%prime[j]==0)break; } } } void factor(int n,int e[MAX_N],int x){ for(int i=0;i<tot;i++){ while(n%prime[i]==0){ e[i]+=x; n/=prime[i]; } if(n==1)break; } } void f(int n,int x){ for(int i=1;i<=n;i++){ factor(i,e,x); } } int main() { IsPrime(); int p,q,r,s; while(cin>>p>>q>>r>>s){ memset(e,0,sizeof(e)); f(p,1); f(s,1); f(r-s,1); f(p-q,-1); f(q,-1); f(r,-1); double ans=1; for(int i=0;i<tot;i++){ ans*=pow(prime[i],e[i]); } printf("%.5lf\n",ans); } return 0; }
相关文章推荐
- android5.1 recentApp 移除全部
- pta---搜索树判断
- android developer tiny share-20160711
- android developer tiny share-20160711
- 输出二叉树中路径上结点值之和为给定值的所有路径
- codeforces 361 B - Mike and Shortcuts
- 基于busybox文件系统挂载nfs卡死现象解决办法
- QtQuick 上拉加载的实现
- 谈谈RGB、YUY2、YUYV、YVYU、UYVY、AYUV
- hdu2031 进制转换
- bzoj 4415: [Shoi2013]发牌
- 默认复制构造函数 bitwise 语义 delete 多次----腾讯2016研发工程师笔试题(一)
- 命令行安装IIS方法之一
- volley的使用(二)
- FastDFS install
- [leetcode-372]Super Pow(java)
- 自定义控件_day02
- 建kafka gradle工程解决证书认证问题
- babystep.....脑残的baby(QAQ)提高训练
- $().each和$.each()的区别