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

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

 

2015 Multi-University Training Contest 10
 


 

 

解题:

CRB and Candies(lcm(C(n,0)..C(n,n)=lcm(1,2,,,n+1)/(n+1)))hdu5407_第1张图片

 

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

题目链接: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;
}


 

 

 

 

 

 

 

你可能感兴趣的:(gcd,2015,LCM,and,Candies,CRB,hdu5407,Multi-Universit)