Unknown Treasure

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2209    Accepted Submission(s): 821

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 k different 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
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5842 5841 5840 5839 5838 
 
分析:
根据Lucas求解中 每一个i:C(n,m)%pi,然后根据中国剩余定理把这些结果整合起来。就能得到答案。
注意中国剩余定理在计算当前余数与其他余数乘积的最小公倍数的gcd为1的值时候,由于数据量太大,要取模。
#include<iostream>
#include<stdio.h>
using namespace std;
long long pri[];
long long a[];
long long ext_gcd(long long a,long long b,long long *x,long long *y)
{
if(b==)
{
*x=,*y=;
return a;
}
long long r = ext_gcd(b,a%b,x,y);
long long t = *x;
*x= *y;
*y = t - a/b * *y;
return r;
}
long long quick_mod(long long n,long long m,long long mod)
{
long long ans=;
while(m)
{
if(m&)
ans=(ans*n)%mod;
m>>=;
n=(n*n)%mod;
}
return ans%mod;
}
long long get_c(long long n,long long m,long long mod)
{
long long a=,b=;
for(int i=; i<=m; i++)
{
b=b*i%mod;
a=a*(n-i+)%mod;
}
return (a*(quick_mod(b,mod-,mod)))%mod;
}
long long Lucas(long long n,long long m,long long mod)
{
if(m==) return ;
return (Lucas(n/mod,m/mod,mod)*get_c(n%mod,m%mod,mod))%mod;
}
long long mul(long long a,long long n,long long mod)
{
a = (a%mod+mod)%mod;
n = (n%mod+mod)%mod;
long long ret =;
while(n)
{
if(n&)
ret=(ret+a)%mod;
a=(a+a)%mod;
n>>=;
}
return ret%mod;
}
long long chinese_reminder(long long a[],long long pri[],int len)
{
long long mul_pri=;
long long res=;
for(int i=;i<len;i++)
{
mul_pri*=pri[i];
}
for(int i=;i<len;i++)
{
long long m = mul_pri/pri[i];
long long x,y;
ext_gcd(pri[i],m,&x,&y);
//res=(res+y*m*a[i])%mul_pri;
res=(res+mul(mul(y,m,mul_pri),a[i],mul_pri))%mul_pri;
}
return ((res%mul_pri+mul_pri)%mul_pri);
}
int main()
{
int t;
long long n,m;
int k;
scanf("%d",&t);
while(t--)
{
scanf("%I64d%I64d%d",&n,&m,&k);
for(int i=; i<k; i++)
scanf("%I64d",pri+i);
for(int i=; i<k; i++)
{
a[i]=Lucas(n,m,pri[i]);
}
long long ans = chinese_reminder(a,pri,k);
printf("%I64d\n",ans);
}
return ;
}

hdu 5446 Unknown Treasure Lucas定理+中国剩余定理的更多相关文章

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

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

  2. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

  3. hdu 5446 Unknown Treasure lucas和CRT

    Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

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

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

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

  6. HDU 5446——Unknown Treasure——————【CRT+lucas+exgcd+快速乘+递推求逆元】

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

  7. 【bzoj3782】上学路线 dp+容斥原理+Lucas定理+中国剩余定理

    题目描述 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不能通过这些路口.小C喜欢走最短的路径到达目的 ...

  8. 卢卡斯定理&&中国剩余定理

    卢卡斯定理(模数较小,且是质数) 式子C(m,n)=C(m/p,n/p)*C(m%p,n%p)%p 至于证明(我也不会QAQ,只要记住公式也该就好了). 同时卢卡斯定理一般用于组合数取模上 1.首先当 ...

  9. hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. Linux中环境变量文件及配置(转载)

    一.环境变量文件介绍 转自:http://blog.csdn.net/cscmaker/article/details/7261921 Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登 ...

  2. ORACLE 远程导入导出数据库

      Oracle数据导入导出imp/exp就相当于oracle数据还原与备份.exp命令可以把数据从远程数据库服务器导出到本地的dmp文件,imp命令可以把dmp文件从本地导入到远处的数据库服务器中. ...

  3. SpringMVC java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name

    当跳转到一个含有form表单的页面的时候 如<form:form commandName="useCarInfo" 必须要new一个useCarInfo的同名实例给jsp来接 ...

  4. 如何获取Iframe的页面控件的值

    有时候我们在页面需要使用iframe的方法来引用另一个页面,虽然个人建议不要使用iframe哈,但是有些时候是没得法了才使用的咯,那么在使用的过程中也会遇到某一些问题,我将自己遇到的问题记录下来方便以 ...

  5. php简易灌水

    <?php $data = array ('content' => '白菜大侠','itemid'=>58); $data = http_build_query($data); $o ...

  6. mongodb启动后台服务

    将MongoDB部署在服务器机子上时mongodb的实例应为后台服务进行的方式运行,而非前台进程,否则远程会话一关闭mongodb也跟着关闭了.本文介绍mongodb后台服务进程开启和关闭的操作. 开 ...

  7. 模拟赛1029d1

    第二题[题目描述]给你两个日期,问这两个日期差了多少毫秒.[输入格式]两行,每行一个日期,日期格式保证为"YYYY-MM-DD hh:mm:ss"这种形式.第二个日期时间一定比第一 ...

  8. 2.3顺序容器-deque

    deque(双向队列) 1) *    :包含deque头文件 **  :deque也是一个可变长数组,适用于vector的操作都适用于deque ***:对比vector的优势在于在头部存取元素可以 ...

  9. JAVA中读取xls数据方法介绍

    用例编号(UI-0001) 用例名称({验证页面跳转|验证元素文本}-简要明确表述) 验证类型 是否执行 初始URL 初始元素xpath 目标元素xpath 目标元素属性 期望结果 UI-0001 验 ...

  10. php 审核管理

    权限管理界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...