Harmonic Value Description
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Special Judge
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
next_permutation打了个表 发现 调和值必定>=n-1且连续
所以第k小的串 就是调和值=k+n-2的
2个连续的奇数gcd必定=1
当k%2==1时
gcd(k,2*k)=k //调和值 + k-1
gcd(k,2)=1
gcd(2*k-1 ,2*k+1)=1
gcd(k-1,k-3)>=2 //所以把1放k-1,k-3之间,使调和值不变
排列2*k , k , 2 , 3 , 4 ...... k-1 , 1 , k-3 , ... , 2*k-1 ,2*k+1 , ....必定符合
当k%2==0时
gcd(k-1,2*(k-1))=k-1 //调和值 + k-2
gcd(2(k-1),2)=1 //调和值再 + 1
gcd(2(k-1)-1, 2(k-1)+1)=1
gcd(k-2,k)>=2 //所以1放k-2,k之间,使调和值不变
k-1 , 2(k-1), 2 , 3 , .......k-2, 1 , k ,.....2(k-1)-1, 2(k-1)+1 , .....必定符合
然后特判下k=2的情况就可以了
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>
//#define CHECK_TIME
const int N=10000+3;
int elem[N];
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
int main()
{
//freopen("/home/lu/文档/r.txt","r",stdin);
//freopen("/home/lu/文档/w.txt","w",stdout);
int T,n,k;
scanf("%d",&T);
for(int tt=1;tt<=T;++tt){
scanf("%d%d",&n,&k);
printf("Case #%d:",tt);
for(int i=1;i<=n;++i)
elem[i]=i;
if(k==2){
printf(" 2 4 1 3");
for(int i=5;i<=n;++i)
printf(" %d",i);
putchar('\n');
continue;
}
if(k&1){
printf(" %d %d",2*k,k);
elem[k]=1;
for(int i=2;i<=n;++i)
if(i!=2*k)
printf(" %d",elem[i]);
}
else{
printf(" %d %d %d",k-1,2*(k-1),2);
elem[k-1]=1;
for(int i=3;i<=n;++i)
if(i!=2*(k-1))
printf(" %d",elem[i]);
}
putchar('\n');
}
return 0;
}