题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869
题意:有m张扑克牌,现在可以翻n次,每一次要翻Xi张牌,一开始全部的牌都是背面,问经过n次操作后,这m张牌的不同状态一共有几种。
思路:我们维护每次翻转可以得到的最大正面牌数R和最小正面牌数L,维护n次后,答案为∑C(m,i)( i = L , L+2 , L+4,.....R ),为什么只能取到同奇偶性的数呢。因为每一次翻牌要么把正变反,要么反变正,若一开始有x张正的,则会变为x+1或者x-1,所以可取的方案是同奇偶的。假设当前可以翻转x次,现在维护L和R。R的维护分三种情况:①R+x<=m 全部都翻背面的牌 ②L+x<=m && R+x>m 可以通过选择当前正面的牌使得翻过x张牌后正面的牌达到最大。 ③背面牌的最大值都比x要小,所以超出的部分就要通过翻正面的牌来补。维护L同理。组合数的处理就用逆元计算。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 1000000009
const int maxn = 100009;
int n,m;
int X[maxn];
LL f[maxn];
void init()
{
f[0] = 1;
rep(i,1,100000)
f[i] = f[i-1] * i % mod;
}
LL multi( LL a , LL b )
{
LL ans = 1;
while( b )
{
if ( b & 1 ) ans = ( ans * a ) % mod;
a = ( a * a ) % mod;
b >>= 1;
}
return ans;
}
LL cal( int n , int m )
{
return ( f[n] * multi( f[m] , mod - 2) ) % mod * multi( f[n-m] , mod - 2 ) % mod;
}
int cal1( int L , int R , int choose )
{
if ( L >= choose ) return L - choose;
if ( R >= choose ) return (R-choose)%2;
return choose - R;
}
int cal2( int L ,int R , int choose )
{
int ret = m - R;
if ( choose <= ret ) return R+choose;
if ( choose <= m - L ) return choose + L + (m-choose-L)/2*2;
return 2*m-L-choose;
}
int main()
{
init();
while(scanf("%d%d",&n,&m)==2)
{
rep(i,1,n) scanf("%d",&X[i]);
int L,R;
L = R = 0;
rep(i,1,n)
{
int tl = cal1(L,R,X[i]);
int tr = cal2(L,R,X[i]);
L = tl;
R = tr;
}
LL ans = 0;
for( int i = L; i <= R; i+=2 )
{
ans = ( ans + cal( m , i ) ) % mod;
}
printf("%I64d\n",ans);
}
return 0;
}