Harmonic Value Description (思维+找规律)

Harmonic Value Description

  HDU - 5916 
 
The harmonic value of the permutation p1,p2,pn is 
i=1n1gcd(pi.pi+1)

Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n].

InputThe first line contains only one integer T (1T100), which indicates the number of test cases. 

For each test case, there is only one line describing the given integers n and k (12kn10000).
OutputFor each test case, output one line “Case #x: p1 p2  pn”, where x is the case number (starting from 1) and p1 p2  pn is the answer.

Sample Input

2

4 1

4 2

Sample Output

Case #1: 4 1 3 2

Case #2: 2 4 1 3

思路:如果k=1 那么便 1 2 3...n 相邻两两之间的最大公约数为1 ,如果k不为1,那么只用让k和2*k这两个数之间的最大公约数变成k,其余仍为1即可。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream>
 5 using namespace std;
 6 int casen;
 7 int n,k;
 8 int main()
 9 {
10     cin>>casen;
11     int cas=0;
12     while(casen--)
13     {
14         scanf("%d%d",&n,&k);
15         cas++;
16         printf("Case #%d:",cas);
17         if(k==1)
18         {
19             for(int i=1;i<=n;i++)
20             {
21                 printf(" %d",i);
22             }
23             printf("\n");
24         }
25         else
26         {
27             printf(" %d %d",2*k,k);
28             for(int i=k-1;i>=1;i--)
29                 printf(" %d",i);
30             for(int i=k+1;i<=n;i++)
31             {
32                 if(i!=2*k)
33                 {
34                     printf(" %d",i);
35                 }
36             }
37             printf("\n");
38         }
39     }
40 return 0;
41 }

猜你喜欢

转载自www.cnblogs.com/1013star/p/9709845.html
今日推荐