HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5446
Unknown Treasure
#### 问题描述
> On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a combination lock and some numbers on it. After quite a research, the mathematician found out that the correct combination to the lock would be obtained by calculating how many ways are there to pick m different apples among n of them and modulo it with M. M is the product of several different primes.
#### 输入
> On the first line there is an integer T(T≤20) representing the number of test cases.
>
> 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 of primes. Following on the next line are k different primes p1,...,pk. It is guaranteed that M=p1⋅p2⋅⋅⋅pk≤1018 and pi≤105 for every i∈{1,...,k}.
#### 输出
> For each test case output the correct combination on a line.
#### 样例
> **sample input**
> 1
> 9 5 2
> 3 5
>
> **sample output**
> 6
题意
求C[n][m]%(P1 * P2 * P3 * ... * pk)
题解
由于n,m都特别大,所以我们用卢卡斯定理对C[n][m]进行pi进制的拆项得到结果ai,用卢卡斯定理的时候p不能太大,否则就没有意义了,所以我们不能直接用M=P1 * P2 * P3 * ... * pk(而且这个不是质数!!!)进行拆项。
然后对所有的ai用中国剩余定理求出C[n][m]%(P1 * P2 * P3 * ... * pk)。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
LL pi[22], a[22];
LL mul(LL a, LL n, LL mod) {
LL ret = 0;
LL t1 = a, t2 = n;
while (n) {
//puts("mul");
if (n & 1) ret = (ret + a) % mod;
a = (a + a) % mod;
n >>= 1;
}
return ret;
}
void gcd(LL a, LL b, LL& d, LL& x, LL& y) {
if (!b) { d = a; x = 1; y = 0; }
else { gcd(b, a%b, d, y, x); y -= x*(a / b); }
}
LL inv(LL a, LL mod) {
LL d, x, y;
gcd(a, mod, d, x, y);
return d == 1 ? (x + mod) % mod : -1;
}
LL get_C(LL n, LL m, LL mod) {
if (n < m) return 0;
LL ret = 1;
for (int i = 0; i < m; i++) ret = ret*(n - i) % mod;
LL fac_m = 1;
for (int i = 1; i <= m; i++) fac_m = fac_m*i%mod;
return ret*inv(fac_m, mod) % mod;
}
LL lucas(LL n, LL m, LL mod) {
if (m == 0) return 1LL;
return get_C(n%mod, m%mod, mod)*lucas(n / mod, m / mod, mod) % mod;
}
LL china(int n) {
LL M = 1, d, y, x = 0;
for (int i = 0; i < n; i++) M *= pi[i];
for (int i = 0; i < n; i++) {
LL w = M / pi[i];
gcd(pi[i], w, d, d, y);
x = (x + mul(mul(y, w, M), a[i], M)) % M;
}
return (x + M) % M;
}
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
LL n, m; int k;
scanf("%lld%lld%d", &n, &m, &k);
for (int i = 0; i < k; i++) {
scanf("%lld", &pi[i]);
a[i] = lucas(n, m, pi[i]);
}
LL ans = china(k);
printf("%lld\n", ans);
}
return 0;
}
HDU 5446 Unknown Treasure Lucas+中国剩余定理的更多相关文章
- hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k]) 0< n,m < 1018 思路:这题基本上算是模版题了 ...
- 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 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 + 中国剩余定理 + 模拟乘法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...
- 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——————【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 ...
- HDU 5446 Unknown Treasure
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure(Lucas定理+CRT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...
随机推荐
- 【转】准确理解CSS clear:left/right的含义及实际用途
零.说点什么 好久没更新了.并不是在折腾什么大作,而是广度学习与实践中,加上婚礼等诸多大事,所以产出较少. 今天这篇也只是小作,博客是自己很好的学习工具,只要我学习不止,博客也会不断更新的. 我们平时 ...
- 例题6-10 The Falling Leaves,UVA699
这道题我的思路是先通过递归构建树,然后进行遍历将位置和保存在map映射中,最后按顺序输出map集合中的值. 至于如何遍历,我是依次尝试了宽度优先遍历和深度优先遍历,当然这都是可以的.不过期间写错了很多 ...
- 无需server-U IIS7.5 在已有的多个WEB网站上配置FTP发布
1 新建一个用于ftp登陆的计算机用户. 操作:开始→管理工具→计算机管理→本地用户和组→用户,新建一个计算机用户,设置好用户名和密码,例如:nenkea nkscl 2 在web站点文件夹下,把新建 ...
- PHP字符串拼接与MySQL语句
这个部分总是钻牛角尖.总是出错. public function getList($pagesize=25){ $where = '1'; $tableName = $this->getTabl ...
- 9)Java内部类(Inner Class)
内部类:不可以有静态数据,静态方法或者又一个静态内部类 内部类的优点:隐藏类的细节,内部类可以声明为私有.内部类可以访问外部类的对象(包括private) 静态内部类:可以有静态数据,静 ...
- C语言-L Buffer is too small && 0 解决方法
问题如下: 问题出在程序语句(见下): 其中,字符串p1和p2分别指向某个字符串,p是定义的一个字符数组.问题出现在对strlen()的使用,这个函数计算的字符串长度是不包括'\0'的,所以在设置第二 ...
- 蓝牙4.0LED灯控方案
一.LED照明机遇 相对传统光源产品,LED灯凭借其光效高.寿命长.不含汞.总拥有成本低等优势,已被普遍认为是一种革命性和替代性的技术.随着全球白炽灯禁产.禁用政策的依次落实,白炽灯将逐渐消失于市场. ...
- WPF Event 在 Command 中的应用初级篇,支持所有Event 展示松耦合设计的全部代码 - 解决TextBoxBase.TextChanged或者TextBox.TextChanged等类似事件绑定问题。
做过WPF开发的人,都知道做MVVM架构,最麻烦的是Event的绑定,因为Event是不能被绑定的,同时现有的条件下,命令是无法替代Event.而在开发过程中无法避免Event事件,这样MVVM的架构 ...
- Secondary IP Addressing
Secondary IP Addressing secondary IP addressing. Secondary addressing uses multiple networks or subn ...
- ToolBar存档
上图是将本阶段要完成的结果画面做了标示,结合下面的描述希望大家能明白. colorPrimaryDark(状态栏底色):在风格 (styles) 或是主题 (themes) 里进行设定. App ba ...