CRB and Candies(组合数学+求逆元+lcm)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407

题目:

Problem Description
CRB has N different candies. He is going to eat K candies.
He wonders how many combinations he can select.
Can you answer his question for all K(0 ≤ KN)?
CRB is too hungry to check all of your answers one by one, so he only asks least common multiple(LCM) of all answers.
 

 

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case there is one line containing a single integer N.
1 ≤ T ≤ 300
1 ≤ N106
 

 

Output
For each test case, output a single integer – LCM modulo 1000000007( 109+7).
 

 

Sample Input
5 1 2 3 4 5
 

 

Sample Output
1 2 3 12 10
 
题意:求C(n,0) ~C(n,n)的最小公倍数。
思路:结果是1~(n+1)的最小公倍数除以n+1,证明过程请按 传送门~对于求1~n+1的最小公倍数其实就是将所有1~n+1内的所有素数的最大的落在该区间内的幂次相乘即可~
代码实现如下:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 const int maxn = 1e6 + 7;
 8 const int mod = 1e9 + 7;
 9 int t, n, len;
10 int p[maxn], is_prime[maxn];
11 
12 void init() {
13     len = 0;
14     for (int i = 0; i < maxn; i++) {
15         p[i] = 1;
16     }
17     p[0] = p[1] = 0;
18     for (int i = 2; i * i < maxn; i++) {
19         if (p[i]) {
20             for (int j = i * i; j < maxn; j += i) {
21                 p[j] = 0;
22             }
23         }
24     }
25     for(int i = 2; i < maxn; i++) {
26         if(p[i]) {
27             is_prime[len++] = i;
28         }
29     }
30 }
31 
32 ll ModPow(ll x, ll p) {
33     ll rec = 1;
34     while (p) {
35         if (p & 1) rec = (ll) rec * x % mod;
36         x = (ll) x * x % mod;
37         p >>= 1;
38     }
39     return rec;
40 }
41 
42 int main() {
43     init();
44     cin >> t;
45     while (t--) {
46         cin >> n;
47         ll ans = 1, tmp;
48         n++;
49         for (int i = 0; i < len && is_prime[i] <= n; i++) {
50             tmp = 1;
51             while (tmp * is_prime[i] <= n) {
52                 tmp = tmp * is_prime[i];
53             }
54             ans = ans * tmp % mod;
55         }
56         cout << (ans * ModPow(n, mod - 2) % mod) << endl;
57     }
58     return 0;
59 }

 

转载于:https://www.cnblogs.com/Dillonh/p/8990601.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值