卢卡斯定理模板
作者:互联网
3.卢卡斯定理
可用于求组合数mod p的值,其公式为:
\[ C_{n}^{k}=(C_{\frac{n}{p}}^{\frac{k}{p}}*C_{n\%p}^{k\%p})\% p \]
上模板:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
typedef long long ll;
int T;
ll n, m, p;
ll j[N];
ll pow(ll a, ll b, ll p)
{
ll res = 1;
for (; b; b >>= 1)
{
if (b & 1)
res = (res * a) % p;
a = a * a % p;
}
return res;
}
ll C(ll n, ll m)
{
if (m > n)
return 0;
return ((j[n] * pow(j[m], p - 2, p)) % p * pow(j[n - m], p - 2, p) % 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()
{
j[0] = 1;
cin >> T;
while (T--)
{
cin >> n >> m >> p;
for (int i = 1; i <= p; i++)
j[i] = j[i - 1] * i % p;
cout << Lucas(n + m, n) << endl;
}
system("pause");
return 0;
}
标签:return,int,res,定理,long,pow,卢卡斯,ll,模板 来源: https://www.cnblogs.com/StungYep/p/12253913.html