acm-Simple Theme Ignatius and the Princess ii--hdu1027

Source: Internet
Author: User

Reprint Please specify Source:http://blog.csdn.net/lttree

Ignatius and the Princess IITime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 4436 Accepted Submission (s): 2642


Problem Descriptionnow Our hero finds the door to the Beelzebub feng5166. He opens the door and finds feng5166 is on to kill our pretty princess. But now the Beelzebub have to beat our hero first. feng5166 says, "I had three question for you, if you can work them out, I'll release the princess, or you'll be my di Nner, too. " Ignatius says confidently, "OK, at last, I'll save the princess."

"Now I'll show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...n-1,n I s the smallest sequence among all the sequence which can is composed with number 1 to N (each number can is and should be u Se only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...n,n-1. Now I'll give you and numbers, N and M. You should tell me the Mth smallest sequence which are composed with number 1 to N. It's easy, isn ' t? Hahahahaha ... "
Can Ignatius to solve this problem?

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

Outputfor each of the test case, you are only having to output the sequence satisfied the Beelzebub ' s demand. When output A is sequence, you should print a space between the other numbers, but does not output any spaces after the last number.

Sample Input
6 411 8

Sample Output
1 2 3 5 6 41 2 3 4 5 6 7 9 8 11 10

Authorignatius.l
title: http://acm.hdu.edu.cn/showproblem.php?pid=1027


This problem, test instructions is to find the nth arrangement of the M.

Should belong to one of the combinatorial mathematics, just before the cantor unfolded. Feel able to do it with Cantor.

(Comtop unfold details can be stamped →http://blog.csdn.net/lttree/article/details/24798653)

However, I looked at the scope of the data was frightened. N and M (1<=n<=1000, 1<=m<=10000).

Factorial, at most just 10, how n can reach 1000 ....

A careful thought will be able to find that the M maximum is 10000. That is to say, at most, there are only 8 numbers behind to move. The front will not move.

Because the factorial of 1~8 is: 1,2,6,24,120,720,5040,40320.

The factorial of 8 is the order of the 40320>10000 within 10000 kinds. Can only change in the last 8.

Another way of saying it. No matter how much n is, when N>8, the former N-8 is unchanged, there are only 8 changes in the back.

For example: N is 11. So the front 3 number is 1 2 3 The order must be constant, the change is always the back 4~11


After thinking out, solve this problem is not too difficult.

I used to be. The side of the output, but always PE, there may be some place did not think of it.

Directly the answer exists in an ans array, and the last output together, on AC.


/********************************************************************************** Author:Tree                       **from:http://blog.csdn.net/lttree * * Title:ignatius and the Princess II **source:hdu 1027 * * Hint: Cantor expands **********************************************************************************/#i Nclude <iostream>using namespace Std;int fac[]={1,1,2,6,24,120,720,5040,40320};//store answer int ans[10001],len;// Cantor expands the inverse of N to sort several numbers. K is the number of the first few.    Num for this n number should start from how much void Reverse_kangtuo (int n,int k,int num) {int I, J, T, vst[11]={0};    Char s[11];    --k;        for (i=0; i<n; i++) {t = k/fac[n-i-1];                for (j=1; j<=n; j + +) if (!vst[j]) {if (t = = 0) break;            --t;        } s[i] = ' 0 ' +j;        VST[J] = 1;    K%= Fac[n-i-1]; }//Sort the assigned answer array for (int kk=0;kk<n;++kk) ans[len++]=s[kk]-' 1 ' +num;}    int main () {int n,m; int I,j,temp1,temP2;        while (cin>>n>>m) {i=1;        len=0;            if (n>8) {temp1=n%8;            Temp2= (n/8-1) *8;            Corresponding answer assignment for (; i<=temp1;++i) ans[len++]=i;            for (j=0;j<temp2;++j,++i) ans[len++]=i;        Reverse_kangtuo (8,m,i);        } else Reverse_kangtuo (n,m,i);        Output, note that there is no space after the last number for (i=0;i<len-1;++i) cout<<ans[i]<< "";    cout<<ans[len-1]<<endl; } return 0;}


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

acm-Simple Theme Ignatius and the Princess ii--hdu1027

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.