CRB and Candies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 722 Accepted Submission(s): 361
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
2
3
4
5
Sample Output
1
2
3
12
10
Author
KUT(DPRK)
Source
题目大意:让你求LCM(C(n,0),C(n,1),C(n,2)...C(n,n-1),C(n,n)),最后结果取模。
解题思路:其实只要有公式了,问题就很好解决了。f(n)是求1 - n的最小公倍数。这个是可以借鉴得。如果n是一个素数p的k次方,那么就乘以素数p。主要需要求逆元,和快速判断x是否为素数p的k次方。
#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
const int maxn=1e6+20;
const INT MOD=1e9+7;
INT f[maxn],g[maxn],inv[maxn];
int p[maxn];
void init(){
for(int i=1;i<maxn;i++){
p[i]=i;
}
for(int i=2;i<maxn;i++){
if(p[i]==i){
for(int j=i+i;j<maxn;j+=i){
p[j]=i;
}
}
}
}
bool check(int x){
int d=p[x];
if(x>1){
while(x%d==0){
x/=d;
}
return x==1;
}
return false;
}
void get_f(){
f[1]=1;
for(int i=2;i<maxn;i++){
if(check(i)){
f[i]=f[i-1]*p[i]%MOD;
}else{
f[i]=f[i-1];
}
}
}
INT Powmod(INT a,INT n){
a%=MOD;
INT ret=1;
while(n){
if(n&1)
ret= ret * a % MOD;
n>>=1;
a = (a*a)%MOD;
}
return ret;
}
INT get_inv(int n){
return Powmod((INT)n,MOD-2);
}
INT get_g(int n){
return f[n+1]*get_inv(n+1)%MOD;
}
int main(){
int t,n;
init();
get_f();
scanf("%d",&t);
while(t--){
scanf("%d",&n);
INT ans=get_g(n);
printf("%lld\n",ans);
}
return 0;
}