多校数论题-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
 


这道题一上来就打表了发现答案是lcm(1,2,3,……n,n+1)/(n+1)

然而lcm这个东西怎么求呢,

可以发现随着n的增长好多最小公倍数都是不变的,增长的位置都发生在有新的质因子产生或者原本质因子的次数增大的地方,

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

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



#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN=1020;
int prime[1022];
bool prime_[1000005];
int cnt[1000005]={0};
int a[1000005]={0};
const int MOD=1e9+7;
void getPrime_()
{
    memset(prime_,0,sizeof(prime_));
    prime_[1]=1;
    for (int i=2;i<=1000005;i++){
        if (prime_[i]==0)
        {
            for (int j=i;j<=1000005/i;j++)
            {
                prime_[j*i]=1;
            }
        }
    }
}
int fast(int a,int b)
{
    if (b==1) return a;
    long long ret=fast(a,b>>1);
    ret*=ret;
    ret%=MOD;
    if (b%2==1) ret*=a;
    ret%=MOD;
    return (int)ret;
}
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for (int i=2;i<=MAXN;i++){
        if (!prime[i]) prime[++prime[0]]=i;
        for (int j=1;j<=prime[0]&&prime[j]<=MAXN/i;j++){
            prime[prime[j]*i]=1;
            if (i%prime[j]==0) break;
        }
    }
}
long long factor[100][2];
int getFactors(long long x)
{
    int fatcnt=0;
    long long tmp=x;
    for (int i=1;prime[i]<=tmp/prime[i];i++){
        factor[fatcnt][1]=0;
        if (tmp%prime[i]==0){
            factor[fatcnt][0]=prime[i];
            while (tmp%prime[i]==0){
                factor[fatcnt][1]++;
                tmp/=prime[i];
            }
            fatcnt++;
        }
    }
    if (tmp!=1){
        factor[fatcnt][0]=tmp;
        factor[fatcnt++][1]=1;
    }
    return fatcnt;
}
int main(int argc, const char * argv[])
{
    int i,j,m,n,k;
    a[1]=1;
    getPrime();
    getPrime_();
    memset(cnt,0,sizeof(cnt));
    for (i=1;i<=prime[0];i++)
    {
        for (j=prime[i];1000005/prime[i]>=j;j*=prime[i])
        {
            cnt[prime[i]*j]=prime[i];
        }
    }
    for (i=2;i<=1000001;i++)
    {
        int ret=1;
        if (prime_[i]==0)
            a[i]=(long long)a[i-1]*i%MOD;
        else if (cnt[i]>0)
            a[i]=(long long)a[i-1]*cnt[i]%MOD;
        else
            a[i]=a[i-1];
        
        
    }
    cin>>k;
    while (k--)
    {
        scanf("%d",&n);
        cout<<(long long)a[n+1]*fast(n+1,MOD-2)%MOD<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值