HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】

本文介绍了一道关于组合数学的问题CRBandCandies,该题要求计算所有可能的糖果组合方式的最小公倍数,并提供了一种通过预处理最小公倍数和求逆元的方法来解决该问题的有效算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CRB and Candies

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


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

 

 

题目大意:让你求LCM(C(n,0),C(n,1),C(n,2)...C(n,n-1),C(n,n)),最后结果取模。

解题思路:其实只要有公式了,问题就很好解决了。f(n)是求1 - n的最小公倍数。这个是可以借鉴得。如果n是一个素数p的k次方,那么就乘以素数p。主要需要求逆元,和快速判断x是否为素数p的k次方。

 

#include<bits/stdc++.h>
using namespace std;
typedef long long INT;
const int maxn=1e6+20;
const INT MOD=1e9+7;
INT f[maxn],g[maxn],inv[maxn];
int p[maxn];
void init(){
    for(int i=1;i<maxn;i++){
        p[i]=i;
    }
    for(int i=2;i<maxn;i++){
        if(p[i]==i){
            for(int j=i+i;j<maxn;j+=i){
                p[j]=i;
            }
        }
    }
}
bool check(int x){
    int d=p[x];
    if(x>1){
        while(x%d==0){
            x/=d;
        }
        return x==1;
    }
    return false;
}
void get_f(){
    f[1]=1;
    for(int i=2;i<maxn;i++){
        if(check(i)){
            f[i]=f[i-1]*p[i]%MOD;
        }else{
            f[i]=f[i-1];
        }
    }
}
INT Powmod(INT a,INT n){
    a%=MOD;
    INT ret=1;
    while(n){
        if(n&1)
            ret= ret * a % MOD;
        n>>=1;
        a = (a*a)%MOD;
    }
    return ret;
}
INT get_inv(int n){
    return Powmod((INT)n,MOD-2);
}
INT get_g(int n){
    return f[n+1]*get_inv(n+1)%MOD;
}
int main(){
    int t,n;
    init();
    get_f();
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        INT ans=get_g(n);
        printf("%lld\n",ans);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/chengsheng/p/4803332.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值