复制代码

板子 Lucas

大数理论 入门

Luacs(n,m,p) = C(n%p,m%p)*Lucas(n/p,m/p,p);

C(n,m,p) = n!/(n-m)! ^p-2 modp

#include<bits/stdc++.h>
using namespace std;
#define LOACL  freopen("in","r",stdin);\
         freopen("out","w",stdout); 
#define FASTIO  ios::sync_with_stdio(false);
#define CLOCK cout<<1.*clock()/CLOCKS_PER_SEC<<"ms"<<"\n";


const int   inf = 987654321;
const int    sz = (int)1e6 + 5;
const int   mod = (int)1e9 + 7;
const int sqrtn = 300; 

#define add(u,v,w) e[++tot]=(edge){v,head[u],w},head[u]=tot; 
#define CLR(arr,val) memset(arr,val,sizeof(arr)) 
#define DBG(x) cout<<(#x)<<"="<<x<<endl
#define DBG2(x,y) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<endl
#define DBG3(x,y,z) cout<<(#x)<<"="<<x<<"\t"<<(#y)<<"="<<y<<"\t"<<(#z)<<"="<<z<<endl
 

#define FOR(i, a, b)  for(int i=(a); i<(b); i++)
#define REP(i, a, b)  for(int i=(a); i<=(b); i++)
#define DOWN(i, a, b) for(int i=(a); i>=(b); i--)
#define Loop(i,u) for(int i =head[u];i;i=e[i].nxt)

#define all(x) x.begin(),x.end()
#define low(x) (x)&(-x)
#define pb push_back
typedef long long ll; 
typedef double dl; 
ll a[sz];
int p;
ll pow(ll y,int z,int p)
{
    y%=p; ll ans=1;
    for(int i=z;i;i>>=1,y=y*y%p)
        if(i&1) ans=ans*y%p;
    return ans;
}
ll C(ll n,ll m )
{
    if(m>n) return 0;
    return ((a[n]*pow(a[m],p-2,p))%p*pow(a[n-m],p-2,p)%p);
}
ll Lucas(int n,int m)
{
    if(!m) return 1;
    return C(n%p,m%p)*Lucas(n/p,m/p)%p;
}
int main()
{
    LOACL
    int t,n,m ;    
    
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>p;
        a[0]=1;
           REP(i,1,p) a[i]=a[i-1]*i%p;
        cout<<Lucas(n+m,n)<<endl;
    }
}
View Code

 

posted @ 2018-05-25 23:59  pg633  阅读(152)  评论(0编辑  收藏  举报