HDU4869:Turn the pokers(快速幂求逆元+组合数)

题意:

给出n次翻转和m张牌,牌相同且一开始背面向上,输入n个数xi,表示xi张牌翻转,问最后得到的牌的情况的总数。

思路:

首先我们可以假设一开始牌背面状态为0,正面则为1,最后即是求ΣC(m,k),k为所有能取到1的情况。首先我们要确认最后1的奇偶性。因为一次翻转0->1,或者1->0,则最后所有1的情况的奇偶性相同。然后我们要找到最小的1的个数i和最大的1的个数j,i为能翻1则翻1,j为能翻0则翻0,介于中间的情况是取偶数步数,一半翻1,一半翻0,保持1的个数不变。那么k为(i<=k<=j&&(k-i)&1==0)。

然后求组合数会涉及到一个求逆元的问题,可以采用快速幂或者打表,求逆元的方法我会最近补上。

-END-

 1 #include<cstdio> 
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 #define LL __int64
 6 const int maxn=1e5+10;
 7 const int MOD =1e9+9;
 8 LL c[maxn];//记录C(n,i)
 9 LL pow_mod(LL n,LL p)//快速幂求逆元 
10 {
11     LL s=1;
12     for(int i=p-2;i;i>>=1,n=n*n%MOD)
13     {
14         if(i&1) s=s*n%MOD; 
15     }
16     return s;
17 } 
18 
19 int main()
20 {
21     int n,m;
22     while(scanf("%d%d",&n,&m)==2)
23     {
24         int i,j,k,x,p,q;
25         i=j=0;
26         for(k=0;k<n;k++)
27         {
28             scanf("%d",&x);
29             //求最小1的个数
30             if(i>=x) p=i-x;
31             else if(j>=x) p=((i&1)==(x&1)?0:1);
32             else p=x-j;
33             //求最大1的个数
34             if(j+x<=m) q=j+x;
35             else if(i+x<=m) q=(((i+x)&1)==(m&1)?m:m-1);
36             else q=2*m-(i+x);
37             i=p;j=q; 
38         }
39         LL ans=0;
40         c[0]=1;
41         if(i==0) ans+=c[0];
42         for(k=1;k<=j;k++)
43         {
44             if(m-k<k) c[k]=c[m-k];
45             else c[k]=c[k-1]*(m-k+1)%MOD*pow_mod(k,MOD)%MOD;
46             if(k>=i&&(k&1)==(i&1)) ans+=c[k];
47         }
48         printf("%I64d\n",ans%MOD);
49     }
50 } 
View Code

相关资料:

http://blog.csdn.net/a601025382s/article/details/38047129

http://syncshinee.github.io/2015/05/10/InverseElement/#u65B9_u6CD52-_u901A_u8FC7_u5FEB_u901F_u5E42_u6C42_u9006_u5143

posted @ 2016-07-13 21:05  遗风忘语  阅读(462)  评论(0编辑  收藏  举报