Unknown Treasure

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

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
题意:求c(n,m)%(p1*p2*...*pk);
思路:求出每个c(n,m)%p1=a1......求出a数组;
   然后根据a求中国剩余即是答案;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
ll p[],a[];
ll n,m;
ll mulmod(ll x,ll y,ll m)
{
ll ans=;
while(y)
{
if(y%)
{
ans+=x;
ans%=m;
}
x+=x;
x%=m;
y/=;
}
ans=(ans+m)%m;
return ans;
}
ll ff(ll x,ll p)
{
ll ans=;
for(int i=;i<=x;i++)
ans*=i,ans%=p;
return ans;
}
ll pow_mod(ll a, ll x, ll p) {
ll ret = ;
while (x) {
if (x & ) ret = ret * a % p;
a = a * a % p;
x >>= ;
}
return ret;
} ll Lucas(ll n, ll k, ll p) { //C (n, k) % p
ll ret = ;
while (n && k) {
ll nn = n % p, kk = k % p;
if (nn < kk) return ; //inv (f[kk]) = f[kk] ^ (p - 2) % p
ret = ret * ff(nn,p) * pow_mod (ff(kk,p) * ff(nn-kk,p) % p, p - , p) % p;
n /= p, k /= p;
}
return ret;
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
exgcd(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
ll CRT(ll a[],ll m[],ll n)
{
ll M = ;
ll ans = ;
for(ll i=; i<=n; i++)
M *= m[i];
for(ll i=; i<=n; i++)
{
ll x, y;
ll Mi = M / m[i];
exgcd(Mi, m[i], x, y);
//ans = (ans + Mi * x * a[i]) % M;
ans = (ans +mulmod( mulmod( x , Mi ,M ), a[i] , M ) ) % M;
}
ans=(ans + M )% M;
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int k;
scanf("%lld%lld%d",&n,&m,&k);
for(int i=;i<=k;i++)
scanf("%lld",&p[i]);
for(int i=;i<=k;i++)
a[i]=Lucas(n,m,p[i]);
printf("%lld\n",CRT(a,p,k));
}
return ;
}

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

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

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

  2. HDU 5446 Unknown Treasure (卢卡斯+CRT

    代码: #include"bits/stdc++.h" #define db double #define ll long long #define vec vector<l ...

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

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

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

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

  6. hdu 5446 Unknown Treasure Lucas定理+中国剩余定理

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

  7. hdu 5446 Unknown Treasure lucas和CRT

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

  8. HDU 5446 Unknown Treasure

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

  9. HDU 5446 Unknown Treasure(Lucas定理+CRT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...

随机推荐

  1. Jquery的命名冲突

    $是Jquery的别名,为了编码方便,我们可以使用$符号来调用Jquery的函数.然而,当我们引入多个JS库的时候,如果另外一个库中也引用了$符号作为别名的话,那么我们在使用$符号的时候,由于同一个作 ...

  2. C#网络编程之--TCP协议(一)

    TCP 是面向连接的传输协议 面向连接,其实就好比,A打电话给B,如果B接听了,那么A和B之间就的通话,就是面向连接的 TCP 是全双工的传输协议 全双工,这个理解起来也很简单,A打电话给B,B接听电 ...

  3. 利用vs2013简单初探Python

    最近无聊,某天无意在vs2013上发现了Python...... Python介绍:可以自己搜索一下. 接下来,准备工具: Win7搭建开发环境.需要准备Python.PTVS2013. 1.http ...

  4. Unit01-OOP-对象和类(上)

    Unit01-OOP-对象和类(上) 1.什么是类?什么是对象?  1)现实生活是由很多很多对象组成的    基于对象抽出了类  2)对象:真实存在的单个的个体    类:类型.类别,代表一类个体  ...

  5. java netty之ServerBootstrap的启动

    通过前面的几篇文章,对整个netty部分的架构已经运行原理都有了一定的了解,那么这篇文章来分析一个经常用到的类:ServerBootstrap,一般对于服务器端的编程它用到的都还算是比较的多..看一看 ...

  6. 转:"在已损坏了程序内部状态的XXX.exe 中发生了缓冲区溢出"的一种可能原因

    我的问题跟原作者的问题差不多.头文件和DLL不匹配导致的. 原文链接:http://blog.csdn.net/u012494876/article/details/39030887 今天软件突然出现 ...

  7. iOS,Xcod7/8,iOS使用修改点

    1.Xcod7使用修改点 2.Xcode8使用修改点 Xcod7使用修改点 1.xcode7 新建的项目,Foundation下默认所有http请求都被改为https请求. HTTP+SSL/TLS+ ...

  8. 计划将项目中使用entity framework的要点记录到改栏目下

    ef监控sql执行性能日志.http://www.cnblogs.com/CreateMyself/p/5277681.html http://123.122.205.38/cn_sql_server ...

  9. java学习:Hibernate入门

    相对微软的linq-to-sql或EF框架而言,"Hibernate对于eclipse的集成开发“ 新手并不容易掌握,下面是新手上路的步骤: 一.准备工作: 1.先下载eclipse (官网 ...

  10. Latex 分段函数

    Latex里面分段函数的输入: \begin{equation}     P_{r-j}=    \begin{cases}    0&\mbox{if $r-j$ is odd}\\     ...