2015 Multi-University Training Contest 10 (hdu 5407 CRB and Candies)
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.
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
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
题意:求g(n)=LCM(C(n,0),C(n,1),C(n,2)....C(n,n))。
思路:先占一下官方的题解
关于这个定理的证明在知乎上有大佬给出了:https://www.zhihu.com/question/34859879
估计当时这道题也就是oeis了。
const ll mod=1e9+7;
const ll maxn=1000005;
int prime[maxn],pn;
int v[maxn];
ll f[maxn],g[maxn];
void init_prime()
{
int i, j;
for(i = 2; i * i <= maxn; ++i)
{
if(!prime[i])
for(j = i * i; j < maxn; j += i)
prime[j] = 1;
}
pn = 0;
for(i = 2;i <= maxn; ++i)
if(!prime[i])
prime[pn++] = i;
}
ll inv(ll a,ll m)
{
ll p=1,q=0,b=m,c,d;
while(b>0)
{
c=a/b;
d=a;
a=b;
b=d%b;
d=p;
p=q;
q=d-c*q;
}
return p<0?p+m:p;
}
int main()
{
int t;
scanf("%d",&t);
memset(v,0,sizeof(v));
init_prime();
for(int i=0;i<pn;i++)
{
ll k=prime[i];
while(k<maxn)
{
v[k]=prime[i];
k*=prime[i];
}
}
f[1]=1;
for(int i=2;i<maxn;i++)
{
if(v[i])
{
f[i]=f[i-1]*v[i];
f[i]%=mod;
}
else f[i]=f[i-1];
f[i]%=mod;
g[i-1]=f[i]*inv(i,mod);
g[i-1]%=mod;
}
int n;
while(t--)
{
scanf("%d",&n);
printf("%I64d\n",g[n]);
}
return 0;
}