首页 > 代码库 > HDU 5916 Harmonic Value Description 【构造】(2016中国大学生程序设计竞赛(长春))
HDU 5916 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 JudgeProblem DescriptionThe 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].
InputThe 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).
OutputFor 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 Input24 14 2
Sample OutputCase #1: 4 1 3 2Case #2: 2 4 1 3
Statistic | Submit | Clarifications | Back
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5916
题目大意:
给你N和K(1<=K<2K<=2N),求一个1~2N的排列,使得∑gcd(Ai,Ai+1)恰为第K小(等视为相同小)。
题目思路:
【构造】
这题一看2K<=2N,可能就是构造题。如果1,2,3...2N排列的话∑gcd(Ai,Ai+1)为第1小。
若要∑gcd(Ai,Ai+1)=K,只需要将2K和K提出来,将2K放在开头,K放在第二个,接下来只要满足相邻两个数仍然相差1即可。
只要将K-1~1接在K后面,把K+1~2N接在1后面,去掉2K即可。(2K-1,2K+1为奇数,gcd=1)
1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #include<iomanip> 8 #include<map> 9 #include<stack>10 #include<queue>11 #include<set>12 #include<bitset>13 #include<memory.h>14 #include<time.h>15 #include<stdio.h>16 #include<stdlib.h>17 #include<string.h>18 //#include<stdbool.h>19 #include<math.h>20 #define min(a,b) ((a)<(b)?(a):(b))21 #define max(a,b) ((a)>(b)?(a):(b))22 #define abs(a) ((a)>0?(a):(-(a)))23 #define lowbit(a) (a&(-a))24 #define sqr(a) ((a)*(a))25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))26 #define mem(a,b) memset(a,b,sizeof(a))27 #define eps (1e-10)28 #define J 1000029 #define mod 100000000730 #define MAX 0x7f7f7f7f31 #define PI 3.1415926535897932332 #pragma comment(linker,"/STACK:1024000000,1024000000")33 #define N 10434 using namespace std;35 typedef long long LL;36 double anss;37 LL aans,sum;38 int cas,cass;39 int n,m,lll,ans;40 int main()41 {42 #ifndef ONLINE_JUDGEW43 // freopen("1.txt","r",stdin);44 // freopen("2.txt","w",stdout);45 #endif46 int i,j,k;47 // init();48 // for(scanf("%d",&cass);cass;cass--)49 for(scanf("%d",&cas),cass=1;cass<=cas;cass++)50 // while(~scanf("%s",s))51 // while(~scanf("%d",&n))52 {53 scanf("%d%d",&n,&m);54 printf("Case #%d: ",cass);55 printf("%d %d",m+m,m);56 for(i=m-1;i;i--)printf(" %d",i);57 for(i=m+1;i<=n;i++)58 if(i!=m+m)printf(" %d",i);59 puts("");60 }61 return 0;62 }63 /*64 //65 66 //67 */
HDU 5916 Harmonic Value Description 【构造】(2016中国大学生程序设计竞赛(长春))
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。