HDU 4869 Turn the pokers (inference), hdupokers

Source: Internet
Author: User

HDU 4869 Turn the pokers (inference), hdupokers
HDU 4869 Turn the pokers

Question Link

Question: given n flip poker methods, you can select xi cards for each method to flip. There are m cards in total and the number of cards after the last flip is displayed.

Train of Thought: For each flip, if we can determine the final positive increase, then all the situations are the sum of C (m, Zhang number) in all cases, then, we will find that there will actually be an upper and lower bounds, and the number at every two locations is a good solution, because during the flop, the corresponding card will definitely be flipped, if you flip one more card to the top, you need to flip one more card to the bottom, and the parity remains unchanged. Therefore, you only need to enter the number of cards each time, maintain the upper and lower bounds, and finally sum the cards.

Code:

#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;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.