码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4869 Turn the pokers(推理)

时间:2016-01-28 20:55:06      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

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


HDU 4869 Turn the pokers(推理)

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5167224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!