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. WireShark网络性能分析

    最近生产上出现一个性能问题,表现为:行情延时5s左右.从log一路追查下去,发现是我们自己写的一个行情网关(部署在xx.xx.xx.132)<->第三方的中转网关(部署在xx.xx.xx. ...

  2. Linux快捷键和别名

    一.设置别名 1使用命令行     alias 别名='命令'(只对本次登陆生效) 2.使用配置文件设置别名(永久生效) vi /root/.bashrc        打开系统别名配置文件,一般是用 ...

  3. ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务解决

    先看oracle的监听和oracle的服务是否都启动了. 启动oracle监听:cmd命令行窗口下,输入lsnrctl start,回车即启动监听. 查看oracle的sid叫什么,比如创建数据库的时 ...

  4. CMD和AMD区别的概括

    CMD和AMD区别   AMD CMD 关于依赖的模块 提前执行(不过 RequireJS 从 2.0 开始,也改成可以延迟执行(根据写法不同,处理方式不同)), 延迟执行 关于依赖的位置 依赖前置 ...

  5. Fragment的使用(一)

    本篇博客主要讲的是如何使用Fragment. 使用Fragment的步骤类似于自定义View的步骤: 定义Fragment的布局文件 实现扩展Fragment的子类 在扩展子类的onCreateVie ...

  6. 802.11MAC基础

    做无线网络测试已经大半年了,在这过程中发现<802.11权威指南>真是以本好书,在这里分享一下学习到的知识,也帮助我记忆. 1.MAC: mac(媒介访问控制层),它位于物理层之上,控制着 ...

  7. spring 源码

    spring AOP的Advice(通知) Advice(通知)定义在连接点做什么,为切面增强提供织入接口. BeforeAdvice AfterAdvice ThrowsAdvice的设计,体现了A ...

  8. C语言程序设计第十次作业

    一.实验内容        1.有5名学生,每名学生有语文.数学和外语3门课的考试成绩.编程统计各学生的总分和平均分以及所有学生各科的平均分.要求成绩在程序中初始化,结果以表格的形式输出.      ...

  9. string和char*的相互转换

    原文地址: 点击打开链接

  10. python基础之内置函数

    该博客内容参考http://www.cnblogs.com/wupeiqi/articles/4943406.html 内置函数 一 详细见python文档,猛击这里 文件操作 操作文件时,一般需要经 ...