Ignatius and the Princess II (full arrangement)

Source: Internet
Author: User

Ignatius and the Princess II

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 6359 Accepted Submission (s): 3760

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 INPUT6 411 8

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

Problem: Water, let's find the first small sequence; it's small here should be reverse order number from small to large;

In fact, is a full arrangement, the recursion is good;

Code:

#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>using namespacestd;#defineMem (x, y) memset (x,y,sizeof (x))#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)#defineP_ printf ("")Const intmaxn=1010;intANS[MAXN];intVIS[MAXN];intn,m;intCNT;intFlot;voidDfsintnum) {    if(Flot)return; if(num==o) {CNT++; if(cnt==M) {             for(intI=0; i<n;i++){                if(i) p_; printf ("%d", Ans[i]); } puts (""); Flot=1; }        return ; }     for(intI=0; i<n;i++){        if(vis[i+1])Continue; Ans[num]=i+1; Vis[i+1]=1; DFS (Num+1); Vis[i+1]=0; }}intMain () { while(~SCANF ("%d%d",&n,&M)) {mem (Vis,0); CNT=flot=0; DFS (0); }    return 0;} 

Ignatius and the Princess II (full arrangement)

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.