hdu 4869 Turn the pokers (2014多校聯合第一場 I),hdupokers
Turn the pokers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1265 Accepted Submission(s): 465
Problem DescriptionDuring summer vacation,Alice stay at home for a long time, with nothing to do. She went out and bought m pokers, tending to play poker. But she hated the traditional gameplay. She wants to change. She puts these pokers face down, she decided to flip poker n times, and each time she can flip Xi pokers. She wanted to know how many the results does she get. Can you help her solve this problem?
http://acm.hdu.edu.cn/showproblem.php?pid=4869
思路:可惜了,比賽中沒有想出來,一比賽出來就想到了。
首先考慮兩次翻轉,翻轉a個和b個,不妨設a>b,現在考慮翻轉後正面朝上的個數(假設一開始都是背面朝上,下面用0表示背面朝上,1表示正面朝上)。翻轉後,1的個數最大可能為a+b,最小為a-b。進一步觀察可發現經過兩次翻轉,1的個數可取的值為最小值為a-b,最大值為a+b,且間隔為2的一個區間,也就是a-b,a-b+2,a-b+4......a+b-2,a+b。那麼現在如果再加一個數C,同理可得到1的個數會是一個區間,那麼我們只要維護這個區間即可。注意會有這樣的情況,比如a+b超過n或a-b小於0,這個時候就要討論算出新的區間,這個仔細分情況討論就行。最後算出1的個數的區間[L,R]後,最後的答案就為C(n,L)+C(n,L+2)+C(n,L+4)+......C(n,R),最後要模數1e9+7。(C(n,m)為n個數中取m個的組合數)因為這裡的n比較大,所以不能直接按公式C[n][m]=C[n-1][m]+C[n-1][m-1]來做,因為C(n,m)=n!/(m!*(n-m)!),那麼預先處理出n!和n!關於1e9+7的逆元,再根據公式即可得到答案。
#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>#define ll long long#define maxn 100010#define mod 1000000009using namespace std;ll pow(ll x,ll y){ if(y==0) return 1; ll tmp=pow(x,y/2); tmp=(tmp*tmp)%mod; if(y&1) tmp=tmp*x%mod; return tmp;}ll c[maxn],b[maxn];void init(){ c[0]=1; for(int i=1;i<=100000;i++) c[i]=(c[i-1]*i)%mod; for(int i=0;i<=100000;i++) b[i]=pow(c[i],mod-2);}ll getC(int n,int m){ if(m==0||m==n) return 1; ll tmp=c[n]*b[m]%mod*b[n-m]%mod; return tmp;}int main(){ freopen("dd.txt","r",stdin); init(); int n,m; while(scanf("%d%d",&n,&m)!=EOF) { int x; int l=0,r=0; for(int i=1;i<=n;i++) { scanf("%d",&x); int rr=r; if(r+x<=m) r+=x; else { if(l+x<=m) { if((m-l-x)%2) r=m-1; else r=m; } else { r=m-(l+x-m); } } if(l-x>=0) l-=x; else { if(rr>=x) { if((rr-x)%2) l=1; else l=0; } else l=x-rr; } } ll ans=0; for(int i=l;i<=r;i+=2) { ans=(ans+getC(m,i))%mod; } printf("%I64d\n",ans); } return 0;}