您的位置:首页 > 其它

HDU 5407 CRB and Candies 数论

2017-07-23 14:47 169 查看
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
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: