hdu 1027 Ignatius and the Princess II(全配列)
2979 ワード
転載は出典を明記してください。http://blog.csdn.net/u012860063
テーマリンク:http://acm.hdu.edu.cn/showproblem.php?pid=1027
Ignatius and the Princess II
Problem Description
Now our heo finds the door to the BEelzbub feng 5166.He opens the door and finds feng 5166 isaabot to kill out to pretty Princess.But now the BEelzbub has to beathe herso first.feng.feng 5166 says、「I forfore e e e e e e e e e e e e e e e e e thethethethe the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the ininininine e e e e e e e e e gnatius says confidently「OK、at last,I will save the Princess.「」Now I will show you the first problem.「feng 5166 says」Given a sequence of number 1 to N,we define that 1,2,3…N-1N is the smalest sequence among all the sequence which can be compsed with number 1 to N(each number can be and shoud be use onlyonece).So it's see the second smber 1.Number 2.Noves 2.N giveN and M.You shoud tell me the Mth smalest sequence which is compsed with number 1 to N.It's easury,isn't is?hahahahahahahaha…「Can you help Ignatius to solive this problem?
Input
The input contains several test case s.Each test case consists of two numbers,N and M(1<=N==1000,1<=M==10000).You may asume there is always a sequence satisfied the BEeleb'deput.The mination.The。
Output
For each test case,you only have to output the sequence satisfied the BEelzeb's demand.When output a sequence,you shoud print a space between two numbers,but dot nout put any spaces after the.last.
Sample Input
Sample Output
与えられたnとm nは1…nの数字配列を表し、m番目の配列を求める。
この問題でSTLの神器:next_を発見しました。permutationとprev_permutation(前の)関数
STLドキュメントの説明に従い、next_permutation関数は、指定されたシーケンスの次の大きなシーケンスをアルファベット順に生成します。シーケンス全体がマイナス順になるまで。prev_permutation関数は、与えられたシーケンスを生成する前の小さなシーケンスである。二つの原理は同じで、一回の順番だけが反対です。
コードは以下の通りです
テーマリンク:http://acm.hdu.edu.cn/showproblem.php?pid=1027
Ignatius and the Princess II
Problem Description
Now our heo finds the door to the BEelzbub feng 5166.He opens the door and finds feng 5166 isaabot to kill out to pretty Princess.But now the BEelzbub has to beathe herso first.feng.feng 5166 says、「I forfore e e e e e e e e e e e e e e e e e thethethethe the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the ininininine e e e e e e e e e gnatius says confidently「OK、at last,I will save the Princess.「」Now I will show you the first problem.「feng 5166 says」Given a sequence of number 1 to N,we define that 1,2,3…N-1N is the smalest sequence among all the sequence which can be compsed with number 1 to N(each number can be and shoud be use onlyonece).So it's see the second smber 1.Number 2.Noves 2.N giveN and M.You shoud tell me the Mth smalest sequence which is compsed with number 1 to N.It's easury,isn't is?hahahahahahahaha…「Can you help Ignatius to solive this problem?
Input
The input contains several test case s.Each test case consists of two numbers,N and M(1<=N==1000,1<=M==10000).You may asume there is always a sequence satisfied the BEeleb'deput.The mination.The。
Output
For each test case,you only have to output the sequence satisfied the BEelzeb's demand.When output a sequence,you shoud print a space between two numbers,but dot nout put any spaces after the.last.
Sample Input
6 4 11Sample Output
1 2 3 5 4 1 2 4 4 4 5 6 7 9 8 11 10与えられたnとm nは1…nの数字配列を表し、m番目の配列を求める。
この問題でSTLの神器:next_を発見しました。permutationとprev_permutation(前の)関数
STLドキュメントの説明に従い、next_permutation関数は、指定されたシーケンスの次の大きなシーケンスをアルファベット順に生成します。シーケンス全体がマイナス順になるまで。prev_permutation関数は、与えられたシーケンスを生成する前の小さなシーケンスである。二つの原理は同じで、一回の順番だけが反対です。
コードは以下の通りです
#include
#include
#include
#include
using namespace std;
#define N 1047
int num[N];
int main()
{
int n , m , i ,k;
while(~scanf("%d%d",&n,&m))
{
memset(num,0,sizeof(num));
for(i = 0 ; i < n ; i++)
{
num[i] = i+1;
}
for(i = 1 ; i < m ; i++)
{
next_permutation(num,num+n);
}
for(i = 0 ; i < n-1 ; i++)
printf("%d ",num[i]);
printf("%d
",num[n-1]);
}
return 0;
}