Ignatius and the Princess II

韓小妹發表於2018-08-12

給你N個整數,分別是1,2,3,。。。N。問你全排列的第M個排列為多少?

Input

The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub's demand. The input is terminated by the end of file. 

Output

For each test case, you only have to output the sequence satisfied the BEelzebub's demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number. 

Sample Input

6 4
11 8

Sample Output

1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10

AC碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=10010;
int a[maxn];
int main()
{
	int n,m;
	while(~scanf("%d %d",&n,&m))
	{
		memset(a,0,sizeof(a));
		int t=1;
		for(int i=0;i<n;i++)
		a[i]=i+1;
		while(next_permutation(a,a+n))
		{//引數n指的是 進行排列的長度
			if(t++==m-1)
			break;
		}
		for(int i=0;i<n-1;i++)
		printf("%d ",a[i]);
		printf("%d\n",a[n-1]);
	}
	return 0;
}

 

相關文章