HDU 4869 Turn the pokers(推理),hdupokers

來源:互聯網
上載者:User

HDU 4869 Turn the pokers(推理),hdupokers
HDU 4869 Turn the pokers

題目連結

題意:給定n個翻轉撲克方式,每次方式對應可以選擇其中xi張進行翻轉,一共有m張牌,問最後翻轉之後的情況數

思路:對於每一些翻轉,如果能確定最終正面向上張數的情況,那麼所有的情況就是所有情況的C(m, 張數)之和,那麼這個張數進行推理會發現,其實會有一個上下界,每隔2個位置的數字就是可以的方案,因為在翻牌的時候,對應的肯定會有牌被翻轉,而如果向上牌少翻一張,向下牌就要多翻一張,奇偶性是不變的,因此只要每次輸入張數,維護上下界,最後在去求和即可

代碼:

#include <cstdio>#include <cstring>typedef long long ll;const ll MOD = 1000000009;const int N = 100005;int n, m, num;ll fac[N];ll exgcd(ll a, ll b, ll &x, ll &y) {    if (!b) {x = 1; y = 0; return a;}    ll d = exgcd(b, a % b, y, x);    y -= a / b * x;    return d;}ll inv(ll a, ll n) {    ll x, y;    exgcd(a, n, x, y);    return (x + n) % n;}ll C(int n, int m) {    return fac[n] * inv(fac[m] * fac[n - m] % MOD, MOD) % MOD;}int main() {    fac[0] = 1;    for (ll i = 1; i < N; i++)fac[i] = fac[i - 1] * i % MOD;    while (~scanf("%d%d", &n, &m)) {scanf("%d", &num);int up = num;int down = num;for (int i = 1; i < n; i++) {    scanf("%d", &num);    int up2 = m - down;    int down2 = m - up;    if (num >= down && num <= up)down = ((down&1)^(num&1));    else if (num < down) down = down - num;    else down = num - up;    if (num >= down2 && num <= up2) {up = m - ((up2&1)^(num&1));    }    else if (num < down2) {up = m - (down2 - num);    }    else up = m - (num - up2);}ll ans = 0;for (int i = down; i <= up; i += 2) {    ans = (ans + C(m, i)) % MOD;}printf("%lld\n", ans);    }    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.