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. curl post

    //Post方式实现 $url = "http://localhost/web_services.php"; $post_data = array ("username& ...

  2. eclipse 导入工程报错Unable to execute dex: Multiple dex files define Landroid/annotation/SuppressLint

    对策: 检查libs 是否有重复加载的.

  3. 初学者的python学习笔记1——作业篇

    既然是学习,作业必不可少,其实在看后面讲思路之前还是感觉自己写的不错,但是和后面一对比,感觉实在是想的太片面太肤浅了,还需要太多太多改进的地方. 首先放一下作业要求. 最开始做的时候真的是完全按照字面 ...

  4. Ext JS 4 老特征:statics 静态的变量和方法

    l   一.静态的变量和静态的方法 [译文原创Learning Ext JS 4 第51页] Statics的方法只属于类本身,而不属于任何一个实例,这就决定了我们是在定义类的内部来用statics方 ...

  5. AFN断点续传思路

  6. css样式多个类、标签用【逗号 空格 冒号 点】分开的解析

    一:#a,b{…………}  id叫a和一个标签是b的样式(平行关系) 二:#a b{…………}  id叫a下面的一个标签是b的样式(包含关系) 三:#a.b{…………}  id叫a的下面的class叫 ...

  7. MATLAB中白噪声的产生

    rand产生的是[0,1]上的均匀分布的随机序列randn产生均值为0,方差为1的高斯随机序列,也就是白噪声序列 rand产生的是均匀分布白噪声序列randn产生的是正态分布的白噪声序列 MATLAB ...

  8. iOS学习之git的使用

    SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器.集中式版本控 ...

  9. 如何在tomcat中如何部署java EE项目

    如何在tomcat中如何部署java EE项目 1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法.2.在tomcat安 ...

  10. iShare.js分享插件

    iShare.js是一个小巧的分享插件,纯JS编写,不依赖任何第三方库,使用简便. 为啥写这个插件? 因为在搭建个人blog时(还没有搭建好(¯﹃¯)),对目前国内比较受欢迎的分享插件都不太满意,主要 ...