hdu 2184模拟

/*

 * hdu2184/win.cpp

 * Created on: 2012-8-2

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;

typedef unsigned long long I64u;

typedef stack<int> ms;

I64u powoftwo[64];

void init() {

    powoftwo[0] = 0;

    for(int i = 1; i < 64; i++) {

        powoftwo[i] = powoftwo[i - 1] * 2 + 1;

    }

}



int fun(I64u m) {

    for(int i = 0; i < 64; i++) {

        if(m == powoftwo[i]) {

            return i;

        }

    }

    for(int i = 63; i >= 0; i--) {

        if(m > powoftwo[i]) {

            return -i;

        }

    }

    return 0;

}



void mymove(ms &sa, ms &sc, int n) {

    ms temp;

    for(int i = 0; i < n; i++) {

        temp.push(sa.top());

        sa.pop();

    }

    for(int i = 0; i < n; i++) {

        sc.push(temp.top());

        temp.pop();

    }

}



void work(ms &sa, ms &sb, ms &sc, int n, I64u m) {

    I64u ret = powoftwo[n - 1];

    if(m < ret) {

        work(sa, sc, sb, n - 1, m);

        return ;

    }

    mymove(sa, sb,  n - 1);

    m -= ret;

    if(m > 0) {

        mymove(sa, sc, 1);

        m--;

    }

    if(m > 0) {

        work(sb, sa, sc, n - 1, m);

    }

}



int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    int T, n;

    init();

    scanf("%d", &T);

    I64u m;

    while(T--) {

        scanf("%d%I64u", &n, &m);

        stack<int> s[3];

        for(int i = n; i > 0; i--) {

            s[0].push(i);

        }

        work(s[0], s[1], s[2], n, m);

        for(int i = 0; i < 3; i++) {

            int size = s[i].size();

            printf("%d", size);

            ms temps;

            while(!s[i].empty()) {

                temps.push(s[i].top());

                s[i].pop();

            }

            for(int j = 0; j < size; j++) {

                printf(" %d", temps.top());

                temps.pop();

            }

            putchar('\n');

        }

    }

    return 0;

}

你可能感兴趣的:(HDU)