3 4 3 2 3 3 3 3 2 3
8 3HintFor the second example: 0 express face down,1 express face up Initial state 000 The first result:000->111->001->110 The second result:000->111->100->011 The third result:000->111->010->101 So, there are three kinds of results(110,011,101)
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
typedef long long ll;
const ll mod=1e9+9;
ll base[maxn],ans,tp;
void gcd(ll a,ll b,ll& d,ll& x,ll& y)//扩展的欧几里德
{
if(!b)
d=a,x=1,y=0;
else
gcd(b,a%b,d,y,x),y-=x*(a/b);
}
ll inv(ll a,ll n)//求逆元
{
ll x,y,d;
gcd(a,n,d,x,y);
return d==1?(x+n)%n:-1;
}
int main()
{
int n,m,i,x,miv,mav,nmi,nma;
for(i=base[0]=1;i<=1e5;i++)
base[i]=(base[i-1]*i)%mod;
while(~scanf("%d%d",&n,&m))
{
scanf("%d",&x);
ans=0,miv=mav=x;
for(i=1;i<n;i++)
{
scanf("%d",&x);
nmi=miv,nma=mav;
if(x<=miv)
nmi-=x;
else if(x<=mav)
nmi=(mav&1)^(x&1);
else
nmi=x-mav;
if(x<=m-mav)
nma+=x;
else if(x<=m-miv)
nma=m-((mav&1)^(x&1)^(m&1));
else
nma=2*m-x-miv;
mav=nma,miv=nmi;
}
for(i=miv;i<=mav;i+=2)
{
tp=(base[m-i]*base[i])%mod;
tp=inv(tp,mod);
ans+=(base[m]*tp)%mod;
ans%=mod;
}
printf("%I64d\n",ans);
}
return 0;
}
hdu 4869 Turn the pokers(递推&组合数学&逆元),布布扣,bubuko.com
hdu 4869 Turn the pokers(递推&组合数学&逆元)
原文地址:http://blog.csdn.net/bossup/article/details/38277449