您的位置:首页 > 其它

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

以下为代码

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息