标签:
C(n,k)有多种解法,1,dp递推;2,直接计算;3,lucas定理
lucas定理适合组合数取余数的计算,n和k的范围可到10^18
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 7 using namespace std; 8 9 const int maxn=1000100; 10 const int INF=(1<<29); 11 const int p=10007; 12 13 typedef unsigned long long ll; 14 15 ll n,m; 16 17 ll qpow(ll n,ll k) 18 { 19 ll res=1; 20 while(k){ 21 if(k&1) res=(res%p)*(n%p)%p; 22 n=(n%p)*(n%p)%p; 23 k>>=1; 24 } 25 return res; 26 } 27 28 ll C(ll n,ll k) 29 { 30 if(n<k) return 0; 31 ll res=1; 32 for(int i=1;i<=k;i++){ 33 ll a=(n-k+i)%p; 34 ll b=i%p; 35 res=res*(a*qpow(b,p-2)%p)%p; 36 } 37 return res%p; 38 } 39 40 ll lucas(ll n,ll k) 41 { 42 if(k==0) return 1; 43 return (C(n%p,k%p)%p)*(lucas(n/p,k/p)%p)%p; 44 } 45 46 int main() 47 { 48 while(cin>>n>>m){ 49 cout<<lucas(m,n)<<endl; 50 } 51 return 0; 52 }
标签:
原文地址:http://www.cnblogs.com/--560/p/4480969.html