HDU Harmonic Value Description 2016中国大学生程序设计竞赛(长春)-重现赛

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 (12k≤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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值