hdu 5446 Unknown Treasure lucas和CRT
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的更多相关文章
- HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k]) 0< n,m < 1018 思路:这题基本上算是模版题了 ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
- hdu 5446 Unknown Treasure Lucas定理+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...
- 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.. ...
- hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure(Lucas定理+CRT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...
- 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 ...
随机推荐
- eclipse不自动弹出提示(Alt+/ 快捷键失效)
转自:http://www.cnblogs.com/shaweng/archive/2013/09/26/3340016.html 主要有一下几种方法: 1.次方法用于没有一点提示的情况:依次打 ...
- struts采用JavaServlet/JSP技术,实现了基于Java EEWeb应用的MVC设计模式的应用框架
今天我用Ecipse搭建Struts框架,并且使用Struts框架编写一个最简单的例子,相信读者能够很容易的明白. Struts是当今Java比较流行的三大框架之一,三大框架是Struts,sprin ...
- spring + mybatis 注解式事务不回滚的原因分析 @Transactional
在一个项目中发现spring的事务无法回滚. DEBUG: org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.ses ...
- annotatedClasses和component-scan冲突吗
annotatedClasses:配置在sessionFactory下面表示的是,哪些实体需要映射,代码如下: <bean id="sessionFactory" class ...
- UVa 10622 (gcd 分解质因数) Perfect P-th Powers
题意: 对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值. 分析: 将x分解质因数,然后求所有指数的gcd即可. 对于负数还要再处理一下,负数求得的p必须是奇数才行. #inclu ...
- [POJ 2461] Billiard
同swustoj 11 Billiard Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1362 Accepted: 8 ...
- LeetCode Summary Ranges (统计有序数列范围)
题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...
- 装饰器模式(Decorator)
转自http://blog.csdn.net/hust_is_lcd/article/details/7884320 1.认识装饰器模式 装饰模式能够实现动态的为对象添加功能,是从一个对象外部来给对象 ...
- 【转】eclipse新建项目,报错“Error: workspace\appcompat_v7\res\values-v21\styles_base.xml No resource found that matches the given name”
原文网址:http://www.cnblogs.com/mbp-study/p/5268478.html 新建项目报错,不知道为什么,以前从未出现过的错误,把sdk更新之后,出现莫名错误,自己也是一知 ...
- SharePoint 2010 出错! HTTP Error 503. The service is unavailable
转:http://544729.blog.51cto.com/534729/464087 昨天,公司的sharepoint 2010 无法打开,提示HTTP Error 503. The servic ...