首页 > 代码库 > HDU 4869 Turn the pokers(推理)

HDU 4869 Turn the pokers(推理)

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