链接:

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(组合数,中国剩余定理)的更多相关文章

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

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

  2. HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)

    Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...

  3. hdu X问题 (中国剩余定理不互质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory ...

  4. HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)

    分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...

  5. HDU 3579 Hello Kiki 中国剩余定理(合并方程

    题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> # ...

  6. hdu 3579 Hello Kiki (中国剩余定理)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. POJ 2891 Strange Way to Express Integers 中国剩余定理解法

    一种不断迭代,求新的求余方程的方法运用中国剩余定理. 总的来说,假设对方程操作.和这个定理的数学思想运用的不多的话.是非常困难的. 參照了这个博客的程序写的: http://scturtle.is-p ...

  8. 中国剩余定理&Lucas定理&按位与——hdu 5446

    链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...

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

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

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

随机推荐

  1. 【转帖】为什么redis 是单线程的?

    为什么redis 是单线程的? https://cloud.tencent.com/developer/article/1120615 云服务器企业新用户优先购,享双11同等价格 立即抢购 以前一直有 ...

  2. [转帖]MySQL语句大全

    MySQL语句大全 https://www.cnblogs.com/jicki/p/5548676.html 一.连接mysql. 格式: mysql -h主机地址 -u用户名 -p用户密码 二.修改 ...

  3. phpstrom xdebug phpstudy调试,跳不到设置断点的原因,以及配置方法

    设置的是127 的地址,而用localhost 方式请求,所以无法跳到断点 环境 phpstudy 2018 php 7.2.10 xdebug版本 (因为要与php版本要对应,注意别选错了,我的用的 ...

  4. oracle 常用sql 经典sql函数使用 sql语法

    各种树操作, 用来查询表中带有子父节点的信息 Oracle 树操作(select-start with-connect by-prior) select m.org_id from sm_organ ...

  5. Python-02-基础知识

    一.第一个Python程序 [第一步]新建一个hello.txt [第二步]将后缀名txt改为py [第三步]使用记事本编辑该文件 [第四步]在cmd中运行该文件 print("Hello ...

  6. Python random模块(以后用到一个再更新一个)

    random模块是产生随机数的模块 1.random.random() 这是产生0~1之间一个随机浮点数,但是不会包括1 import random num = 0 while num < 10 ...

  7. 1010 Radix:猥琐的测试数据

    谨以此题纪念边界测试数据浪费了我多少时间:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 # ...

  8. 全面优化MySQL

    MySQL性能瓶颈原因 硬件.系统因素 CPU 磁盘I/O 网络性能 操作系统争用 MySQL相关因素 数据库设计 索引.数据类型 应用程序性能 特定请求.短时事务 配置变量 缓冲区.高速缓存.Inn ...

  9. dfs的剪枝优化

    两个剪枝问题 1. 当两点的距离(需要走的步数)大于剩下的时间时 剪去 2.奇偶剪枝问题 如果起点到终点所需走的步数的奇偶性与时间奇偶性不同的时候 剪去 起点到终点步数的奇偶性的判断 首先 明确点的奇 ...

  10. hdu 2102 a计划问题。。 双层dfs问题

    Problem Description 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验.魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长 ...