POJ 1833 permutation (STL)

Source: Internet
Author: User

Arranged

http://poj.org/problem?id=1833

Time limit:1000ms

Memory limit:30000k

Description

Topic Description:

As you know, given a positive integer n, the number of n 1 to n can constitute n! , listing these permutations in a small to large order (dictionary order), such as n=3, listing 1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 16 permutations.

Task Description:

Give an arrangement, find the arrangement of the following K, and if the last arrangement is encountered, the next 1 rows are ranked 1th, that is, 1 2 3...n.

For example: n = 3,k=2 gives an arrangement of 2 3 1, its bottom 1 arranges 3 1 2, and the next 2 is ranked 3 2 1, so the answer is 3 2 1.

Input

The first line is a positive integer m, represents the number of test data, the following is the M group of test data, the first row of each set of test data is 2 positive integers n (1 <= N < 1024) and K (1<=k<=64), the second row has n positive integers, is 1,2 ... an arrangement of N.

Output

For each group of input data, output a row, n number, separated by a space in the middle, representing the input arrangement of the next K arrangement.

Sample Input

3

3 1

2 3 1

3 1

3 2 1

10 2

1 2 3 4 5 6 7 8 9 10

Sample Output

3 1 2

1 2 3

1 2 3 4 5 6 7 9 8 10

Complete code:

/*469ms,168kb*/
    
#include <cstdio>  
#include <algorithm>  
using namespace std;  
    
int num[1024];  
    
int main (void)  
{  
    int m, n, K;  
    scanf ("%d", &m);  
    while (m--)  
    {  
        scanf ("%d%d", &n, &k);  
        for (int i = 0; i < n; ++i)  
            scanf ("%d", &num[i]);  
        while (k--)  
            next_permutation (num, num + N);  
        printf ("%d", num[0]);  
        for (int i = 1; i < n; ++i)  
            printf ("%d", Num[i]);  
        Putchar (' \ n ');  
    }  
    return 0;  
}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.