首页 > ACM题库 > HDU-杭电 > HDU 4759-Poker Shuffle-数论-[解题报告]HOJ
2015
09-17

HDU 4759-Poker Shuffle-数论-[解题报告]HOJ

Poker Shuffle

问题描述 :

Jason is not only an ACMer, but also a poker nerd. He is able to do a perfect shuffle. In a perfect shuffle, the deck containing K cards, where K is an even number, is split into equal halves of K/2 cards which are then pushed together in a certain way so as to make them perfectly interweave. Suppose the order of the cards is (1, 2, 3, 4, …, K-3, K-2, K-1, K). After a perfect shuffle, the order of the cards will be (1, 3, …, K-3, K-1, 2, 4, …, K-2, K) or (2, 4, …, K-2, K, 1, 3, …, K-3, K-1).
Suppose K=2^N and the order of the cards is (1, 2, 3, …, K-2, K-1, K) in the beginning, is it possible that the A-th card is X and the B-th card is Y after several perfect shuffles?

输入:

Input to this problem will begin with a line containing a single integer T indicating the number of datasets.
Each case contains five integer, N, A, X, B, Y. 1 <= N <= 1000, 1 <= A, B, X, Y <= 2^N.

输出:

Input to this problem will begin with a line containing a single integer T indicating the number of datasets.
Each case contains five integer, N, A, X, B, Y. 1 <= N <= 1000, 1 <= A, B, X, Y <= 2^N.

样例输入:

3
1 1 1 2 2
2 1 2 4 3
2 1 1 4 2

样例输出:

Case 1: Yes
Case 2: Yes
Case 3: No

标签: 思维 模拟
题意: 对于一副有2^n张牌的扑克, 标号1 ~ 2^n. 起初, 牌是有序的(1, 2, 3, 4, …), 洗完一次牌后, 将变为(1, 3, 5, …, 2^n – 1, 2, 4,…, 2^n) 或 (2, 4, …, 2^n, 1, 3, 5, …, 2^n – 1), 可以多次洗牌, 洗牌的规则总是如此反复. 现问的是, 给你n, a, x, b, y, 有没有可能在若干次洗牌后, 使得a位置的牌的值为x, b位置的牌的值为y. 其中n <= 1000.
分析:
cwz指出这种洗牌规则不符合常规, 还真是, 先不管了~~
突破点, 牌数为2^n, 明显要看数据的二进制表现.
先a = a-1, b = b-1, x = x-1, y = y-1, 便于处理.
对于牌a, 牌b, 一次洗牌后, 如果a与b同奇或同偶, 那么a, b一定会同时分到整副牌的前一半或后一半, 这点在二进制中, 相当于将a, b右移一位, 然后再在其最高位同补上一个0或一个1; 同样的如果a, b奇偶性不同, a, b一定不会同时分到整副牌的前半或后半, 那么在最高位补的数也不一样;
洗一次牌后, 等于是a,b的二进制位都右移了一位, 同时最高位又都补上一位, 补的位恰好其奇偶性是否相同的性质与右移出的那一位是一致的! 那么说, 将a, b的二进制表示数连成一个环(环中n个数对(ai, bi)), 记h(ai, bi) = (ai == bi), 洗牌若干次后, 环的h性质一直不变!
由于只是判断(a, b)到(x, y)是否有解, 意味着, 我们洗一次牌, 右移一位a, b时, 补的最高位的形式是可以随便定的, 只要h性质不变. 

最后此题的作法就是, 先把a,b,x,y化成二进制, 看看是否存在一个t使得对于所有的i, 都有(a[(t + i) % n] == b[(t + i) % n]) == (x[i] == y[i]). 存在即Yes, 否则No. 

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
// #pragma commet(linker, "/STACK:102400000, 102400000")
using namespace std;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<PII> VPII;

template <class T> inline void checkMin(T& a, T b) { if (b < a) a = b; }
template <class T> inline void checkMax(T& a, T b) { if (b > a) a = b; }

const int N = 105;
const double INF = 1e20;

struct Rec {
    int i, j, k;
    Rec(int a, int b, int c) : i(a), j(b), k(c) {}
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int n;
string a, b, x, y;

void getIn(string& x) {
    char s[1005];
    scanf("%s", s);
    int l = strlen(s);
    for (int i = 0; i < l / 2; i++) swap(s[i], s[l - i - 1]);

    int k = 0; while (s[k] == '0') s[k++] = '9';
    s[k]--;
    x.resize(0);
    for (int i = 0; i < n; i++) {
        if ((s[0] - '0') & 1) x += '1'; else x += '0';
        int jw = 0;
        for (int i = l - 1; i >= 0; i--) {
            int val = s[i] - '0' + jw * 10;
            jw = val & 1;
            s[i] = (val >> 1) + '0';
        }
    }
}

int main() {
    int T; scanf("%d", &T);
    for (int cas = 1; cas <= T; cas++) {
        scanf("%d", &n);
        getIn(a);
        getIn(x);
        getIn(b);
        getIn(y);

        bool flag = false;
        for (int t = 0; t < n; t++) {
            bool ff = true;
            for (int i = 0; i < n; i++) {
                int k = i + t;
                if (k >= n) k -= n;
                if ((a[k] == b[k]) ^ (x[i] == y[i])) { ff = false; break; }
            }
            if (ff) { flag = true; break; }
        }
        if (flag) printf("Case %d: Yes\n", cas);
        else printf("Case %d: No\n", cas);
    }
    return 0;
}

参考:http://blog.csdn.net/yihuikang/article/details/12142875