[模板] 卢卡斯定理
2018-03-29 09:00
274 查看
题目描述:
求Cmn+m% pCn+mm% p保证p为质数
题目分析:
卢卡斯定理Cmn% p=Cm%pn%p∗Cm/pn/p%pCnm% p=Cn%pm%p∗Cn/pm/p%p
题目链接:
Luogu 3807Ac 代码:
// luogu-judger-enable-o2 #include <cstdio> #include <iostream> #include <cstring> #define ll long long ll num[210000]; ll mod; ll fastpow(ll x,ll y) { ll ans=1; x%=mod; for(;y;y>>=1,x=(x*x)%mod) if(y&1) ans=(ans*x)%mod; return ans; } ll C(ll n,ll m) { if(n<m) return 0ll; ll up=1ll,down=1ll; for(ll i=n-m+1;i<=n;i++) (up*=i)%=mod; for(ll i=2;i<=m;i++) (down*=i)%=mod; return up*fastpow(down,mod-2)%mod; } ll Lucas(ll n,ll m) { ll ans=1; while(n&&m) { ans=(ans*C(n%mod,m%mod))%mod; n/=mod,m/=mod; } return ans; } int main() { int T; scanf("%d",&T); while(T--) { ll n,m; scanf("%lld%lld%lld",&n,&m,&mod); printf("%lld\n",Lucas(n+m,m)); } return 0; }
相关文章推荐
- 洛谷P3807 【模板】卢卡斯定理exgcd
- 卢卡斯定理模板(当n和m很大,p为素数时)
- 洛谷 P3807 【模板】卢卡斯定理
- 【Luogu3807】【模板】卢卡斯定理(数论)
- luogu P3807 【模板】卢卡斯定理
- [Luogu 3807]【模板】卢卡斯定理
- 洛谷.3807.[模板]卢卡斯定理(Lucas)
- [模板]-卢卡斯(Lucas)定理
- 组合数模板+ 卢卡斯定理
- 组合数取模(卢卡斯定理、模板)
- 【模板】卢卡斯定理
- 【洛谷】3807 【模板】卢卡斯定理
- 洛谷—— P3807 【模板】卢卡斯定理
- 卢卡斯定理的模板以及应用
- HDU3037Saving Beans(lucas定理模板)
- 中国剩余定理模板
- [卢卡斯定理+中国剩余定理] hdu 5446 Unknown Treasure
- bzoj 4403 序列统计 卢卡斯定理
- 【中国剩余定理模板】
- Lucas定理模板【bzoj2982】【combination】