Lucas定理的应用
2016-10-11 22:28
239 查看
//此处暂时只到笔者目前接触过的题
Lucas定理适用于long long范围内(无法打表,且单次求时间较短)大整数组合数求模,除数p为素数且不超过6位。使用时先写好板子,求C(n,m)时只需调用函数lucas(n,m)即可得到结果。
例题: xdu1032(裸题)
传送门:http://acm.xidian.edu.cn/problem.php?id=1032
以下为代码
Lucas定理适用于long long范围内(无法打表,且单次求时间较短)大整数组合数求模,除数p为素数且不超过6位。使用时先写好板子,求C(n,m)时只需调用函数lucas(n,m)即可得到结果。
例题: xdu1032(裸题)
传送门:http://acm.xidian.edu.cn/problem.php?id=1032
以下为代码
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int maxn=1000100; const int INF=(1<<29); const int p=10007; typedef long long ll; ll n,m; ll qpow(ll n,ll k) { ll res=1; for(;k;k>>=1) { if(k&1) res=(res%p)*(n%p)%p; n=(n%p)*(n%p)%p; } return res; } ll C(ll n,ll k) { if(n<k) return 0; ll res=1; for(int i=1; i<=k; i++) { ll a=(n-k+i)%p; ll b=i%p; res=res*(a*qpow(b,p-2)%p)%p; } return res%p; } ll lucas(ll n,ll k) { if(k==0) return 1; return (C(n%p,k%p)%p)*(lucas(n/p,k/p)%p)%p; } int main() { while(~scanf("%lld%lld",&n,&m)) { printf("%lld\n",lucas(m,n)); } return 0; }
相关文章推荐
- FZU 2020 组合(Lucas定理的应用)
- HDU 4349——Lucas定理的巧妙应用
- poj 3146 Interesting Yang Hui Triangle(lucas定理的应用)
- HDU 3037 Saving Beans(Lucas定理的直接应用)
- HDU 4349 Xiao Ming's Hope (Lucas定理的应用)
- Lucas定理及其应用
- HDU4349—Lucas定理的应用
- Lucas定理应用分析——大组合数取模
- HDU 3037 Saving Beans(Lucas定理的直接应用)
- 教你一波Lucas(卢卡斯)定理在数论解题中的应用
- HDU - 5446 Unknown Treasure (数论部分定理的综合应用,lucas+CRT)
- BZOJ 4403:浅谈Lucas定理应用及组合数建模
- Lucas定理在ACM中的应用
- HDU 3037 Saving Beans(Lucas定理的直接应用)
- Lucas定理C递归实现
- Lucas 定理
- hdu-3037-Saving Beans(Lucas定理+大组合数取模)
- [BZOJ2142]礼物(扩展Lucas定理+中国剩余定理)
- Ceizenpok’s formula Gym - 100633J 扩展Lucas定理 + 中国剩余定理
- BZOJ2142 礼物 [扩展lucas定理]