Problem Description
The harmonic value of the permutation p1,p2,⋯pn is
∑n−1i=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
Source
2016中国大学生程序设计竞赛(长春)-重现赛
求一个排列,这个排列所有相邻的两个数的GCD的累加和是:所有排列的累加GCD中第K大的。
先打了表。发现每种排列里,累加GCD最小的那个是n-1(GCD全为1),那么第k大的应该就是n+k-2,相当与这个排列里有个GCD为1的变成了k。那么我们就要是的新的排列里多了个GCD==k,自然想到k,与2*k.(题目中悄悄的给了个2*k<=n)。那么就是说2k肯定存在。。所以直接让k,2k在一起其他依然GCD==1就好。然后我们又可以发现2*k-1与2*k+1其实是互质(打个表,至少10000以内是)拿好直接两这两个拼接起来。我们无法判断2*k与k+1是否互质,那么我们可以吧1拿过来隔开他们。这样这个序列就构成了。。
//我们不知道2*k-1与2*k+1是否互质是,其实可以吧2拿过来隔开这里(前提是k!=2),因为2*k-1与2*k+1都是奇数,与2肯定互质,所以也可以这么构造。(k==2呢?我相信谁都会构造,2,4,1,3.。。。)
#include<bits/stdc++.h>
using namespace std;
#define LL long long
int a[1000];
int main(){
int t,n,k;
cin>>t;
for(int cas=1;cas<=t;cas++){
cin>>n>>k;
cout<<"Case #"<<cas<<": ";
if(k==1){
cout<<1;
for(int i=2;i<=n;i++) cout<<" "<<i;
cout<<endl;
continue;
}
for(int i=2;i<=k-1;i++) cout<<i<<" ";
cout<<k<<" "<<2*k<<" "<<1;
for(int i=k+1;i<=2*k-1;i++) cout<<" "<<i;
for(int i=2*k+1;i<=n;i++) cout<<" "<<i;
cout<<endl;
}
return 0;
}