Unknown Treasure

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5446

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

HINT

题意

给你n,m,num

然后给你num个数,k1,k2....,knum

然后让你求C(n,m)%(k1*k2*....*knum)

题解:

首先,C(n,m)%k我们是会求的,大概这部分子问题是一个很经典的题目。

假设你会求了,那么我们就可以由此得到num个答案,是%k1,k2,k3....knum后得到的值

然后我们就可以看出就是类似韩信点兵一样的题,三个人一组剩了2个,五个人一组剩了2个这种……

这时候,就用中国剩余定理处理处理就好了

注意ll*ll会爆,所以得手写个快速乘法

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//************************************************************************************** void extend_gcd(ll a,ll &x,ll b,ll &y)
{
if(b==)
{
x=,y=;
return;
}
ll x1,y1;
extend_gcd(b,x1,a%b,y1);
x=y1;
y=x1-(a/b)*y1;
}
ll inv(ll a,ll m)
{
ll t1,t2;
extend_gcd(a,t1,m,t2);
return (t1%m+m)%m;
}
ll qpow(ll x,ll y,ll m)
{
if(!y)return ;
ll ans = qpow(x,y>>,m);
ans = ans*ans%m;
if(y&)ans = ans*x%m;
return ans;
} ll nump(ll x,ll p)
{
ll ans = ;
while(x)ans+=x/p,x/=p;
return ans;
}
ll fac(ll n,ll p,ll pk)
{
if(n==)return ;
ll ans = ;
for(ll i=;i<=pk;i++)
{
if(i%p==)continue;
ans = ans*i%pk;
}
ans = qpow(ans,n/pk,pk);
ll to = n%pk;
for(ll i =;i<=to;i++)
{
if(i%p==)continue;
ans = ans*i%pk;
}
return fac(n/p,p,pk)*ans%pk;
}
ll cal(ll n,ll m,ll p ,ll pi,ll pk)
{
ll a = fac(n,pi,pk),b=fac(m,pi,pk),c=fac(n-m,pi,pk);
ll d = nump(n,pi)-nump(m,pi)-nump(n-m,pi);
ll ans = a%pk * inv(b,pk)%pk * inv(c,pk)%pk*qpow(pi,d,pk)%pk;
return ans*(p/pk)%p*inv(p/pk,pk)%p;
}
ll mCnmodp(ll n,ll m,ll p)
{
ll ans = ;
ll x = p;
for(ll i =;i*i<=x&&x>;i++)
{
ll k=,pk=;
while(x%i==)
{
x/=i;
k++;
pk*=i;
}
if(k>)
ans=(ans+cal(n,m,p,i,pk))%p;
}
if(x>)ans=(ans+cal(n,m,p,x,x))%p;
return ans;
}
ll qtpow(ll x,ll y,ll M)
{
ll ret=0LL;
for(x%=M;y;y>>=1LL)
{
if(y&1LL)
{
ret+=x;
ret%=M;
if(ret<) ret+=M;
}
x+=x;
x%=M;
if(x<) x+=M;
}
return ret;
}
void solve(ll r[],ll s[],int t)
{
ll M=1LL,ans=0LL;
ll p[],q[],e[];
for(int i=;i<t;i++)
M*=r[i];
for(int i=;i<t;i++)
{
ll tmp=M/r[i],tt;
extend_gcd(tmp,p[i],r[i],q[i]);
p[i]%=M;
if(p[i]<) p[i]+=M;
e[i]=qtpow(tmp,p[i],M);
tt=qtpow(e[i],s[i],M);
ans=(ans+tt)%M;
if(ans<) ans+=M;
}
printf("%I64d\n",ans);
} ll CCC[],DDD[];
int main()
{
int t;
scanf("%d",&t);
int num = ;
ll n,m,p;
while(t--)
{
memset(CCC,,sizeof(CCC));
memset(DDD,,sizeof(DDD));
scanf("%I64d %I64d %d",&n,&m,&num);
for(int i=;i<num;i++)
{
scanf("%I64d",&CCC[i]);
DDD[i]=mCnmodp(n,m,CCC[i]);
}
solve(CCC,DDD,num);
}
return ;
}

hdu 5446 Unknown Treasure lucas和CRT的更多相关文章

  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定理+中国剩余定理

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

  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 卢卡斯+中国剩余定理

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

  7. HDU 5446 Unknown Treasure

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

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

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

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

随机推荐

  1. 函数lock_rec_get_nth_bit

    /*********************************************************************//** Gets the nth bit of a rec ...

  2. asp mvc 路由

    public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Workflo ...

  3. Access增删改查 (持续更新中)

    关于Access数据库(2003)的增删改查,其实和Sql大体差不多,但是还有很多不一样的地方.下面列几个容易犯的错误:  1.Access数据库的位置: conn = new OleDbConnec ...

  4. UVa 129 (回溯法) Krypton Factor

    回溯法确实不是很好理解掌握的,学习紫书的代码细细体会. #include <cstdio> ]; int n, L, cnt; int dfs(int cur) { if(cnt++ == ...

  5. [POJ 3420] Quad Tiling

      Quad Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3495   Accepted: 1539 Des ...

  6. STL sort()函数

    C++之所以得到这么多人的喜欢,是因为它既具有面向对象的概念,又保持了C语言高效的特点.STL 排序算法同样需要保持高效.因此,对于不同的需求,STL提供的不同的函数,不同的函数,实现的算法又不尽相同 ...

  7. 使用 Apache MINA2 实现 Web 系统的消息中间件

    本文将介绍如何使用 Apache MINA2(以下简称 MINA2)解决复杂 Web 系统内各子系统之间同步消息中间件的问题.MINA2 为开发高性能和高可用性的网络应用程序提供了非常便利的框架.从本 ...

  8. REST API TESTING

    在敏捷开发过程中 每隔两周就是一个sprint,,, 在上个sprint中,任务就是REST API TESTING 因为以前没做过API 测试,不懂,然后经过询问查找 终于知道,需要发送请求,然后获 ...

  9. 【树莓PI】下载机

    sudo app-get install ntfs-3g  读写ntfs格式的磁盘 mount -t ntfs /dev/sda4 /mnt/usb -o nls=utf8,umask=0 fdisk ...

  10. faplayer编译配置经验

    最近在做在线m3u8类格式的视频直播应用, 在获取m3u8的文件之后,如果采用Android系统播放器来播,会有各种各样的问题,容易卡死.不连续,也不能自定义一些选项.查找资料以后,决定采用fapla ...