Harmonic Value Description
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 147 Accepted Submission(s): 103
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 ⋯ 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
题意:问第k小的排列是什么
解题思路:第一小的肯定是 gcd(pi,pi+1)= 1的;第二小的就是有一个是2,其它都是1;第k小的就是有一个是k,其他都是1;注意题目给的数据是 2*k<=n的;所以把看k 和 2*k 提出来,其它的保证gcd(pi,pi+1)= 1即可;
#include <iostream>
#include <cmath>
#include <cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int a[10004];
int main()
{
int t,n,k,cas=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
printf("Case #%d:",++cas);
printf(" %d %d",2*k,k);
for(int i=k-1; i>0; i--)
printf(" %d",i);
for(int i=k+1; i<=n; i++)
if(i!=2*k)
printf(" %d",i);
printf("\n");
}
return 0;
}

本文探讨了如何找出所有n个数的排列中,和谐值为严格第k小的排列。通过分析排列特点,给出了一个高效的算法实现,并提供了完整的代码示例。
325

被折叠的 条评论
为什么被折叠?



