hdu 5916 Harmonic Value Description

来源:互联网 发布:手机用数据会出400bad 编辑:程序博客网 时间:2024/05/02 02:31
Problem Description
The harmonic value of the permutation p1,p2,pn is
i=1n1gcd(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 (1T100), which indicates the number of test cases.

For each test case, there is only one line describing the given integers n and k (12kn10000).
 

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
24 14 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;}

0 0
原创粉丝点击