Problem Description
Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess."
"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
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
Author
Ignatius.L
参考文献:
这是一道全排列的题!可以调用库函数!next_permuatio(数组起点,数组终点)!按字典序排列!
全排列next_permutation 转载
这个函数可以计算一组数据的全排列
假设数列 d1,d2,d3,d4……
范围由[first,last)标记,调用next_permutation使数列逐次增大,这个递增过程按照字典序。
若当前调用排列到达最大字典序,比如dcba,就返回false,同时重新设置该排列为最小字典序。
返回为true表示生成下一排列成功。
另外,库中另一函数prev_permutation与next_permutation相反,由原排列得到字典序中上一次最近排列。
**********************************************************************************************
答案:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#include<vector>
#include<algorithm>
using namespace std;
int a[10000],coun,n,m,i;
int main(int argc, char* argv[])
{
while (cin>>n>>m)
{
for (i=0;i<n;i++)
{
a[i] = i+1;
}
vector<int> iv(a,a+n);
coun = 1;
{
while (cin>>n>>m)
{
for (i=0;i<n;i++)
{
a[i] = i+1;
}
vector<int> iv(a,a+n);
coun = 1;
while(next_permutation(iv.begin(),iv.end()))
{
coun++;
if (coun == m)
break;
}
printf("%d",iv[0]);
for (i = 1; i < n; ++i)
printf(" %d",iv[i]);
printf("/n");
}
return 0;
}
{
coun++;
if (coun == m)
break;
}
printf("%d",iv[0]);
for (i = 1; i < n; ++i)
printf(" %d",iv[i]);
printf("/n");
}
return 0;
}
个人认为天才的做法(非本人所做):
#include<stdio.h>
int a[1001];
int s[8];
int main(int argc, char* argv[])
{
int i,j,k;
int h,n,m,temp;
for (i = 1; i <= 1000; i++)
a[i] = i;
s[1] = 1; s[2] = 2; s[3] = 3; s[4] = 7;
s[5] = 25; s[6] = 121; s[7] = 721; s[8] = 5041;
while (scanf("%d%d",&n,&m)!=EOF)
{
while (m != 1)
{
for (i = 8; i > 0; i--)
if (m >= s[i])
break;
m = m-s[i]+1;
for (j = n-i+2; j <= n; j++)
{
for (k = n; k > j; k--)
if (a[k] < a[k-1])
{
temp=a[k];
a[k]=a[k-1];
a[k-1]=temp;
}
}
h = n-i+2;
while (a[n-i+1] > a[h])
h++;
temp = a[n-i+1];
a[n-i+1] = a[h];
a[h] = temp;
}
for(j = 1; j <= n; j++)
printf(j-1?" %d":"%d",a[j]);
printf("/n");
for(j = 1; j <= n; j++)
a[j] = j;
}
return 1;
}
{
int i,j,k;
int h,n,m,temp;
for (i = 1; i <= 1000; i++)
a[i] = i;
s[1] = 1; s[2] = 2; s[3] = 3; s[4] = 7;
s[5] = 25; s[6] = 121; s[7] = 721; s[8] = 5041;
while (scanf("%d%d",&n,&m)!=EOF)
{
while (m != 1)
{
for (i = 8; i > 0; i--)
if (m >= s[i])
break;
m = m-s[i]+1;
for (j = n-i+2; j <= n; j++)
{
for (k = n; k > j; k--)
if (a[k] < a[k-1])
{
temp=a[k];
a[k]=a[k-1];
a[k-1]=temp;
}
}
h = n-i+2;
while (a[n-i+1] > a[h])
h++;
temp = a[n-i+1];
a[n-i+1] = a[h];
a[h] = temp;
}
for(j = 1; j <= n; j++)
printf(j-1?" %d":"%d",a[j]);
printf("/n");
for(j = 1; j <= n; j++)
a[j] = j;
}
return 1;
}
本文介绍了一种解决特定数学问题的方法,即找到由1到N组成的第M小的全排列序列。通过使用C++标准库函数next_permutation实现全排列,并给出了两种不同的解决方案。
292

被折叠的 条评论
为什么被折叠?



