permutation 1

permutation 1

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int A[22];
 4 void init()
 5 {
 6     for(int i=1; i<=21; i++)A[i]=i;
 7 }
 8 int N;
 9 struct Nod
10 {
11     int A[11],B[12];
12 };
13 Nod T[50005];
14 bool cmp(Nod A,Nod B)
15 {
16     int n=N-1;
17     if(n>8)n=8;
18     for(int i=0; i<n; i++)
19     {
20         if(A.B[i]!=B.B[i])
21         return(A.B[i]<B.B[i]);
22 
23     }
24 }
25 void work()
26 {
27     long long K;
28     int cnt=0;
29     init();
30     scanf("%d%lld",&N,&K);
31     if(N<=8)
32     {
33         do
34         {
35             for(int i=0; i<N; i++)
36             {
37                 T[cnt].A[i]=A[i+1];
38             }
39             for(int i=0; i<N-1; i++)
40             {
41                 T[cnt].B[i]=T[cnt].A[i+1]-T[cnt].A[i];
42             }
43             cnt++;
44 
45         }
46         while(next_permutation(A+1,A+N+1));
47 
48         sort(T,T+cnt,cmp);
49         for(int i=0; i<N; i++)
50         {
51             cout<<T[K-1].A[i]<<(i==N-1?'\n':' ');
52         }
53 
54     }
55     else
56     {
57         do
58         {
59             if(N>9)
60             T[cnt].A[0]=N-9;
61             else T[cnt].A[0]=N;
62             for(int i=N-8; i<N; i++)
63             {
64                 T[cnt].A[i-N+9]=A[i];
65             }
66             for(int i=0; i<9; i++)
67             {
68                 //cout<<T[cnt].A[i]<<'\n';
69                 T[cnt].B[i]=T[cnt].A[i+1]-T[cnt].A[i];
70             }
71             cnt++;
72 
73 
74         }
75         while(next_permutation(A+N-8,A+N));//cout<<cnt<<'\n';
76         sort(T,T+cnt,cmp);
77         if(N>9)
78         cout<<N<<' ';
79         for(int i=1;i<=N-10;i++){
80             cout<<i<<' ';
81         }
82         for(int i=0; i<9; i++)
83         {
84             cout<<T[K-1].A[i]<<(i==8?'\n':' ');
85         }
86     }
87 }
88 int main()
89 {
90     freopen("1.in","r",stdin);
91     int _T;
92     scanf("%d",&_T);
93     while(_T--)
94     {
95         work();
96     }
97 
98 }

 

posted @ 2019-08-06 12:24  liulex  阅读(196)  评论(0编辑  收藏  举报