【HDU - 5916】Harmonic Value Description (构造,思维,SJ题)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/82430328

题干:

The harmonic value of the permutation p1,p2,⋯pnp1,p2,⋯pn is 

∑i=1n−1gcd(pi.pi+1)∑i=1n−1gcd(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].

Input

The first line contains only one integer T (1≤T≤1001≤T≤100), which indicates the number of test cases. 

For each test case, there is only one line describing the given integers n and k (1≤2k≤n≤100001≤2k≤n≤10000).

Output

For each test case, output one line “Case #x: p1 p2 ⋯ pnp1 p2 ⋯ pn”, where x is the case number (starting from 1) and p1 p2 ⋯ pnp1 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

解题报告:

      构造。这题显然不会有统一的规律让你去找或者暴力求解。所以我们要考虑是不是个Special Judge的题,也就是,我们只需要给出其中的一个答案就可以了。

      显然我们不难发现,最小的∑值,恰好是n-1,也就是说,按照升序排序,每两个数的gcd都=1,所以他让求第k小的,也就是我们只需要让其中一对儿gcd=1的变成=k的就可以了,题目给了一个隐藏的暗示:2*k<=n,所以我们就把2*k 和 k 放在前两个的位置上,其余的数依旧保证“相邻的数的gcd=1”即可。于是想到先继续从k+1开始输出到2*k-1,然后再输出1~k-1  。这样刚好符合题意。

AC代码:

#include<bits/stdc++.h>
using namespace std;
 
int main(){
    int t,n,cas=1,k;
    cin>>t;
    while(t--){
        cin>>n>>k;
        printf("Case #%d: %d %d",cas++,2*k,k);
        for(int i=k+1;i<=n;i++){
            if(i==2*k)continue;
            printf(" %d",i);
        }
        for(int i=1;i<=k-1;i++)printf(" %d",i);
        puts("");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/82430328