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

HDU 5916 Harmonic Value Description【构造】【2016中国大学生程序设计竞赛(长春)】

2016-10-21 22:27 411 查看


Harmonic Value Description

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 431    Accepted Submission(s): 274
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
.

 

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

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5916

题意:1-n的自然数,让你将其重新排序,使得相邻两个数的GCD之和为K,(K<=n/2)

原数列的GCD之和为n-1。

第一小的肯定是n-1,第二小的肯定是n-2,只需要使得出现相邻的两个数的GCD为2就OK了,以此类推,

因题目中K<=n/2,极限情况就是将所有的偶数放在依次放在前面,奇数依次放到后面即可,

该题有多种答案,满足即可。

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=10000+5;
bool vis[maxn];
int main()
{
int T,kase=0;
cin>>T;
while(T--)
{
int n,k;
cin>>n>>k;
memset(vis,false,sizeof(vis));
printf("Case #%d:",++kase);
for(int i=1;i<=k;i++)
{
vis[i*2]=true;
}
for(int i=1;i<=n;i++)
if(vis[i])
cout<<" "<<i;
for(int i=1;i<=n;i++)
if(!vis[i])
cout<<" "<<i;
cout<<endl;
}
return 0;
}

尊重原创,转载请注明出处:http://blog.csdn.net/hurmishine
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐