码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 10375 (唯一分解定理) Choose and divide

时间:2014-12-14 07:02:25      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

题意:

求组合数C(p, q) / C(r, s)结果保留5为小数。

分析:

先用筛法求出10000以内的质数,然后计算每个素数对应的指数,最后再根据指数计算答案。

bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 
 5 const int maxn = 10000;
 6 int pri[maxn], cnt, e[maxn];    //e记录每个素数的质数 
 7 bool vis[maxn + 10];
 8 
 9 void add_interger(int n, int d)    //乘以n的d次幂 
10 {
11     for(int i = 0; i < cnt; ++i)
12     {
13         while(n % pri[i] == 0)
14         {
15             n /= pri[i];
16             e[i] += d;
17         }
18         if(n == 1) return;
19     }
20 }
21 
22 void add_factorial(int n, int d)    //乘以(n!)的d次幂 
23 {
24     for(int i = 2; i <= n; ++i)
25         add_interger(i, d);
26 }
27 
28 int main()
29 {
30     //freopen("10375in.txt", "r", stdin);
31     
32     int m = sqrt(maxn + 0.5);
33     for(int i = 2; i <= m; ++i) if(!vis[i])
34         for(int j = i*i; j <= maxn; j += i) vis[j] = true;
35     cnt = 0;
36     for(int i = 2; i < maxn; ++i) if(!vis[i]) pri[cnt++] = i;
37     //for(int i = 0; i < 10; ++i) printf("%d\n", pri[i]);
38     
39     int p, q, r, s;
40     while(scanf("%d%d%d%d", &p, &q, &r, &s) == 4)
41     {
42         memset(e, 0, sizeof(e));
43         
44         add_factorial(p, 1);
45         add_factorial(q, -1);
46         add_factorial(p-q, -1);
47         add_factorial(r, -1);
48         add_factorial(s, 1);
49         add_factorial(r-s, 1);
50         
51         double ans = 1.0;
52         for(int i = 0; i < cnt; ++i)
53             ans *= pow(pri[i], e[i]);
54             
55         printf("%.5f\n", ans);
56     }
57     
58     return 0;
59 }
代码君

 

UVa 10375 (唯一分解定理) Choose and divide

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/AOQNRMGYXLMV/p/4162185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!