Mashmokh and Numbers

                                Mashmokh and Numbers

题目描述:

            这个题目大概讲述的是,让你构建一个长度为n的数列,这个数列的任意两个数是互不相同的,这个数列的得分是这样统计的,首先先找数列前两个数,求出它们的最大公约数,然后总得分加上这个数值,把这两个数移出数列,然后继续重复上面的操作,直到数列只剩下1个数或者没有数,然后要求这个总得分恰好等于k,问我们这个数列是怎样的。如果构建不出这样的数列的话,输出-1。

题目分析:

            首先我们可以先判断一下-1的情况是怎样的,首先我们知道k最少只能是\left \lfloor \frac{n}{2} \right \rfloor,如果k小于这个数的话,那么肯定不存在一个数列满足条件。

            首先我们要知道:两个相邻的数的最大公约数为1。

            因此,当n为奇数时,前面n-3个数我们可以这样去构建,每两个数是连续的数,然后第n-2个数和第n-1个数我们可以构建最大公约数为k-\frac{n-3}{2}这样的两个数,然后最后一个数随便找一个不存在于数列中的数即可。

            当n为偶数时,前面n-2个数我们可以这样去构建,每两个数是连续的数,然后第n-1个数和第n个数我们可以构建最大公约数为k-\frac{n-2}{2}这样的两个数。

            按照上面的思路构建满足条件的数列即可。

代码:

#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <algorithm>
#include <cstring>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <deque>
#define reg register
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define lowbit(x) (x&(-x))
using namespace std;
const int Maxn=1e5+5;
const int Maxa=1e9;
int main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	if (n==1)
	{
		if (k==0)
		{
			printf("1\n");
			return 0;
		}
		printf("-1\n");
		return 0;
	}
	if (k<n/2)
	{
		printf("-1\n");
		return 0;
	}
	int rest=k-n/2+1;
	if (rest>n)
	{
		int temp=n/2*2-1;
		for (reg int i=2;i<=temp;i++) printf("%d ",i);
		printf("%d %d ",rest,rest*2);
		if (n&1) printf("1\n");
		return 0;
	}
	int t=n/rest*rest+rest;
	printf("%d %d ",rest,t);
	for (reg int i=1;i<=n-2;i++) printf("%d ",t+i);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36679229/article/details/88925657