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