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;
}