Lucas定理

Lucas' theorem

In number theory, Lucas's theorem expresses the remainder of division of the binomial coefficient mathtex.cgi?%7Bm%20%5Cchoose%20n%7Dby a prime number p in terms of the base p expansions of the integers m and n.     ----wiki

表达式

p%7D%5C%20*%5C%20%20C_%7Bn%5C%p%7D%5E%7Bm%5C%p%7D

对于非负整数mn和素数p,如果有:


46939983.jpg

则有下式成立:

30039157.jpg

牛刀小试

题目链接:hdu-3037
题目大意:求在n棵树上摘不超过m颗豆子的方案,结果对p取模。
解题思路:
首先,n棵树上摘m课豆子的方案数相当于从n个数中可重复的选m个数的组合数,为mathtex.cgi?C_%7Bn+m-1%7D%5Em。那么现在就是求mathtex.cgi?C_%7Bn+m-1%7D%5E0+C_%7Bn+m-1%7D%5E1+C_%7Bn+m-1%7D%5E2+...+C_%7Bn+m-1%7D%5Em%20=%20C_%7Bn+m%7D%5Em

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 100001;
LL mod;
LL jc[N];

LL quick(LL a, LL b)
{
    LL c = 1;
    while(b)
    {
        if(b&1) c = c * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return c;
}

LL NY(LL a)
{
    return quick(a, mod-2);
}
void init()
{
    jc[0] = 1;
    for(LL i=1; i<mod; i++)
    {
        jc[i] = i * jc[i-1] % mod;
    }
}
LL C(LL n, LL m)
{
    if(n < m) return 0;
    return jc[n] % mod * NY(jc[m]) % mod * NY(jc[n-m]) % mod;
}
LL Lucas(LL n, LL m)
{
    if(!m) return 1;
    return Lucas(n/mod, m/mod) * C(n%mod, m%mod) % mod;
}

int main()
{
    LL n, m;
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%I64d %I64d %I64d", &n, &m, &mod);
        init();
        printf("%I64d\n", Lucas(n+m, m));
    }
    return 0;
}

转载于:https://www.cnblogs.com/WCB-ACM/p/5265987.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值