CRB and Candies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 584 Accepted Submission(s): 292
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 ≤ K ≤ N )?
CRB is too hungry to check all of your answers one by one, so he only asks least common multiple(LCM) of all answers.
He wonders how many combinations he can select.
Can you answer his question for all K (0 ≤ K ≤ N )?
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 ≤ N ≤ 106
1 ≤ T ≤ 300
1 ≤ N ≤ 106
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
Author
KUT(DPRK)
Source
题目大意:GRB有n种糖果,每次可以选取k个。GRB想知道,对于所有的K,求lcm(C(n,0),…,C(n,k),…,C(n,n))mod 1000000007。
解题思路:
代码如下:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#include <ctime>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
const int N = 1000010;
int n, t;
ll ans, mid;
//inv[i]表示i的逆元;f[i]表示1~i的最小公倍数;anss[i]表示i对应的结果
ll inv[N], f[N], anss[N];
//valid[]标记素数
bool valid[N];
//p[i]储存素数
int p[N], su1[N];
void solve()
{
inv[1] = 1;
for(int i = 2; i < N; i++)//求i的逆元
inv[i] = inv[mod%i]*(mod-mod/i) % mod;
memset(valid, 0, sizeof(valid));//线性筛选N以内的素数
int k = 0;
for(int i = 2; i<N; i++)
{
if(valid[i]==0)
p[k++]=i;
for(int j = 0; j<k && i*p[j]<N; j++)
{
valid[i*p[j]] = 1;
if(i%p[j] == 0)
break;
}
}
memset(su1, 0, sizeof(su1));
for(int i = 0; i < k; i++)//寻找满足p^k的数,其中p为素数,k为正整数
{
ll mid = p[i];
while(mid < N)
{
su1[mid] = p[i];
mid *= p[i];
}
}
f[1] = 1;//打N以内的1~i的最小公倍数表
for(int i = 2; i < N; i++)
{
if(su1[i])
f[i] = f[i-1]*su1[i];
else
f[i] = f[i-1];
f[i] %= mod;
anss[i-1] = (f[i]*inv[i])%mod;//答案表
}
}
int main()
{
solve();
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
printf("%I64d\n", anss[n]);
}
return 0;
}