Unknown Treasure

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

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*p3..pk
题解:中国剩余定理+lucas
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll __int64
#define mod 10000000007
using namespace std;
ll n,m,k;
int t;
ll exm;
ll f[];
void init(int p) { //f[n] = n!
f[] = ;
for (int i=; i<=p; ++i) f[i] = f[i-] * i % p;
}
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;
} 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 pow_mod(ll a, ll x, int p) {
ll ret = ;
while (x) {
if (x & ) ret = ret * a % p;
a = a * a % p;
x >>= ;
}
return ret;
}
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 +mulmod( mulmod( x , Mi ,M ), a[i] , M ) ) % M;
}
ans=(ans + M )% M;
return ans;
} ll Lucas(ll n, ll k, ll p) { //C (n, k) % p
init(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 * f[nn] * pow_mod (f[kk] * f[nn-kk] % p, p - , p) % p;
n /= p, k /= p;
}
return ret;
}
int main ()
{
scanf("%d",&t);
{
for(int i=;i<=t;i++)
{
ll ee[];
ll gg[];
scanf("%I64d %I64d %I64d",&n,&m,&k);
for(ll j=;j<k;j++)
{
scanf("%I64d",&exm);
gg[j]=exm;;
ee[j]=Lucas(n,m,exm);
}
printf("%I64d\n",CRT(ee,gg,k));
}
}
return ;
}
 

HDU 5446 中国剩余定理+lucas的更多相关文章

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

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

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

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

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

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

  4. hdu 5446 Unknown Treasure 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

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

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

  6. hdu 5446 Unknown Treasure lucas和CRT

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

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

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

  8. 【bzoj1951】: [Sdoi2010]古代猪文 数论-中国剩余定理-Lucas定理

    [bzoj1951]: [Sdoi2010]古代猪文 因为999911659是个素数 欧拉定理得 然后指数上中国剩余定理 然后分别lucas定理就好了 注意G==P的时候的特判 /* http://w ...

  9. CRT中国剩余定理 & Lucas卢卡斯定理

    数论_CRT(中国剩余定理)& Lucas (卢卡斯定理) 前言 又是一脸懵逼的一天. 正文 按照道理来说,我们应该先做一个介绍. 中国剩余定理 中国剩余定理,Chinese Remainde ...

随机推荐

  1. Visual Basic 2012 借助DataGridView控件将SQL server2012 数据导入到Excel 2010

    摘  要: SQL Server 2012 数据和Excel 2010之间的连接和数据的传输,本篇文章主要针对的是SQL Server 2012 数据导入到Excel 2010文件中.Excel软件对 ...

  2. Android Binder

    http://blog.csdn.net/luoshengyang/article/details/6618363   Android进程间通信(IPC)机制Binder简要介绍和学习计划

  3. VS2013 有效密钥

    今天打开笔记本上的VS2013,发现试用版到期了,就到网上找密钥,找了一些时候找到一个有效序列号,记录如下: BWG7X-J98B3-W34RT-33B3R-JVYW9

  4. 译:Boost Property Maps

    传送门:Boost Graph Library 快速入门 原文:Boost Property Map 图的抽象数学性质与它们被用来解决具体问题之间的主要联系就是被附加在图的顶点和边上的属性(prope ...

  5. DataTable转Json字符串(使用Newtonsoft.Json.dll)

    DataTable转Json字符串(使用Newtonsoft.Json.dll) 在需要把DataTable转为Json字符串时,自己手动拼接太麻烦,而且容易出错,费时费力,使用Newtonsoft. ...

  6. 安全协议系列(二)----CCM与CCMP

    CCMP(CTR with CBC-MAC Protocol) 是 IEEE 802.11i 中推出使用基于 AES 的 CCM 模式的安全加密协议.与原来脆弱的 WEP 算法及临时补救措施 TKIP ...

  7. ad bga扇出 和群组布线

    本文关于如何快速规范的bga布线和扇出做笔记 目的:layout一个ili的3+1的控制板.把线距控制在4mil 这样可以节约制造成本. 问题:需要大改布局.尤其是bga扇出和通道连接的问题. 细节: ...

  8. ionic 使用

    1. 编译时目录下不能有中文文件的名称,否则会报一个资源错误 ,返回aapt.exe'' finished with non-zero exit value 1. 2. 编译完成后在手机上无法访问网络 ...

  9. JAVA Arrays.binarySearch

    转自:http://blog.csdn.net/somebodydie/article/details/8229343 package com.jaky; import java.util.*; pu ...

  10. [解决方案] pythonchallenge level 1

    http://www.pythonchallenge.com/pc/def/map.html g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amk ...