hdu5916Harmonic Value Description+构造

Problem Description
The harmonic value of the permutation p1,p2,⋯pn is
n1i=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).

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
2016中国大学生程序设计竞赛(长春)-重现赛

求一个排列,这个排列所有相邻的两个数的GCD的累加和是:所有排列的累加GCD中第K大的。
先打了表。发现每种排列里,累加GCD最小的那个是n-1(GCD全为1),那么第k大的应该就是n+k-2,相当与这个排列里有个GCD为1的变成了k。那么我们就要是的新的排列里多了个GCD==k,自然想到k,与2*k.(题目中悄悄的给了个2*k<=n)。那么就是说2k肯定存在。。所以直接让k,2k在一起其他依然GCD==1就好。然后我们又可以发现2*k-1与2*k+1其实是互质(打个表,至少10000以内是)拿好直接两这两个拼接起来。我们无法判断2*k与k+1是否互质,那么我们可以吧1拿过来隔开他们。这样这个序列就构成了。。

//我们不知道2*k-1与2*k+1是否互质是,其实可以吧2拿过来隔开这里(前提是k!=2),因为2*k-1与2*k+1都是奇数,与2肯定互质,所以也可以这么构造。(k==2呢?我相信谁都会构造,2,4,1,3.。。。)

#include<bits/stdc++.h>
using namespace std;
#define LL long long

int a[1000];
int main(){
    int t,n,k;
    cin>>t;
    for(int cas=1;cas<=t;cas++){
        cin>>n>>k;
        cout<<"Case #"<<cas<<": ";
        if(k==1){
            cout<<1;
            for(int i=2;i<=n;i++) cout<<" "<<i;
            cout<<endl;
            continue;
        }
        for(int i=2;i<=k-1;i++) cout<<i<<" ";
        cout<<k<<" "<<2*k<<" "<<1;
        for(int i=k+1;i<=2*k-1;i++) cout<<" "<<i;
        for(int i=2*k+1;i<=n;i++) cout<<" "<<i;
        cout<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值