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

转载于:https://www.cnblogs.com/moonbay/archive/2012/08/02/2619615.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值