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
解法:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+7;
const LL mod = 1e9+7;
//g(n) = lCM(C(n,0),C(n,1),...,C(n,n))
//f(n) = LCM(1,2,...,n) g(n)=f(n+1)/(n+1)
//f(1)=1, if(n=p^k) f(n)=f(n-1)*p else f(n)=f(n-1)
bool prime[maxn];
int pri[maxn], cnt;
LL p[maxn];
LL f[maxn], g[maxn];
void predeal()
{
for(int i=2; i*i<=maxn; i++){
if(!prime[i]){
for(int j=i*2; j<maxn; j+=i){
prime[j]=1;
}
}
}
cnt=0;
for(int i=2; i<maxn; i++){
if(!prime[i]){
pri[cnt++]=i;
}
}
}
LL qsm(LL a, LL n){
LL ret = 1;
while(n){
if(n&1) ret=ret*a%mod;
a = a*a%mod;
n>>=1;
}
return ret;
}
int main()
{
predeal();
memset(p, 0, sizeof(p));
for(int i=0; i<cnt; i++){
LL k=pri[i];
while(k<maxn){
p[k] = pri[i];
k*=pri[i];
}
}
f[1]=1;
for(int i=2; i<maxn; i++){
if(p[i]){
f[i]=f[i-1]*p[i]%mod;
}
else f[i]=f[i-1]%mod;
g[i-1]=f[i]*qsm(i,mod-2)%mod;
}
int T;
scanf("%d", &T);
while(T--){
int n;
scanf("%d", &n);
printf("%lld\n", g[n]);
}
return 0;
}
本文介绍了一种解决CRB糖果组合问题的方法,通过计算不同数量的糖果可能组成的组合数目的最小公倍数,并利用模运算确保数值大小可控。算法采用预处理素数和最小质因数的方式加速计算。

被折叠的 条评论
为什么被折叠?



