HDU-5446-UnknownTreasure(组合数,中国剩余定理)
链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5446
题意:
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.
思路:
lucas定理,p为素数时。
C(n, m) = C(n/p, m/p)+C(n%p, m%p).
对每个pi算出值,然后中国剩余定理求解。
因为数值较大。。很容易溢出,快速乘,顺序也会导致溢出。。
求逆元的时候,用快速幂会溢出,可以把快速幂里面的乘法用快速乘。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int MAXN = 1e5+10;
const int MOD = 1e9+7;
LL F[MAXN], Finv[MAXN];
LL P[MAXN], A[MAXN];
LL MulMod(LL a, LL b, LL mod)
{
LL res = 0;
while(b>0)
{
if (b&1)
res = (res+a)%mod;
a = (a+a)%mod;
b >>= 1;
}
return res;
}
LL PowMod(LL a, LL b, LL mod)
{
LL res = 1;
while(b>0)
{
if (b&1)
res = res*a%mod;
a = a*a%mod;
b >>= 1;
}
return res;
}
void Init(LL n, LL m, LL mod)
{
F[0] = F[1] = 1;
for (LL i = 2;i <= n;i++)
F[i] = F[i-1]*i%mod;
Finv[m] = PowMod(F[m], mod-2, mod);
Finv[n-m] = PowMod(F[n-m], mod-2, mod);
}
LL Comb(LL n, LL m, LL mod)
{
if (m > n)
return 0;
if (m == n)
return 1;
Init(n, m, mod);
return F[n]*Finv[m]%mod*Finv[n-m]%mod;
}
LL Lucas(LL n, LL m, LL mod)
{
if (m == 0)
return 1;
return MulMod(Lucas(n/mod, m/mod, mod), Comb(n%mod, m%mod, mod), mod);
}
void ExGCD(LL a, LL b, LL &x, LL &y)
{
if (b == 0)
{
x = 1, y = 0;
return;
}
ExGCD(b, a%b, x, y);
LL tmp = x;
x = y;
y = tmp-a/b*y;
}
LL CRT(int k)
{
LL Pm = 1;
LL res = 0;
for (int i = 1;i <= k;i++)
Pm *= P[i];
for (int i = 1;i <= k;i++)
{
LL x, y;
LL mi = Pm/P[i];
ExGCD(mi, P[i], x, y);
res = (res+MulMod(MulMod(x, mi, Pm), A[i], Pm))%Pm;
}
return (res+Pm)%Pm;
}
int main()
{
int t, k;
LL n, m;
scanf("%d", &t);
while(t--)
{
scanf("%lld%lld%d", &n, &m, &k);
for (int i = 1;i <= k;i++)
scanf("%lld", &P[i]);
for (int i = 1;i <= k;i++)
A[i] = Lucas(n, m, P[i]);
printf("%lld\n", CRT(k));
}
return 0;
}
HDU-5446-UnknownTreasure(组合数,中国剩余定理)的更多相关文章
- hdu 5446 Unknown Treasure 中国剩余定理+lucas
题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- hdu X问题 (中国剩余定理不互质)
http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others) Memory ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- HDU 3579 Hello Kiki 中国剩余定理(合并方程
题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> # ...
- hdu 3579 Hello Kiki (中国剩余定理)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理解法
一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...
- 中国剩余定理&Lucas定理&按位与——hdu 5446
链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...
- 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 + 中国剩余定理 + 模拟乘法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...
随机推荐
- [转帖]libev与libuv的区别
libev与libuv的区别 https://www.cnblogs.com/charlesblc/p/6341280.html 参考: http://blog.csdn.net/w616589292 ...
- [转帖]TPC-C解析系列03_TPC-C基准测试之SQL优化
TPC-C解析系列03_TPC-C基准测试之SQL优化 http://www.itpub.net/2019/10/08/3330/ TPC-C是一个非常严苛的基准测试模型,考验的是一个完备的关系数据库 ...
- Java串口通信 RXTX 解决过程
背景介绍: 由于第一次用Java与硬件通信,网上查了许多资料,在这进行整理,便于以后学习.本人串口测试是USB串口设备连接电脑,在设备管理器中找到端口名称(也可以通过一些虚拟串口工具模拟). 下面主要 ...
- Python03之单引号、双引号、三单引号、三双引号
今天在学习字符串的时候,发现字符串有时使用单引号,有时使用双引号,而有时还使用三引号.至此我整理如下: 单引号和双引号区别.三单引号和三双引号也没什么区别. 一: 两单引号之间不可以出现单引号,如果出 ...
- django 相关配置(pycharm)
第二步
- WUSTOJ 1321: Alphabet Cookies(Java)字符统计
题目链接:1321: Alphabet Cookies Description Kitty likes cookies very much, and especially the alphabet c ...
- Linux命令格式及7个常见终端命令
Linux命令格式 Linux常见的7个终端命令
- MySql五大引擎的区别以及优劣之分
五大引擎 一: MyISAM:在创建MyISAM的时候会出来三个默认的文件 1.tb_demo.frm,存储表定义:2.tb_demo.MYD,存储数据:3.tb_demo.MYI,存储索引. 因为M ...
- flutter从入门到精通三
flutter可以通过一套代码运行在多个平台上,包括移动,web,桌面,嵌入式,但是在 Web 平台的支持尚未达到 Beta 阶段,请不要用在生产环节,在阅读文档时候,推荐大家阅读https://fl ...
- Spring Cloud Alibaba学习笔记(5) - 整合Sentinel及Sentinel规则
整合Sentinel 应用整合Sentinel 在dependencies中添加依赖,即可整合Sentinel <dependency> <groupId>com.alibab ...