卢卡斯定理(Lucas)

来源:互联网 发布:电池测试系统算法软件 编辑:程序博客网 时间:2024/06/03 14:48

Lucas

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3037

题目大意:求C(n,m)%p;

题目思路:卢卡斯定理

#include <iostream>#include <cstdio>using namespace std;typedef long long ll;ll a[100000+10];ll quick_mod(int a,int b,int p){    ll base=a,ans=1;    while(b)    {        if(b&1)            ans=(ans*base)%p;        base=(base*base)%p;        b>>=1;    }    return ans;}void inti(int p){    a[0]=1;    for(int i=1;i<=p;i++)        a[i]=a[i-1]*i%p;}ll lucas(ll n,ll m,ll p){    ll re=1;    while(n&&m)    {        ll x=n%p,y=m%p;        if(x<y)            return 0;        re=re*a[x]*quick_mod(a[y]*a[x-y]%p,p-2,p)%p;        n/=p;        m/=p;    }    return re;}int main(){    int t;    //cin>>t;    scanf("%d",&t);    while(t--)    {        ll n,m,p;        //cin>>n>>m>>p;        scanf("%lld%lld%lld",&n,&m,&p);        inti(p);        //cout<<lucas(n+m,m,p)<<endl;        printf("%lld\n",lucas(n+m,m,p));    }    return 0;}
原创粉丝点击