Hdoj.5407 CRB and Candies【数论。。。】 2015/12/05

CRB and Candies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 845    Accepted Submission(s): 405


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
 

Author
KUT(DPRK)
 

Source
题意:求C(0,n),C(1,n),。。。,C(n,n)的最小公倍数
解析:问题最终可化简为求LCM(1,2,3,...,n,n+1)/(n+1),求LCM时,只有碰到质数或质数的多次幂时最小公倍数才会改变。。。。。。
可以发现随着n的增长好多最小公倍数都是不变的,增长的位置都发生在有新的质因子产生或者原本质因子的次数增大的地方,

所以首先找出1-1000的所有质数,然后把1-1000000的所有的质数幂统计出来

然后lcm就求出来了,最后乘以(n+1)的逆元即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

#define mod 1000000007

int prime1[1010],ans[1000010],prime2[1000010];
bool p[1000010];
int cst=0;

void f(){
    int i,j;
    memset(p,false,sizeof(p));
    for( i = 2 ; i <= 1000000 ; ++i ){
        if( !p[i] ){
            if( i <= 1000 )
                prime1[cst++] = i;
            for( j = i+i ; j < 1000000 ; j+=i )
                p[j] = true;
        }
    }
}

void fun(){
    int i,j,k;
    memset(prime2,0,sizeof(prime2));
    for( i = 0 ; i < cst ; ++i ){
        j = prime1[i];
        while( j * prime1[i] < 1000001 ){
            j*=prime1[i];
            prime2[j] = prime1[i];
        }
    }
    ans[1] = 1;
    for( i = 2 ; i <= 1000001 ; ++i ){
        if( !p[i] ){
            ans[i] = (long long)ans[i-1]*i%mod;
        }
        else if( prime2[i] > 0 ){
            ans[i] = (long long)ans[i-1]*prime2[i]%mod;
        }
        else ans[i] = ans[i-1];
    }
}

int multi(int a,int b){
    if( b == 1 ) return a;
    long long ret = multi(a,b>>1);
    ret *= ret;
    ret %= mod;
    if( b%2==1 ) ret *= a;
    ret %= mod;
    return (int)ret;
}

int main(){
    int t,n,i,j;
    scanf("%d",&t);
    f();
    fun();
    while(t--){
        scanf("%d",&n);
        printf("%lld\n",(long long)ans[n+1]*multi(n+1,mod-2)%mod);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值