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. Java for LeetCode 222 Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  2. window.location.href url含中文服务器收到乱码问题解决

    中文乱码问题 window.location.href url含中文服务器收到乱码问题解决 (1).页面中先对中文进行编码. 如:window.location.href = url+"&a ...

  3. C#导出Excel动态列

    一.用StreamWrite流对象,导出Excel 1. string _sPath = GenerateSalaryMonthlyReport(dgvSalarySum); System.Diagn ...

  4. 非Unicode工程读取Unicode文件

    MyUnicodeReader.h #pragma once /******************************************************************** ...

  5. nodejs链接mongodb数据库

    nodeJs链接mongodb数据库有两种方式,第一种是利用官方自己开发的npm包mongodb链接,第二种是利用第三方npm包mongoose链接:这里如果是window操作系统,建议用mongoo ...

  6. Stanford大学机器学习公开课(三):局部加权回归、最小二乘的概率解释、逻辑回归、感知器算法

    (一)局部加权回归 通常情况下的线性拟合不能很好地预测所有的值,因为它容易导致欠拟合(under fitting).如下图的左图.而多项式拟合能拟合所有数据,但是在预测新样本的时候又会变得很糟糕,因为 ...

  7. 整理 iOS 9 适配中出现的坑(图文)(转)

    作者:董铂然 本文主要是说一些iOS9适配中出现的坑,如果只是要单纯的了解iOS9新特性可以看瞄神的开发者所需要知道的 iOS 9 SDK 新特性.9月17日凌晨,苹果给用户推送了iOS9正式版,随着 ...

  8. execl一个工作薄中有几个个工作表,将这几个个工作表分别保存到不同execl文件中

    用宏运行: Sub QEJebel()    Dim sh As Worksheet    Dim Pa As String    Pa = ThisWorkbook.Path    For Each ...

  9. PHP面试题集

    汗~~做了一下网络上的php题目,不知不觉做到现在.....把答案贴出来如果有问题请欢迎补充和指正 1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分)   $a = da ...

  10. 【Jquery】【控件】flexigrid 自定义查询

    最近用flexigrid作报表,需要自定义条件进行查询,界面如下: 翻了半天文档也没找到如何用POST方法自行传递参数进行查询. 找了一个台湾人写的博客,却要改flexigrid的源代码,更不靠谱. ...