链接:

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. [转帖]AWS第一,「3A格局」稳固,活跃IP是如何被全球云厂商瓜分的?

    AWS第一,「3A格局」稳固,活跃IP是如何被全球云厂商瓜分的? 本文作者:王刚 2019-02-24 10:42 https://www.leiphone.com/news/201902/qsz3c ...

  2. Spyder中代码提示功能添加

    问题描述:Spyder中编写python程序时,无函数智能提示.如想要输入np.reshape,无reshape提示 预期目标:输入 np.  然后智能提示reshape 解决方法: 第一步:进入本地 ...

  3. 用php的for循环输出四边形,各种三角形和菱形【含空心版本】

    <?php // 实心版 //四边形 for( $i = 1; $i <=5; $i++ ){ for( $j = 1; $j <=5; $j++ ){ echo '*'; } ec ...

  4. Arm-Linux 移植 ssh

    背景: 自己拥有一块开发板,但是苦于上面没有ssh,比较不方便.正好趁这个机会,移植ssh.我们使用的ssh是openssh. host平台 :Ubuntu 18.04 arm平台 : S5P6818 ...

  5. Mongodb命令行导入导出数据

    第一步,找到mongodb安装目录第二步,从命令行进入mongodb安装目录下的bin目录第三步(1),导出数据命令(导出的文件有两种格式:json/csv,此处导出的是json文件,对于导出CSV文 ...

  6. jmeter命令行执行脚本_动态参数设置

    从04月换公司开始,就没静下来心来学习,其中发生了比较多的事情吧,不过不管如何,没坚持学习还是因为懒.本周交接完,下周去入职新公司,该静下心来学点什么了. ---------------------- ...

  7. kafka的生产者配置以及发送信息的三种方式

    1.Fire-and-forget 这种方式是不管发送成功与否,客户端都会返回成功.尽管大多数的时候Kafka 在发送失败后,会自己重新自动再一次发送消息,但是也会存在丢失消息的风险 Producer ...

  8. C#6.0的新语法特性

    https://www.cnblogs.com/dotnet261010/p/9147707.html https://www.cnblogs.com/wangdodo/p/7929050.html

  9. Access-Control-Max-Age

    app.UseCors(builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCreden ...

  10. react-native——tab配置及跳转

    在 App 中 tab 是常见的页面类型,在 RN 里使用 react-navigation 可快速地进行 tab 配置. 假设应用有4个页面,两个是tab页面,两个是详情页面. App.js //应 ...