如果知道STL函数库中的next_permutation函数,此题丝毫没有难度。然后可以反复调用此函数即可。我在此基础上做了优化。因为第N!th的序列都相当于将序列的后n位逆序排列。所以可以在调用next_permutation函数之前,优化前N!次调用(N!<nth)。
#include#include using namespace std;#include int main(){ //freopen("Ignatius and the Princess II.txt","r",stdin); int seqLength,seqNumber; while(scanf("%d%d",&seqLength,&seqNumber)!=EOF) { vector array; int sum=1,start=1; while(seqNumber>=sum*(start+1)) { start++; sum*=start; } for (int i=1;i<=seqLength;i++) array.push_back(i); int reverseStart=seqLength-start; reverse(array.begin()+reverseStart,array.end()); for (int i=sum+1;i<=seqNumber;i++) next_permutation(array.begin(),array.end()); for (int i=0;i