HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
题意:求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);
}
}
HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘的更多相关文章
- HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
- hdu 5446 Unknown Treasure lucas和CRT
Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- hdu 5446 Unknown Treasure Lucas定理+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)
题目链接: Hdu 5446 Unknown Treasure 题目描述: 就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.. ...
- hdu 5446 Unknown Treasure 中国剩余定理+lucas
题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...
- HDU 5446 Unknown Treasure(Lucas定理+CRT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...
- HDU 5446——Unknown Treasure——————【CRT+lucas+exgcd+快速乘+递推求逆元】
Each test case starts with three integers n,m,k(1≤m≤n≤1018,1≤k≤10) on a line where k is the number o ...
- ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)
Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...
随机推荐
- 本地yum源安装docker
Directory listing for / audit-libs-python-2.7.6-3.el7.x86_64.rpm checkpolicy-2.5-4.el7.x86_64.rpm co ...
- 3步简单实现SSH无密码登录
下面简单的做下实验: 一:准备2台服务器: 192.168.10.101 2 192.168.10.102 二:关键字解释 ssh-keygen : 产生公钥与私钥对 ssh-copy-id :将本 ...
- caioj 1153 扩展欧几里德算法(解不定方程)
模板题 注意exgcd函数要稍微记一下 #include<cstdio> #include<cctype> #include<algorithm> #define ...
- 紫书 例题8-15 UVa 12174 (滑动窗口)
这道题就是给你一n长序列, 然后把这个序列按顺序分成很多段, 每段长s(最前面可以小于s, 只有第一段的后半段, 最后面也同样, 只有最后一段的前半段), 然后要求是每一段里面没有重复的数, 问你有几 ...
- Python seed() 函数--每次产生一样的随机数系列
import random random.seed( 10 ) print("Random number with seed 10 : ", random.random()) #0 ...
- weblogic 生产模式和开发模式的互相转换
weblogic 生产模式和开发模式的互相转换 学习了:http://blog.csdn.net/qew110123/article/details/45845935 weblogic10.3生产模式 ...
- HDU4324 Triangle LOVE【拓扑排序】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- Linux下离线安装MySQL
Linux下安装mysql 1 检查并卸载已安装mysql 命令:rpm -qa|grep -i mysql 命令:rpm -e --nodeps ‘上个命令后显示的本机已安装mysql依赖’ 如果存 ...
- centos7 更改网络后 无法重启network (解决办法)
今天由于用nat与本地局域网冲突,可能和之前ENSP搭建时虚拟机冲突造成 然后修改了VMnet8的ip,网关,DNS 同时更改虚拟网络编译器与VMnet8保持相同 进入虚拟机,更改/etc/sysco ...
- SparkCore基础(二)
* SparkCore基础(二) 继续探讨SparkCore,开门见山,不多废话. SparkApplication结构探讨 包含关系: 之前我们运行过很多App了,其实每一个App都包含若干个Job ...