hdu 4869 Turn the pokers 策略(組合數),hdupokers

來源:互聯網
上載者:User

hdu 4869 Turn the pokers 策略(組合數),hdupokers

題意:輸入操作次數n和撲克牌數m,一開始撲克牌全都背面朝上。現在輸入n個數xi,表示選擇xi張牌翻轉,問最後的牌的情況有多少種可能?

題解:

我們將一開始的牌認為是m個0,而翻轉就是將0變成1或者1變成0。最後的答案就是ans=∑C(m,k),C(m,k)為組合數,k為所有能取到的1的可能個數。詳細的解釋,先瞭解最後1的個數的奇偶性,跟所有翻牌數的和的奇偶相同(每次翻牌,要麼0->1,要麼1->0,都是在改變1的個數奇偶)。之後我們需要找到最少有i個1,以及最大有j個1;i的情況就是有1就翻1,j的情況就是有0就翻0,而中間的情況時,取偶數步數,一半翻0,一半翻1,保持不變,所以可以確定i,i+2,i+4,...,j-2,j都能被翻到。最後ans=∑C(m,k)(i<=k<=j&&k%2==i%2)。





代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <ctime>#include <algorithm>#include <cmath>#include <queue>#include <vector>using namespace std;#define LL __int64const int maxn=1e5+10;const int mod=1e9+9;LL c[maxn];LL pow_mod(LL a,int b){    LL s=1;    while(b)    {        if(b&1)s=s*a%mod;        a=a*a%mod;        b=b>>1;    }    return s;}int main(){    int n,m;    //freopen("C:\\Documents and Settings\\Administrator\\案頭\\in.txt","r",stdin);    //freopen("C:\\Documents and Settings\\Administrator\\案頭\\out.txt","w",stdout);    //printf("%I64d\n",pow_mod(2,10));    while(scanf("%d%d",&n,&m)!=EOF)    {        int i,j,k,x,p,q;        i=j=0;        for(k=0;k<n;k++)        {            scanf("%d",&x);            //求最小1的個數i            if(i>=x)p=i-x;            else if(j>=x)p=((i&1)==(x&1)?0:1);            else p=x-j;            //求最大1的個數j            if(j+x<=m)q=j+x;            else if(i+x<=m)q=(((i+x)&1)==(m&1)?m:m-1);            else q=2*m-(i+x);            i=p;j=q;            //printf("**%d %d\n",i,j);        }        LL ans=0;        c[0]=1;        if(i==0)ans+=c[0];        for(k=1;k<=j;k++)        {            if(m-k<k)c[k]=c[m-k];            else c[k]=c[k-1]*(m-k+1)%mod*pow_mod(k,mod-2)%mod;            if(k>=i&&(k&1)==(i&1))ans+=c[k];        }        printf("%I64d\n",ans%mod);    }    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.