大组合数Lucas

https://blog.youkuaiyun.com/sr_19930829/article/details/39058487

 LL Lucas(LL n, LL m, int p){
        return m ? Lucas(n/p, m/p, p) * comb(n%p, m%p, p) % p : 1;
 }

Saving Beans HDU3037

 

#include <iostream>
#include <string.h>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxm = 1e5 + 5;
ll fac[maxm];

void init(ll p) {
    fac[0]=1;
    for(int i = 1; i <= p; i++)
        fac[i] = fac[i - 1] * i % p;
}

ll qpow(ll x, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) {
            res = res * x % mod;
        }
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

ll lucas(ll n, ll m, ll p)
{
    ll res = 1;
    while(n && m)
    {
        ll a = n % p;
        ll b = m % p;
        if(a < b) return 0;
        res = (res * fac[a] * qpow(fac[b] * fac[a - b] % p, p - 2, p)) % p;
        n /= p;
        m /= p;
    }
    return res;
}

int t;
ll n, m, p;
int main() {
    scanf("%d", &t);
    while(t--) {
        scanf("%lld%lld%lld", &n, &m, &p);
        init(p);
        printf("%lld\n", lucas(n + m, m, p));
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/downrainsun/p/10400211.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值