首页 > 代码库 > HDU 4869 Turn the pokers(思维+逆元)

HDU 4869 Turn the pokers(思维+逆元)

考试的时候没有做出来。。。

想到了答案一定是一段连续的区间,一直在纠结BFS判断最后的可行1数。

原来直接模拟一遍就可以算出来最后的端点。。。

剩下的就是组合数取模了,用逆元就行了。。。

 

技术分享
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi 3.1415926535
# define eps 1e-9
# define MOD 1000000009
# define INF 1000000000
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<1,l,mid
# define rch p<<1|1,mid+1,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
    int res=0, flag=0;
    char ch;
    if((ch=getchar())==-) flag=1;
    else if(ch>=0&&ch<=9) res=ch-0;
    while((ch=getchar())>=0&&ch<=9)  res=res*10+(ch-0);
    return flag?-res:res;
}
void Out(int a) {
    if(a<0) {putchar(-); a=-a;}
    if(a>=10) Out(a/10);
    putchar(a%10+0);
}
const int N=100005;
//Code begin...

LL f[N];
LL pow_mod(LL a, LL n, LL mod){
    LL ret=1, tmp=a%mod;
    while (n) {
        if (n&1) ret=ret*tmp%MOD;
        tmp=tmp*tmp%MOD;
        n>>=1;
    }
    return ret;
}
LL inv(LL a, LL mod){return pow_mod(a,mod-2,mod);}
void init(){
    f[0]=1;
    FO(i,1,N) f[i]=(f[i-1]*i)%MOD;
}
int main ()
{
    int n, m, x, l, r, tmpl, tmpr;
    LL ans;
    init();
    while (~scanf("%d%d",&n,&m)) {
        l=r=0;
        ans=0;
        FOR(i,1,n) {
            scanf("%d",&x);
            if (l>=x) tmpl=l-x;
            else if(r>=x) tmpl=((l%2)==(x%2))?0:1;
            else tmpl=x-r;
            if (r+x<=m) tmpr=r+x;
            else if(l+x<=m) tmpr=(((l+x)%2)==(m%2)?m:m-1);
            else tmpr=2*m-l-x;
            l=tmpl; r=tmpr;
        }
        for (int i=l; i<=r; i+=2) ans=(ans+(f[m]*inv(f[i]*f[m-i]%MOD,MOD))%MOD)%MOD;
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

HDU 4869 Turn the pokers(思维+逆元)