Martian 2004 Noip National League universal Group

Source: Internet
Author: User

Title Description Description

The man finally ascended the land of Mars and saw the mysterious Martians. Neither humans nor Martians can understand each other's language, but our scientists have invented a method of digital communication. This is the way of communication, first of all, the Martians told a very large number of human scientists, scientists cracked the meaning of the figure, and then put a very small number on this large number, the results told the Martians, as the human answer.

Martians use a very simple way to represent numbers--breaking fingers. The Martians had only one hand, but there were thousands of fingers on the hand, and the fingers were lined up in a column numbered three-in-one. Any of the two fingers of a Martian can exchange positions at will, and they are counted by this method.

A Martian uses a human hand to demonstrate how to count with a finger. If the five fingers-thumb, forefinger, middle finger, ring finger and pinky are numbered 1,2,3,4 and 5 respectively, when they are arranged in the normal order, forming 5 digits 12345, when you exchange the position of the ring finger and the little finger, will form 5 digits 12354, when you put five fingers in the order completely reversed, Will form 54321, in all the 120 5 digits that can be formed, 12345 the smallest, which represents 1;12354 second small, which represents 2;54321 Max, which represents 120. The following table shows the 6 3-digit numbers that can be formed when there are only 3 fingers and the number they represent:

Three-in-number

123

132

213

231

312

321

The number represented

1

2

3

4

5

6

Now you are fortunate to be the first Earth man to communicate with the Martians. A Martian will let you look at his finger, and the scientist will tell you to add a very small number of them. Your task is to add the numbers that the Martians use their fingers to the numbers scientists tell you, and to change the order of the Martians ' fingers according to the added results. The input data guarantees that this result will not exceed the range that the Martian finger can represent.

Enter a description Input Description

The input consists of three lines, and the first line has a positive integer N, representing the number of Martian fingers (1 <= N <= 10000). The second line is a positive integer M, which represents the small integer to be added (1 <= M <= 100). The next line is an arrangement of 1 to N of n integers, separated by spaces, to indicate the order in which the Martian fingers are arranged.

Output description Output Description

The output has only one row, and this line contains n integers, indicating the order in which the changed Martian fingers are arranged. Each of the two adjacent numbers is separated by a space and cannot have extra spaces.

Sample input Sample Input

5

3

1 2 3) 4 5

It's not too hard to change the whole line of code.

Code implementation:

#include <cstdio>
int n,m,q[30000],s[30000];
BOOL v[30000];
void Dfs (int x) {
if (x==n) {
m--;
if (!m) {//full of added items,
for (int i=0;i<n;i++) {//outputs.
printf ("%d", s[i]);
if (i==n-1) printf ("\ n");
else printf ("");
}
n=0;//cut off the remainder of the search.
}
Return
}
int a=1;
while (Q[x]) {a++;q[x]--;} Make the search go to the numbers given by the Martians.
for (int i=a;i<=n;i++) {
if (!v[i]) {
S[x]=i;v[i]=1;
DFS (X+1);
v[i]=0;
}
}
}
int main () {
scanf ("%d%d", &n,&m); m++;
for (int i=0;i<n;i++) {scanf ("%d", &q[i]); q[i]--;}
DFS (0);
return 0;
}

Some people use STL, for example, I have to learn ...

The codes are as follows:

#include<cstdio>

#include<algorithm>

Using namespace std;

< Span class= "Hljs-meta-keyword" > int n,m; int a[10010];

< Span class= "Hljs-meta-keyword" > int main< Span class= "Hljs-params" > () {

< Span class= "Hljs-meta-keyword" > scanf ( "%d%d", &n,&m);

< Span class= "Hljs-meta-keyword" > for (int i=0;i<n;i++) scanf ("%d ", &a[i]);

< Span class= "Hljs-meta-keyword" > for (int i=0;i<m;i++) next_permutation (a,a+ n);

< Span class= "Hljs-keyword" >< Span class= "Hljs-number" >for (int i=0;i<n;i++) printf (

< Span class= "Hljs-keyword" >< Span class= "Hljs-number" >return 0;/ /He didn't hit this because of the compression code!

 

< Span class= "Hljs-keyword" >< Span class= "Hljs-keyword" >< Span class= "Hljs-title" >}

< Span class= "Hljs-keyword" >< Span class= "Hljs-number" > Mars people 04 is a fire. ~

Martian 2004 Noip National League universal Group

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.