CRB and Candies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 209 Accepted Submission(s): 84
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.
1 ≤ T ≤ 300
1 ≤ N ≤ 106
1002.CRB and Candies
The problem is just to calculate g(N) = LCM(C(N,0),C(N,1),...,C(N,N)).
Introducing function f(n) = LCM(1,2,...,n), the fact g(n) = f(n+1)/(n+1)holds.
We calculate f(n) in the following way.
f(1)=1.
If n =pk then f(n) = f(n−1)× p, else f(n) = f(n−1).
Time complexity:O(N⋅logN)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include<cstring>
using namespace std;
int n,T;
const int MAXN = 1000100;
typedef long long ll;
ll f[MAXN],p[MAXN];
const int mod = 1000000007;
ll pmod(ll a, ll n)
{
ll ret = 1;
for (; n; n>>=1, a=a*a%mod) if (n & 1)
ret = ret * a % mod;
return ret;
}
bool ok(ll n)
{
int t = p[n];
while (n % t==0 && n > 1) n /= t;
return n==1;
}
ll inv(ll a)
{
return pmod(a, mod - 2);
}
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;
}
f[0] = 1;
for(int i = 1;i<MAXN;i++)
{
if(ok(i))
f[i] = f[i-1] * p[i] % mod;
else
f[i] = f[i-1];
}
// for(int i = 1;i<=200;i++)
// printf("%d %d\n",i,p[i]);
}
int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
n++;
ll ans = f[n] * inv(n) % mod;
printf("%I64d\n",ans);
}
return 0;
}