Tom and matrix HDU - 5226 (Lucas定理)

Tom and matrix

 HDU - 5226 

题意:就是求组合数~

第一道Lucas定理的题。

参考http://www.cnblogs.com/ykzou/p/4494969.html

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const int maxn=100010;
 5 ll fac[maxn];
 6 ll x1,x2;
 7 ll p;
 8 ll Y1,Y2;
 9 void init(){
10     fac[0]=1;
11     for(int i=1;i<maxn;i++) fac[i]=fac[i-1]*i%p;
12 }
13 
14 ll quickpow(ll a,ll b,ll p){
15     ll temp=a%p,res=1;
16     while(b){
17         if(b&1) res=(res*temp)%p;
18         b>>=1;
19         temp=(temp*temp)%p;
20     }
21     return res;
22 }
23 ll cal(ll n,ll m){
24     if(m>n) return 0;
25     return fac[n]*quickpow(fac[m]*fac[n-m],p-2,p)%p;
26 }
27 ll Lucas(ll n,ll m){
28     if(m==0) return 1;
29     return cal(n%p,m%p)*Lucas(n/p,m/p)%p;
30 }
31 
32 int main(){
33 
34     while(scanf("%lld%lld%lld%lld%lld",&x1,&Y1,&x2,&Y2,&p)!=EOF){
35         init();
36         ll ans=0;
37         for(ll i=Y1;i<=Y2;i++){
38             ans=(ans+Lucas(x2+1,i+1)-Lucas(x1,i+1)+p)%p;
39         }
40         printf("%lld\n",ans);
41     }
42 }
View Code

 

posted @ 2017-08-22 19:30  yijiull  阅读(226)  评论(0编辑  收藏  举报