hdu 2062搜索

挺简单,直接上代码。

/*
 * hdu2062/win.cpp
 * Created on: 2012-10-27
 * 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 long long LL;
const int MAXN = 22;
LL f[MAXN];
int ans[MAXN], N;
bool used[MAXN];
void init() {
    f[0] = 0;
    for(int i = 1; i <= 20; i++) {
        f[i] = i * (f[i - 1] + 1);
    }
}

int getnum(int t) {
    int i = 1;
    while(t > 1 || used[i]) {
        if(!used[i]) {
            t--;
        }
        i++;
    }
    return i;
}

void dfs(int step, LL M) {
    if(M <= 0) {
        ans[step] = -1;
        return ;
    }
    if(step == N) {
        return ;
    }
    int t = 1;
    while(M > f[N - step - 1] + 1) {
        t++;
        M -= f[N - step - 1] + 1;
    }
    ans[step] = getnum(t);
    used[ans[step]] = true;
    dfs(step + 1, M - 1);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    init();
    LL M;
    while(scanf("%d%I64d", &N, &M) == 2) {
        fill(used, used + MAXN, false);
        dfs(0, M);
        printf("%d", ans[0]);
        for(int i = 1; i < N; i++) {
            if(ans[i] > 0) {
                printf(" %d", ans[i]);
            }else {
                break;
            }
        }
        putchar('\n');
    }
//    copy(f, f + 21, ostream_iterator<LL>(cout, "\t"));
    return 0;
}
posted @ 2012-10-28 12:40  moonbay  阅读(129)  评论(0编辑  收藏  举报