HDU 5916 - Harmonic Value Description

Problem Description
The harmonic value of the permutation p1,p2,⋯pn is
∑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≤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≤10000).


Output
For 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


题意:先定义和谐值:一个序列中,将每两个相邻的数都求一遍GCD的全部的和。给出一个序列和一个值 k,将给出的序列进行全排列,求出第 k 小的和谐值所对应的序列。给出一个 T 表示数据组数,每行给出一个 n 和 k,n 表示 1 - n 的序列。

一开始想用 next_permutatiom 函数来做,但是数据真是大的不行。后来发现可以构造出一个序列来,因为相邻的两个数的GCD是1,所以第 k 小的值就是让 k 和 2*k 相邻,其他相邻的数的GCD还是1即可。一开始先摆出 2*k 和 k,然后 k+1 到 n,最后 1 到 k-1。

注意是 k 和 k+1 相邻,不要让 2*k 和 k+1 相邻。


#include <cstdio>

int main()
{
    int T;
    scanf("%d", &T);
    for (int icase = 1; icase <= T; ++icase)
    {
        int n, k;
        scanf("%d%d", &n, &k);
        printf("Case #%d: %d %d", icase, k*2, 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);
        printf("\n");
    }
    return 0;
}


发布了158 篇原创文章 · 获赞 23 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章