BZOJ 2982: combination Lucas模板题

Code:

#include<bits/stdc++.h>
#define ll long long 
#define maxn 1000003 
using namespace std;
const ll mod = 10007; 
void setIO(string s)
{
	string in=s+".in"; 
	freopen(in.c_str(),"r",stdin); 
}
struct Comb
{
	ll fac[maxn]; 
	ll qpow(ll base,ll k)
	{
		ll tmp=1;
		while(k)
		{
			if(k&1)tmp=tmp*base%mod; 
			k>>=1; 
			base=base*base%mod; 
		}
		return tmp; 
	}
	void init()
	{
		int i;
		fac[1]=fac[0]=1; 
		for(i=2;i<maxn;++i) fac[i]=(fac[i-1]*i)%mod; 
	}
    ll getinv(ll a)
    {
    	return qpow(a,mod-2); 
    }
    ll C(ll n,ll m)
    {
    	if(m==0) return 1; 
    	if(n<m) return 0; 
    	return (fac[n]*getinv(fac[n-m]*fac[m]))%mod;   
    }
    ll lucas(ll n,ll m)
    {
    	if(m==0) return 1;
    	return (lucas(n/mod,m/mod)*C(n%mod,m%mod))%mod; 
    }
}t;  
int main()
{
	// setIO("input"); 
	int T,n,m; 
	scanf("%d",&T); 
	t.init(); 
	while(T--)
	{
		scanf("%d%d",&n,&m);
		if(n<m) swap(n,m); 
		printf("%lld\n",t.lucas(n,m)); 
	}
	return 0; 
}

  

上一篇:实验五{4}


下一篇:【省内训练2019-06-28】Trominoes