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

来源:互联网 发布:站群软件 编辑:程序博客网 时间:2024/05/17 08:49

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=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 2Case #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


0 0
原创粉丝点击