首页 > 其他 > 详细

CRB and Candies(lcm(C(n,0)..C(n,n)=lcm(1,2,,,n+1)/(n+1)))hdu5407

时间:2015-08-21 15:45:08      阅读:224      评论:0      收藏:0      [点我收藏+]

CRB and Candies

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


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  10^6
 


 

Output

 

For each test case, output a single integer – LCM modulo 1000000007(10^9+7).
 


 

Sample Input

 

5 1 2 3 4 5
 


 

Sample Output

 

1 2 3 12 10
 


 

Author

 

KUT(DPRK)
 


 

Source

 

 


 

 

解题:

技术分享

 

转载请注明出处:寻找&星空の孩子  

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5407

 

//lcm(C(n,0),C(n,1),C(n,2),,,,C(n,n))=lcm(1,2,,,,n,n+1)/(n+1)
//乘法逆元!(除法+取模)
//f(1)=1
//if n=p^k then f(n)=f(n-1)*p  else f(n) =f(n-1)
#include<stdio.h>
#include<string.h>
#define mod 1000000007
#define LL __int64
const LL maxv=1e6+5;
bool isnp[maxv]={false};
LL prime[maxv],pnum;//素数数组,素数个数
LL cas,f[maxv]={0};
void get_prime()//素数打表
{
    pnum=0;
    LL i,j;
 //   memset(isnp,0,sizeof(isnp));
    isnp[0]=isnp[1]=true;
    for(i=2; i<maxv; i++)
    {
        if(!isnp[i]){prime[pnum]=i;pnum++;}
        for(j=0; j<pnum&&prime[j]*i<maxv; j++)
        {
            isnp[i*prime[j]]=true;
            if(i%prime[j]==0)break;
        }
    }

    for(i=0;i<pnum;i++)
    {
        for(j=prime[i];j<maxv;j*=prime[i])
        {
            f[j]=prime[i];
        }
    }

}
void init()
{
    get_prime();
    f[1]=1;
    for(LL i=2;i<maxv;i++)
    {
        if(f[i]) f[i]=f[i]*f[i-1]%mod;
        else f[i]=f[i-1];
    }
//    for(LL i=2;i<100;i++)
//    {
//        printf("i=%I64d\t f=%I64d\n",i,f[i]);
//        if(i%10==0)printf("\n");
//    }
}
void exgcd(LL a,LL b,LL &d,LL &x,LL &y)
{
    if(!b){d=a;x=1;y=0;}
    else
    {
        exgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}

int main()
{
    int T;
    LL n;
    init();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d",&n);
        LL x,y,d;
        exgcd(n+1,mod,d,x,y);//ax = 1 (mod m)
        if(d==1){x=(x%mod+mod)%mod;}
        printf("%I64d\n",f[n+1]*x%mod);
    }
    return 0;
}


 

 

 

 

 

 

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

CRB and Candies(lcm(C(n,0)..C(n,n)=lcm(1,2,,,n+1)/(n+1)))hdu5407

原文:http://blog.csdn.net/u010579068/article/details/47833525

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!