Lucas定理的应用

来源:互联网 发布:数据库系统哪个好 编辑:程序博客网 时间:2024/05/04 11:04

//此处暂时只到笔者目前接触过的题

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;}




0 0
原创粉丝点击