定义推导
公式:
用于求大组合数取模素数
数学证明:
令
n=s∗p+q;m=t∗p+r;(q,r≤p)
∵(ip)=p!i!∗(p−i)!;
∵p
是素数, 且
p
大于
∴p
不会被约,
即结果中必定存在
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(
lognp∗p
)
即 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; (
p≤105
)
题目模板。。。
题目大意:简单来说就是求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;
}
题目大意:求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的专栏的博客
菜鸟一只。。。有什么错误多多指教。。。