Lucas 定理。。。

定义推导
公式:

(nm)%p=mpnp(m%pn%p)%p;(p)

用于求大组合数取模素数

数学证明:

n=sp+q;m=tp+r;(q,rp)
(ip)=p!i!(pi)!;
p 是素数, 且 p 大于i,
p 不会被约,
即结果中必定存在 p ;
(ip)0(modp),i>0 && i<p;
(1+x)p(1+xp) (mod p)

证明过程

左边 xtp+r 的系数为 C(sp + q, tp + r);
右边 xtp 的系数为C(s, t), xq 的系数为C(q,r);
所以这里写图片描述得证。。。
只需要对C(s, t)进行递归调用就可以了。。。时间O( lognpp )
即 C(n, m) % p = C( np , mp ) * C(n%p, m%p) % p;
完。。。

所以。。
对于C(A, B) 将A, B写成P进制。。
即A=a[n] * a[n-1] * … * a[0],B=b[n] * b[n-1] * … * b[0];
即C(A,B) ≡ C(a[n], b[n]) * C(a[n-1], b[n-1]) * … * C(a[0], b[0]) ( mod p);
即 C(n, m) % p = C( np , mp ) * C(n%p, m%p) % p; ( p105

题目模板。。。

hdu 3037

题目大意:简单来说就是求C(n + m, m) % p;

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <cctype>
#include <ctime>
#define MAX 0x3f3f3f3f
#define LL __int64
#define MOD (100)
#define N (20 + 1)

using namespace std;

LL exp_mod(LL a, LL b, LL p)
{
    LL res = 1;

    while(b != 0)
    {
        if(b & 1)
        {
            res = res * a % p;
        }

        a = a * a % p;
        b >>= 1;
    }

    return res;
}

LL Comb(LL a, LL b, LL p)
{
    if (a < b)
    {
        return 0;
    }
    if (a == b)
    {
        return 1;
    }
    if (b > a - b)
    {
        b = a - b;
    }

    LL ca = 1, cb = 1, ans = 1;

    for (LL i = 0; i < b; ++i)
    {
        ca = (ca * (a - i)) % p;
        cb = (cb * (b - i)) % p;
    }

    ans = (ca * exp_mod(cb, p - 2, p)) % p; //(ca/cb)%p CB * cb ≡ 1(mod p); CB = cb^(n - 2);

    return ans;
}

LL Lucas(int n, int m, int p)
{
    LL ans = 1;

    while (n && m && p)
    {
        ans = (ans * Comb(n % p, m % p, p)) % p;
        n /= p;
        m /= p;
    }

    return ans;
}

int main()
{
    int n, m, p, T;
    scanf("%d", &T);

    while (T--)
    {
        scanf("%d%d%d", &n, &m, &p);
        printf("%lld\n", Lucas(n + m, m, p));
    }

    return 0;
}

hdu 5446

题目大意:求C(n, m)的组合数取模M(M = P1 * p2 * … * pk) pi为素数。。。
中国剩余定理 + Lucas定理。。。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <cctype>
#include <ctime>
#define MAX 0x3f3f3f3f
#define LL __int64
#define MOD (100)
#define N 15

using namespace std;

LL A[N], B[N];

LL exp_mod(LL a, LL b, LL p)
{
    LL res = 1;

    while (b)
    {
        if (b & 1)
        {
            res = (res * a) % p;
        }

        a = (a * a) % p;
        b >>= 1;
    }

    return res;
}

LL Comb(LL a, LL b, LL p)
{
    if (a < b) 
    {
        return  0;
    }
    if (a == b)
    {
        return 1;
    }
    if (b > a - b)
    {
        b = a - b;
    }

    LL ans = 1, ca = 1, cb = 1;

    for (int i = 0; i < b; ++i)
    {
        ca = (ca * (a - i)) % p;
        cb = (cb * (b - i)) % p;
    }

    ans = (ca * exp_mod(cb, p - 2, p)) % p;
    return ans;
}

LL Lucas(LL n, LL m, LL p)
{
    LL ans = 1;

    while (n && m && p)
    {
        ans = (ans * Comb(n % p, m % p, p)) % p;
        n /= p;
        m /= p;
    }

    return ans;
}

LL ext_gcd(LL a, LL b, LL &x, LL &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }

    LL ret = ext_gcd(b, a % b, y, x);
    y -= a / b * x;
    return ret;
}

LL multi(LL a, LL b, LL p)  
{
    LL ret = 0;

    while (b)
    {
        if (b & 1)
        {
            ret = (ret + a) % p;
        }

        a = (a + a) % p;
        b >>= 1;
    }

    return ret;
}

LL CRT(LL n, LL *m, LL *a)
{
    LL M = 1, d, y, x = 0;

    for (int i = 0; i < n; i++)
    {
        M *= m[i];
    }

    for (int i = 0; i < n; i++)
    {
        LL w = M / m[i];
        d = ext_gcd(m[i], w, d, y);
        x = (x + multi(multi(y, w, M), a[i], M)) % M;
    }

    return (x + M) % M;
}

int main()
{
    int T;
    scanf("%d", &T);

    while (T--)
    {
        LL m, n, k;
        scanf("%lld%lld%lld", &n, &m, &k);

        for (int i = 0; i < k; i++)
        {
            scanf("%lld", &A[i]);
            B[i] = Lucas(n, m, A[i]);
        }

        printf("%lld\n", CRT(k, A, B));
    }

    return 0;
}

证明参考于 百度百科
说明参考于 pi9nc的专栏的博客
菜鸟一只。。。有什么错误多多指教。。。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值