其他分享
首页 > 其他分享> > P3807 卢卡斯定理(模板)

P3807 卢卡斯定理(模板)

作者:互联网

传送门

给定整数 n, m, p 的值,求出 C((m+n),n) mod p 的值。

输入数据保证 p 为质数。

注: C 表示组合数。

 

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 10000005;
const int inf = 0x3f3f3f3f;
const int mod = 19260817;
int n, m;
ll p,a[maxn];
ll qpow(ll a, ll n) {
    ll res = 1;
    for (; n; n >>= 1, a = a * a % p)if (n & 1)res = res * a % p;
    return res;
}
ll C(ll n, ll m) {
    if (m > n)return 0;
    return (a[n] * qpow(a[m], p - 2)) % p * qpow(a[n - m], p - 2) % p;
}
ll Lucas(ll n, ll m) {
    if (!m)return 1;
    return C(n % p, m % p) * Lucas(n / p, m / p) % p;
}
int main() {
    //freopen("test.txt", "r", stdin);
    int t; scanf("%d", &t);
    while (t--) {
        scanf("%d%d%d", &n, &m, &p);
        a[0] = 1;
        for (int i = 1; i <= p; i++)a[i] = (a[i - 1] * i) % p;
        cout << Lucas(n + m, n) << endl;
    }
    return 0;
}

 

标签:qpow,return,int,res,ll,卢卡斯,P3807,模板,const
来源: https://www.cnblogs.com/MYMYACMer/p/14719441.html