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

解法:
这里写图片描述

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值