/* *********************************************** Author :guanjun Created Time :2016/5/20 0:28:36 File Name :hdu5698.cpp ************************************************ */ #include <iostream> #include <cstdlib> #include <stdio.h> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #define ll long long #define MOD 1000000007 #define INF 0x3f3f3f3f #define maxn 200010 using namespace std; ll fac[maxn],inv[maxn]; ll Pow(ll a,ll n,ll m){ ll ans=1; while(n) { if(n&1) { ans*=a; ans%=m; } a=a*a%m; n>>=1; } return ans; } ll C(ll n,ll m,ll p){ ll res=1; while(n&&m){ ll a=n%p; ll b=m%p; if(a<b)return 0; res=res*fac[a]%p*Pow(fac[b]*fac[a-b]%p,p-2,p)%p; n/=p; m/=p; } return res; } void init(){ fac[0]=1; inv[0]=1; for(int i=1;i<=maxn;i++){ fac[i]=fac[i-1]*i%MOD; inv[i]=Pow(fac[i],MOD-2,MOD);//费马小定理,要求MOD为素数 } } int main() { init(); ll n,m; while(cin>>n>>m){ printf("%I64d ",C(n,m,MOD)); } return 0; }
解析:http://blog.sina.com.cn/s/blog_b9a401a40101dghn.html