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 }

猜你喜欢

转载自www.cnblogs.com/Dillonh/p/8990601.html
crb
今日推荐