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, ...
随机推荐
- kafka那些事儿
1 为什么用消息队列 1)解耦.服务之间没有强依赖,不需要关心调用服务时出现的各种异常,服务挂掉后接口超时等问题 2)异步.解决接口调用多服务时延时高的问题 3)高峰期服务间缓冲.解决工作节奏不一致问 ...
- URI和URL的关系与区别
首先给大家举个例子,有一家公司的总经理,某天,给了我一张名片,上面写了他的头衔,北京XXX公司总经理 张三,还有他的办公室地址,北京市海淀区长安街35号北京XXX公司总经理办公室,那么,我以后给我的朋 ...
- 是否应该学习qt源码(碰到问题的时候,或者文档对函数描述不清楚的时候,可以看一下)
是否应该学习qt源码 如果你想调用某个函数,但是文档并没有清晰描述这个函数的功能的时候,你就需要去阅读源码,看看Qt究竟是怎么实现的.比如用QNetworkAccessManager发送一个QHttp ...
- Linux磁盘管理系列 — LVM和RAID
一.逻辑卷管理器(LVM) 1.什么是逻辑卷管理器(LVM) LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对卷进行操作的抽象层. LVM是建立在硬盘 ...
- 偶数矩阵 Even Parity,UVa 11464
题目描述 Description 给你一个n*n的01矩阵(每个元素非0即1),你的任务是把尽量少的0变成1,使得每个元素的上.下.左.右的元素(如果存在的话)之和均为偶数.如图所示的矩阵至少要把3个 ...
- linux安装png2icon方法
此工具用于将png图片转换为ico格式的文件,一个小工具,但很实用 官网:http://www.winterdrache.de/freeware/png2ico/ 下载: wget http://ww ...
- linux Ubuntu14.04 make编译文件报错:No rule to make target `/usr/lib/libpython2.7.so', needed by `python/_pywraps2.so'. Stop.
错误过程:当“make”编译文件时报错No rule to make target `/usr/lib/libpython2.7.so', needed by `python/_pywraps2.so ...
- 用Python爬取小说《一念永恒》
我们首先选定从笔趣看网站爬取这本小说. 然后开始分析网页构造,这些与以前的分析过程大同小异,就不再多叙述了,只需要找到几个关键的标签和user-agent基本上就可以了. 那么下面,我们直接来看代码. ...
- EntityFramework进阶(二)- DbContext预热
本系列原创博客代码已在EntityFramework6.0.0测试通过,转载请标明出处 在DbContext首次调用的时候,会很慢,甚至会有5,6秒的等待,通常称为冷查询.再次调用的时候,几毫秒就能请 ...
- 如何让类数组也使用数组的方法比如:forEach()
思路: 让类数组绑定数组的方法<div>1</div><div>2</div>方法一: let div = document.getElementsBy ...