HDU5446(2015年长春网络赛1010)
题目我就不列举了。。简单地说就是求C(n,m)%M
明显就是Lucas+中国剩余定理
无限WA就是long long * long long %m会爆。所以要处理一下
#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<vector>
#define Mn 200005
#define Me 1005
#define chn 26
#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define drep(i, r, l) for (int i = (r); i >= (l); --i)
using namespace std;
typedef long long LL;
LL n,m,k;
LL p[15],mod[15];
LL quick_mod(LL a, LL b,LL p)
{
LL ans = 1;
a %= p;
while(b)
{
if(b & 1)
{
ans = ans * a % p;
}
b >>= 1;
a = a * a % p;
}
return ans;
}
LL C(LL n, LL m,LL p)
{
if(m > n) return 0;
LL ans = 1;
for(int i=1; i<=m; i++)
{
LL a = (n + i - m) % p;
LL b = i % p;
ans = ans * (a* (quick_mod(b, p-2,p) % p)) % p;
}
return ans;
}
LL Lucas(LL n, LL m,LL p)
{
if(m == 0) return 1;
return C(n % p, m % p,p) * (Lucas(n / p, m / p,p) % p)%p;
}
// 中国剩余定理
LL Extended_Euclid(LL a, LL b, LL &x, LL &y) {
LL d;
if(b == 0) {
x = 1; y = 0;
return a;
}
d = Extended_Euclid(b, a % b, y, x);
y -= a / b * x;
return d;
}
LL mod_mul(LL a, LL b, LL mod) {
LL ret = 0;
while(b) {
if(b & 1) ret = (ret + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return ret;
}
LL china(LL k,LL *a,LL *mm) {
LL d, x, y, tmp, M, ret;
ret = 0;
M = 1;
for(int i = 0; i < k; i++)
M *= mm[i];
for(int i = 0; i < k; i++) {
tmp = M / mm[i];
d = Extended_Euclid(mm[i], tmp, x, y);
LL temp = mod_mul(y, tmp, M);
temp = mod_mul(temp, a[i], M);
ret = (ret + temp) % M;
//ret=(ret+y*tmp*a[i])%M;
}
return (M + ret) % M;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1.cpp", "r", stdin);
#endif
int T;
scanf("%d", &T);
while(T--)
{
scanf("%I64d%I64d%I64d", &n, &m, &k);
int i;
for(i=0;i<k;i++) scanf("%I64d",&p[i]);
for(i=0;i<k;i++) mod[i]=Lucas(n,m,p[i]);
printf("%I64d\n",china(k,mod,p));
}
return 0;
}