@ZCDHJ
2019-09-16T13:28:07.000000Z
字数 1485
阅读 989
Lucas定理
Lucas定理:当 是质数时,对于任意正整数 满足 。
证明:令 ,,,。需证明 。。因为 ,所以 。那么就有 。考虑 也就是 实际上是在多项式 中 项的系数,由前面的同余式得到这个系数同时也是 也就是 取 取 的情况,所以 得证。
#include <iostream>#include <cstdio>#include <cmath>typedef long long LL;#define int LLconst int MAXN = 1e5;int T;int fac[MAXN | 1];inline int read() {register int x = 0;register char ch = getchar();while (!isdigit(ch)) ch = getchar();while (isdigit(ch)) {x = x * 10 + ch - '0';ch = getchar();}return x;}inline int fast_pow(int x, int y, int p) {int res = 1, base = x;while (y > 0) {if (y & 1) res = (res * base) % p;base = base * base % p;y >>= 1;}return res;}inline int C(int x, int y, int p) {if (x < y) return 0;return (fac[x] * fast_pow(fac[x - y], p - 2, p) % p * fast_pow(fac[y], p - 2, p)) % p;}int Lucas(int x, int y, int p) {if (!y) return 1;return C(x % p, y % p, p) * Lucas(x / p, y / p, p) % p;}signed main() {T = read();while (T--) {int n = read(), m = read(), p = read();fac[0] = 1;for (int i = 1; i <= p; ++i) fac[i] = (fac[i - 1] * i) % p;printf("%lld\n", Lucas(n + m, m, p));}return 0;}
