HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘

HDU 5446 Unknown Treasure

题意:求C(n, m) %(p[1] * p[2] ··· p[k])     0< n,m < 1018

思路:这题基本上算是模版题了,Lucas定理求C(n,m),再用中国剩余定理合并模方程,因为LL相乘会越界,所以用到按位乘。

 #include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 10005
#define MAXK 15
using namespace std;
LL p[MAXK], mod[MAXK];
LL quick_mod(LL x, LL y, LL mod){
if (y == ) return ;
LL res = quick_mod(x, y >> , mod);
res = res * res % mod;
if (y & ){
res = res * x % mod;
}
return res;
}
LL comb(LL n, LL m, LL p){
LL res = ;
for (int i = ; i <= m; i++){
LL x = (n + i - m) % p;
LL y = i % p;
res = res * (x * quick_mod(y, p - , p) % p) % p;
}
return res;
}
LL lucas(LL n, LL m, LL p){
if (m == ) return ;
return lucas(n / p, m / p, p) * comb(n % p, m % p, p) % p;
} LL exgcd(LL a, LL b, LL & x, LL & y)
{
if (b == ){
x = ;
y = ;
return a;
}
else{
LL temp = exgcd(b, a % b, x, y);
LL t = y;
y = x - y * (a / b);
x = t;
return temp;
}
}
LL multi(LL a, LL b, LL m){
LL res = ;
while (b){
if (b & ){
res = (res + a) % m;
}
a = (a + a) % m;
b >>= ;
}
return (res + m) % m;
}
LL china(LL p[], LL mod[], LL m){
LL M = ;
for (int i = ; i <= m; i++){
M *= p[i];
}
LL x, y, res = ;
LL d;
for (int i = ; i <= m; i++){
LL s = M / p[i];
d = exgcd(p[i], s, d, y);
res = (res + multi(multi(y, s, M), mod[i], M)) % M;
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int T;
scanf("%d", &T);
LL n, m, k;
while (T--){
scanf("%I64d%I64d%I64d", &n, &m, &k);
for (int i = ; i <= k; i++){
scanf("%I64d", &p[i]);
mod[i] = lucas(n, m, p[i]);
}
LL ans = china(p, mod, k);
printf("%I64d\n", ans);
}
}
上一篇:cf228 div2 B. Fox and Cross ( 贪心, 模拟)


下一篇:ElastaticSearch -- searchAfter深度分页