Harmonic Value Description
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 54 Accepted Submission(s): 33
Special Judge
Problem Description
The harmonic value of the permutation
p1,p2,⋯pn
is
Mr. Frog is wondering about the permutation whose harmonic value is the strictly k-th smallest among all the permutations of [n].
∑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 ).
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 ⋯ p n
”, 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
题意:找出一个n的全排列p1,p2...pn 使得gcd(p1,p2)+gcd(p2,p3)+..+gcd(pn-1,pn)为所有得到结果的第k小;
题目有给2k<=n,说明对于n个数只会询问最多第n/2小的数,因此只要不断把n中a[k] a[2k]提前,使整个数列的其他数互质并且相邻数也和a[2k]互质;
在构造的时候尽可能少的移动过多元素导致某两个数互质,因此我们可以把a[k] a[2k]和a[1] a[2]交换,因为a[1]肯定和相邻数互质,a[2k]前后都是奇数,与2互质;
但是这样交换时我们有可能使得a[2k]与a[3]非互质,所以当这种情况出现时,我们再将a[3]与a[2k-1]交换;
因为a[2k]是6的倍数,a[2k-1]一定是奇数,交换后与a[4]一定互质。
因此就可以构造出满足题意的排列了~
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int N=10005;
int a[N];
int main(){
int t,cas=1;
scanf("%d",&t);
while(t--){
int n,k;
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++){
a[i]=i;
}
swap(a[1],a[k]);
swap(a[2],a[2*k]);
if(a[2]%3==0){
swap(a[3],a[2*k-1]);
}
printf("Case #%d:",cas++);
for(int i=1;i<=n;i++){
printf(" %d",a[i]);
}
printf("\n");
}
}
/*
999
18 9
30 15
999
2 1
3 1
4 1
4 2
5 1
5 2
6 1
6 2
6 3
7 1
7 2
7 3
8 1
8 2
8 3
8 4
9 1
9 2
9 3
9 4
10 1
10 2
10 3
10 4
10 5
*/
再次%董亚美学姐……一下子就想到把k 2k提前,而窝还是一直手算各种考虑傻逼情况(ㄒoㄒ)//
一定要看清楚k的范围!!