1037

/*
动态规划 计数
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef long long LL;
typedef unsigned long long ULL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define FORi(a,b,c) for(int i=(a);i<(b);i+=c)
#define FORj(a,b,c) for(int j=(a);j<(b);j+=c)
#define FORk(a,b,c) for(int k=(a);k<(b);k+=c)
#define FORp(a,b,c) for(int p=(a);p<(b);p+=c)
#define FORii(a,b,c) for(int ii=(a);ii<(b);ii+=c)
#define FORjj(a,b,c) for(int jj=(a);jj<(b);jj+=c)
#define FORkk(a,b,c) for(int kk=(a);kk<(b);kk+=c)

#define FF(i,a)    for(int i=0;i<(a);i++)
#define FFD(i,a)   for(int i=(a)-1;i>=0;i--)

#define Z(a) (a<<1)
#define Y(a) (a>>1)

const double eps = 1e-6;
const double INFf = 1e10;
const int INFi = 1000000000;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}


// code begin
LL dp[21][21][2]; // 0 up, 1 down

int T,N;
LL M;
vector<int> ans;
bool used[22];


void dfs(int n,LL ret,int pre,int fag)
{
	//printf("%d %lld %d %d\n",n,ret,pre,fag);
	if(n<=0) return;
	LL t = 0;
	int dx = 1;
	if(fag) //下降
	{
		for(int j=1;j<=pre;j++)
		{
			if(!used[j])
			{
				t+=dp[n][dx][0];
				if(t>=ret)
				{
					used[j] = true;
					ans.push_back(j);
					dfs(n-1,ret-t+dp[n][dx][0],j,0);
					break;
				}
				dx++;
			}

		}
	}
	else
	{
		for(int j=1;j<=pre;j++)
			if(!used[j])
				dx++;
		for(int j=pre;j<=N;j++)
		{
			if(!used[j])
			{
				t += dp[n][dx][1];
				if(t>=ret)
				{
					used[j] = true;
					ans.push_back(j);
					dfs(n-1,ret-t+dp[n][dx][1],j,1);
					break;
				}
				dx++;
			}
		}
	}
}

int main()
{
	read;
	write;
	memset(dp,0,sizeof(dp));
	dp[1][1][0] = dp[1][1][1] = 1;
	FORi(2,21,1)
	{
		FORj(1,i+1,1)
		{
			FORk(j,i+1,1)
			{
				dp[i][j][0] += dp[i-1][k][1];
			}
			FORk(1,j,1)
			{
				dp[i][j][1] += dp[i-1][k][0];
			}
		}
	}

	scanf("%d",&T);
	while(T--)
	{
		scanf("%d %lld",&N,&M);
		
		ans.clear();
		memset(used,0,sizeof(used));
		//printf("ans:\n");
		int  p =0;
		LL t = 0;
		FORi(1,N+1,1)
		{
			if(!used[i])
			{
				for(int k=1;k>=0;k--) //先下降,在上升
				{
					t += dp[N][i][k];
					if(t>=M)
					{
						used[i] = true;
						ans.push_back(i);
						dfs(N-1,M-t+dp[N][i][k],i,k);
						p = 1;
						break;
					}
				}
			}
			if(p) break;
		}
		
		//
		
		printf("%d",ans[0]);
		FORi(1,ans.size(),1)
		{
			printf(" %d",ans[i]);
		}
		printf("\n");
	}
	return 0;
}

posted @ 2011-04-02 09:49  AC2012  阅读(146)  评论(0编辑  收藏  举报