Unknown Treasure

Problem Description
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.
 
Input
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 kdifferent primes p1,...,pk. It is guaranteed that M=p1⋅p2⋅⋅⋅pk≤1018 and pi≤105 for every i∈{1,...,k}.

 
Output
For each test case output the correct combination on a line.
 
Sample Input
1
9 5 2
3 5
 
Sample Output
6
 
Source
 
 
题意:M=p1*p2*...pk;求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18。 pi为质数
题解:首先M=x*pi(1<=i<=k)
    M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi得到 B[i];
   最后会得到一个同余方程组
   x≡B[0](mod p[0])
   x≡B[1](mod p[1])
   x≡B[2](mod p[2])
   ......
   解这个同余方程组 用中国剩余定理
  但ll*ll都会爆所以用快速乘
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
LL M[],B[];
int t;
LL n,m,p,num;
LL exgcd(LL a, LL b, LL &x, LL &y)
{
if (!b)
{
x = ;
y = ;
return a;
}
LL gcd = exgcd(b, a % b, x, y);
LL t = x;
x = y;
y = t - (a / b) * x;
return gcd;
}
LL mult(LL a,LL b,LL p)
{
LL ans=;
while(b)
{
if(b&)
ans=(ans+a)%p;
b>>=;
a=(a+a)%p;
}
return ans;
}
LL inverse(LL num, LL mod)
{
LL x, y;
exgcd(num, mod, x, y);
return (x % mod + mod) % mod;
} LL C(LL a, LL b, LL mod)
{
if (b > a)
return ;
LL t1 = , t2 = ;
for (int i = ; i <= b; ++i)
{
t2 *= i;
t2 %= mod;
t1 *= (a - i + );
t1 %= mod;
}
return mult(t1,inverse(t2, mod),mod);
}
LL lucas(LL n, LL m, LL p)
{
if (m == )
return ;
return mult(C(n % p, m % p, p),lucas(n / p, m / p, p),p);
}
LL China(LL m[],LL b[],int k)//m:模数 b:余数
{
LL n=,xx,yy;
LL ans=;
for(int i=;i<k;i++)
n*=M[i];
for(int i=;i<k;i++)
{
LL t=n/M[i];
exgcd(t,M[i],xx,yy);
LL x1=mult(xx,t,n);
LL x2=mult(x1,b[i],n);
ans=(ans+x2)%n;
}
return (ans%n+n)%n;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld",&n,&m,&num);
for(int i=;i<num;i++)
{
scanf("%d",&p);
M[i]=p;
B[i]=lucas(n,m,p);
}
LL ans=China(M,B,num);
printf("%lld\n",ans);
}
return ;
}

来自Tisuama

HDU 5446 CRT+Lucas+快速乘的更多相关文章

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

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

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

  3. HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘

    HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k])     0< n,m < 1018 思路:这题基本上算是模版题了 ...

  4. CRT, lucas及其扩展形式

    CRT, lucas及其扩展形式 exgcd int exgcd(int a, int b, int &x, int &y) { if (b == 0) return a, x = 1 ...

  5. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  6. HDU 1930 CRT

    也是很模板的一道题,给出一些数,分割,模数固定是4个互质的. /** @Date : 2017-09-16 23:54:51 * @FileName: HDU 1930 CRT.cpp * @Plat ...

  7. HDU 1573 CRT

    CRT模板题 /** @Date : 2017-09-15 13:52:21 * @FileName: HDU 1573 CRT EXGCD.cpp * @Platform: Windows * @A ...

  8. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  9. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

随机推荐

  1. Codeforces_779_D.String Game_(二分)

    D. String Game time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  2. HTML `capture` 属性

    file 类型的 <input> 除了调起系统的文件选择框外,还可通过指定 capture 属性来现场拍照或录制.配合 accept 属性,可实现更加便捷的文件获取. 比如想要录制一段视频 ...

  3. 新安装数据库sqlserver2008r2,使用javaweb连接不上问题处理

    鼠标右键[计算机]-->[管理],打开界面如下: 选择自己数据库的实例名: 选择TCP/IP:右键[属性],将所有TCP动态端口的[0]删掉,TCP端口设为1433:重启服务,即可连接. PS: ...

  4. The following packages have unmet dependencies:

    root@ubuntu:~# apt-get install open-iscsiReading package lists... DoneBuilding dependency treeReadin ...

  5. top命令的用法

    top命令的用法 2018年07月15日 09:50:04 zhuoya_ 阅读数:1858    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/z ...

  6. TWaver动画之雷达扫描效果

    UI和功能是好的产品的两个重要因素,很多产品往往只注重功能上的设计,而忽略了UI.在这个“看脸”的时代,就算产品的功能很强大,如果UI跟不上步伐,你的产品都会在客户心中大打折扣.做安全和监控的项目中经 ...

  7. 洛谷—— P1450 [HAOI2008]硬币购物

    P1450 [HAOI2008]硬币购物 硬币购物一共有$4$种硬币.面值分别为$c1,c2,c3,c4$.某人去商店买东西,去了$tot$次.每次带$di$枚$ci$硬币,买$si$的价值的东西.请 ...

  8. 透彻分析C/C++中memset函数

    在C语言中,经常需要对内存进行操作,里面涉及很多函数,但是memset函数的使用有一点需要大家格外注意,这也是我在做项目时遇到过的一个问题,调试了很久才找出来错误. 函数原型是:void *memse ...

  9. linux ifstat-统计网络接口流量状态

    推荐:更多linux 性能监测与优化 关注:linux命令大全 ifstat命令就像iostat/vmstat描述其它的系统状况一样,是一个统计网络接口活动状态的工具.ifstat工具系统中并不默认安 ...

  10. HDU 1325 拓扑排序

    根据题目所给的3个不符合情况的条件,一个个判断图是否符合这3个条件即可 1.不能出现内部环,拓扑排序判断 2.不能有超过1个点的入度为0,因为只有一个树根 3.每个点最多一个入度 这里要注意的一点是这 ...