您的位置:首页 > 产品设计 > UI/UE

hdu 5916 Harmonic Value Description

2016-10-08 12:41 337 查看
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
.

 

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()肯定为1.
很重要的一个条件,1<=2k<=n<=10000;
所以,提取出k 和 2k;
但是当k为奇数时。 gcd(k-1,k+1)可能不是1.
所以把1放在 k-1 和 k+1 之间 保证除了 k 可 2k ;其他gcd()都为1。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T,n,k,ca=1;
cin>>T;
while(T--)
{
scanf("%d%d",&n,&k);
printf("Case #%d: ",ca++);
if(n==1&&k==1) printf("1");
else
if(k&1)
{
printf("%d %d",2*k,k);
for(int i=2;i<k;i++)
printf(" %d",i);
if(k!=1) printf(" 1");
for(int i=k+1;i<2*k;i++)
printf(" %d",i);
for(int i=2*k+1;i<=n;i++)
printf(" %d",i);
}
else
{
printf("%d %d",2*k,k);
for(int i=1;i<k;i++)
printf(" %d",i);
for(int i=k+1;i<2*k;i++)
printf(" %d",i);
for(int i=2*k+1;i<=n;i++)
printf(" %d",i);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: