csu 1994 Drug (lucas定理+容斥原理)

本文提供了一道编号为1994的编程题的解答思路及代码实现,使用C++语言,通过快速幂、组合数计算等算法解决数学组合问题,并特别针对n=r=1的情况进行了讨论。

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

题目链接:点击打开链接

附上代码,这个题目有一个坑的地方就是n=r=1的时候,我没有特判(请教了中南Xu Shu大佬了还是没有找到错误),结果wa了一天(qaq,附上本菜鸡的代码)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const LL p=1e9+7;
const int SIZE=1e6+100;
LL inv[SIZE];

LL quick_mod(LL a, LL b)
{
    LL ans = 1;
    a %= p;
    while(b)
    {
        if(b & 1) ans = ans * a % p;
        b >>= 1;
        a = a * a % p;
    }
    return ans;
}

LL C(LL n, LL m)
{
    if(m > n) return 0;
    LL ans = 1;
    for(int i=1; i<=m; i++)
    {
        LL a = (n + i - m) % p;
        LL b = i % p;
        ans = ans * (a * inv[b]%p) % p;
    }
    return ans;
}

LL Lucas(LL n, LL m)
{
    if(m == 0) return 1;
    return C(n % p, m % p) * Lucas(n / p, m / p) % p;
}

int main()
{
    int T,nofcase=1;
    for(int i=1;i<SIZE;i++)
        inv[i]=quick_mod(i,p-2);
    scanf("%d", &T);
    while(T--)
    {
        LL n,m,r;
        scanf("%lld%lld%lld",&n,&m,&r);
        LL di=Lucas(m,r);
        LL ai=0;
        LL tmp=r;
        for(int i=2;i<=r;i++){
            int sign=(r-i)&1;
            int a = (r-i+1);
            int b = i ;
            tmp=tmp*(a*inv[b]%p)%p;
            if(!sign) ai=(ai+i*tmp%p*quick_mod(i-1,n-1)%p)%p;
            else ai=(ai-i*tmp%p*quick_mod(i-1,n-1)%p+p)%p;
        }
        LL answer=di*ai%p;
        if(n==1&&r==1) printf("Case #%d: %lld\n",nofcase++,m);  //这里其实可以不用特判,因为0^0=1,不过这是我踩过的坑,特意标记出来~qaq
        else printf("Case #%d: %lld\n",nofcase++,answer);
    }
    return 0;
}
/**********************************************************************
	Problem: 1994
	User: Siryuanshao
	Language: C++
	Result: AC
	Time:3724 ms
	Memory:9836 kb
**********************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值