CRB and Candies (combinatorial math + inverse element + lcm)

Topic link: http://acm.hdu.edu.cn/showproblem.php?pid=5407

topic:

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
 
Question meaning: Find the least common multiple of C(n,0) ~C(n,n).
Idea: The result is the least common multiple of 1~(n+1) divided by n+1. Please press the portal ~ For finding the least common multiple of 1~n+1, it is actually dividing all prime numbers within 1~n+1 The largest power that falls within the interval can be multiplied by ~
The code is implemented as follows:
 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 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325268594&siteId=291194637