[卢卡斯定理+中国剩余定理] hdu 5446 Unknown Treasure
来源:互联网 发布:手机通话记录软件大全 编辑:程序博客网 时间:2024/06/06 01:51
题意:
给你N,M,K,求C(N,M)对K个素数的乘积取模的结果。
思路:
因为每个素数都不超过10^5,所以我们可以用lucas求出对于每个素数的答案
然后再用中国剩余定理求最后的答案。
这里要注意的就是中国剩余定理中会爆long long 所以需要用快速乘法。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<iostream>#include<stack>using namespace std;#define ll __int64ll power(ll a,ll b,ll mod){ ll ans=1; while(b) { if(b&1) ans=(ans*a)%mod; a=(a*a)%mod; b>>=1; } return ans%mod;}ll adder(ll a,ll b,ll mod){ ll ans=0; while(b) { if(b&1) ans=(ans+a)%mod; a=(a+a)%mod; b>>=1; } return ans%mod;}ll fc(ll n,ll m,ll mod){ if(m>n) return 0; ll t1,t2; t1=t2=1; for(ll i=n; i>m; i--) { t1=(t1*i)%mod; t2=(t2*(i-m))%mod; } return (t1*power(t2,mod-2,mod))%mod;}ll lucas(ll n,ll m,ll mod){ if(m==0) return 1; ll ans; ans=fc(n%mod,m%mod,mod); return (ans*lucas(n/mod,m/mod,mod))%mod;}ll exgcd(ll a,ll b,ll &x,ll &y){ if(b==0) { x=1; y=0; return a; } ll r=exgcd(b,a%b,x,y),t; t=x; x=y; y=t-a/b*y; return r;}ll Chinese_Remainder(ll n,ll p[],ll q[]) //中国剩余定理 p[]存放两两互质的数 q[]存放余数{ ll ans=0,mul=1; for(int i=0;i<n;i++) mul*=p[i]; for(int i=0;i<n;i++) { ll tep=mul/p[i],x,y,d; d=exgcd(p[i],tep,x,y); ll kx=adder(y,tep,mul); kx=adder(kx,q[i],mul); ans=(ans+kx)%mul; } return (ans%mul+mul)%mul;}int main(){ int t; cin>>t; while(t--) { ll n,m,k; scanf("%I64d%I64d%I64d",&n,&m,&k); ll p[12],q[12]; for(int i=0; i<k; i++) scanf("%I64d",&p[i]); for(int i=0; i<k; i++) q[i]=lucas(n,m,p[i]); printf("%I64d\n",Chinese_Remainder(k,p,q)); } return 0;}
0 0
- [卢卡斯定理+中国剩余定理] hdu 5446 Unknown Treasure
- HDU 5446 Unknown Treasure(Lucas定理+中国剩余定理)
- hdu 5446 Unknown Treasure(lucas定理+中国剩余定理)
- hdu 5446 Unknown Treasure(Lucas定理+中国剩余定理)
- HDU 5446 Unknown Treasure Lucas定理+中国剩余定理
- HDU 5446 Unknown Treasure(Lucas定理+中国剩余定理)
- hdu 5446 Unknown Treasure (Lucas定理+中国剩余定理)
- hdu 5446 Unknown Treasure(lucas+中国剩余定理)
- HDU 5446-Unknown Treasure(Lucas+中国剩余定理)
- HDU 5446 Unknown Treasure (Lucas + 中国剩余定理)
- HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
- hdu 5446 Unknown Treasure(lucas+中国剩余定理)
- Hdu 5446 Unknown Treasure(Lucas+中国剩余定理)
- HDU 5446 Unknown Treasure(lucas+中国剩余定理 / CRT)
- HDU 5446 Unknown Treasure 解题报告(Lucas定理 + 中国剩余定理)
- HDU --- 5446 Unknown Treasure 数论综合【Lucas定理 + 中国剩余定理 + 快速乘 + 思维】
- HDU 5446 Unknown Treasure(中国剩余定理+卢卡斯定理)——2015 ACM/ICPC Asia Regional Changchun Online
- HDU5446 Unknown Treasure Lucas+中国剩余定理
- java.util.ResourceBundle使用
- web.xml配置详解之filter
- HDU 1712 ACboy needs your help
- 在centOS 6.3下,进行hadoop 2.0.0-alpha(yarn)本地模式部署
- jQuery - 设置内容和属性
- [卢卡斯定理+中国剩余定理] hdu 5446 Unknown Treasure
- js打印对象 很有用的一个函数
- MySQL集群:主从数据库配置 实现查询负载
- 为什么很多年轻人总会感到迷茫
- MFC通过注册表 实现程序开机自动运行
- android 开发第六弹--View事件传递和碎片
- 使用Eclipse构建Maven的SpringMVC项目
- Java/Android 多少年月日时分秒前
- ubuntu 13.04安装supervisor