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);
}
}

HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘的更多相关文章

  1. HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...

  2. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

  3. hdu 5446 Unknown Treasure lucas和CRT

    Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  4. hdu 5446 Unknown Treasure Lucas定理+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  5. 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.. ...

  6. hdu 5446 Unknown Treasure 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

  7. HDU 5446 Unknown Treasure(Lucas定理+CRT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...

  8. 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 ...

  9. ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

随机推荐

  1. 【codeforces 429D】Tricky Function

    [题目链接]:http://codeforces.com/problemset/problem/429/D [题意] 给你n个数字; 让你求出一段区间[l,r] 使得 (r−l)2+(∑rl+1a[i ...

  2. Linux学习总结(14)——Linux权限控制

    linux中,权限的学习是必不可少的,不论是作为一名运维工程师或者是单一的管理者,学习好linux中的权限控制,你就可以保护好自己的隐私同时规划好你所管理的一切. 权限的学习是很多的,不要认为自己已经 ...

  3. [转] 经典排序算法 - 基数排序Radix sort

    原理类似桶排序,这里总是需要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,暂时忽视十位数 例如 待排序数组[62,14,59,88,16]简单点五个数字 分 ...

  4. how to deal with &quot;no such file error or diretory&quot; error for a new programmer in QT creator

    when i try to develop a hello demo in QT creator with the code following : #include<QApplication& ...

  5. 【Hibernate学习】 ——ORM(二)

    上篇博客主要介绍了一对一的关系,主要理解单向与双向的差别,主键关联与唯一外键关联的差别.以下继续介绍一对多与多对多关联. 一对多关联映射 一个班级相应多个学生 watermark/2/text/aHR ...

  6. JAVA:从public static void main(String args[])開始

    我们都知道当你要执行一个JAVA文件的时候必需要有一个main函数. 这是为什么呢? 跟C语言的道理一样,当你执行一个文件的时候.你必需要有一个入口函数或者入口地址,在C里面是main函数.相同的在J ...

  7. 设置linux session 编码

    设置linux session 编码 export LANG=zh_CN.utf-8

  8. 聊聊高并发(四十四)解析java.util.concurrent各个组件(二十) Executors工厂类

    Executor框架为了更方便使用,提供了Executors这个工厂类.通过一系列的静态工厂方法.能够高速地创建对应的Executor实例. 仅仅有一个nThreads參数的newFixedThrea ...

  9. 提高FPGA速度的quartus编译选项

    Turning on some optimizations in Quartus II may help increase it. Here are some you may want to try: ...

  10. hdoj--1005--Number Sequence(规律题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...