HDU 5916 Harmonic Value Description(规律+构造)

Harmonic Value Description

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 54    Accepted Submission(s): 33
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  p    n ”, 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
 
题意:找出一个n的全排列p1,p2...pn 使得gcd(p1,p2)+gcd(p2,p3)+..+gcd(pn-1,pn)为所有得到结果的第k小;
题目有给2k<=n,说明对于n个数只会询问最多第n/2小的数,因此只要不断把n中a[k] a[2k]提前,使整个数列的其他数互质并且相邻数也和a[2k]互质;
在构造的时候尽可能少的移动过多元素导致某两个数互质,因此我们可以把a[k] a[2k]和a[1] a[2]交换,因为a[1]肯定和相邻数互质,a[2k]前后都是奇数,与2互质;
但是这样交换时我们有可能使得a[2k]与a[3]非互质,所以当这种情况出现时,我们再将a[3]与a[2k-1]交换;
因为a[2k]是6的倍数,a[2k-1]一定是奇数,交换后与a[4]一定互质。
因此就可以构造出满足题意的排列了~
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
using namespace std;
const int N=10005;
int a[N];

int main(){
	int t,cas=1;
	scanf("%d",&t);
	while(t--){
		int n,k;
		scanf("%d %d",&n,&k);
	
		for(int i=1;i<=n;i++){
			a[i]=i;
		}
		swap(a[1],a[k]);
		swap(a[2],a[2*k]);
		if(a[2]%3==0){
			swap(a[3],a[2*k-1]);
		}
		
		printf("Case #%d:",cas++);
		for(int i=1;i<=n;i++){
			printf(" %d",a[i]);
		}
		printf("\n");
	
	
	}
}
/*
999
18 9
30 15
999
2 1
3 1
4 1
4 2
5 1
5 2
6 1
6 2
6 3
7 1
7 2
7 3
8 1
8 2
8 3
8 4
9 1
9 2
9 3
9 4
10 1
10 2
10 3
10 4
10 5
*/

再次%董亚美学姐……一下子就想到把k 2k提前,而窝还是一直手算各种考虑傻逼情况(ㄒoㄒ)//
一定要看清楚k的范围!!






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值