春季训练赛4:思维模拟题 CF415C Mashmokh and Numbers

「CF415C」
It’s holiday. Mashmokh and his boss, Bimokh, are playing a game invented by Mashmokh.

In this game Mashmokh writes sequence of n distinct integers on the board. Then Bimokh makes several (possibly zero) moves. On the first move he removes the first and the second integer from from the board, on the second move he removes the first and the second integer of the remaining sequence from the board, and so on. Bimokh stops when the board contains less than two numbers. When Bimokh removes numbers x and y from the board, he gets gcd(x, y) points. At the beginning of the game Bimokh has zero points.

Mashmokh wants to win in the game. For this reason he wants his boss to get exactly k points in total. But the guy doesn’t know how choose the initial sequence in the right way.

Please, help him. Find n distinct integers a1, a2, …, an such that his boss will score exactly k points. Also Mashmokh can’t memorize too huge numbers. Therefore each of these integers must be at most 1e9.

Input
The first line of input contains two space-separated integers n, k (1 ≤ n ≤ 1e5; 0 ≤ k ≤ 1e8).

Output
If such sequence doesn’t exist output -1 otherwise output n distinct space-separated integers a1, a2, …, an (1 ≤ ai ≤ 1e9).

题意

题目说给你n个空格填数字,让你找这些数字,使得它们满足下列条件:
这些数字从左往右每次取两个数字(不够两个就不取),这两个数字的gcd为num,把所有的num加到sum里面,那么走完后,要求sum要等于题目给定的数字k。

思路

这里首先是看到题目中的数据,k是小于等于1e8的,而题目说我们凑的数字要小于1e9,那么我们可以用大于1e8小于1e9的数字来凑,以达成题目要求的:每个数字不能重复。
然后看看给出的个数能够凑成多少对两个的数字,用pair=n/2算就OK。然后除了第一对,其他全部输出gcd为1的数字,用大于1e8的数字来配。用两个连续的数字,gcd肯定等于1。然后我们看看这样子凑了pair-1对后,还k差多少,然后第一对的输出(还欠的值)和(还欠的值的倍数)就可以了。

代码

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=1e9; 
int  main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	//分多种情况讨论,模拟即可。
	//难点在于如何配出目标值
	//除了第一个,其他的都为1,第一个的gcd用剩余的需要配的。 
	int pairs=n/2,remain=k-pairs+1,i;
	
	if(pairs>k)//因为组数太多,目标值太小而凑不出来.
		return 0*printf("-1\n");   
	if(n==1)//或者因为无法形成组数来配对
	{
		if(k==0)
			return 0*printf("1");
		else
			return 0*printf("-1");
	}
	printf("%d %d ",remain,remain*2);
	for(i=2;i<=pairs;i++)
		printf("%d %d ",maxn-2*i,maxn-2*i-1);//两个连续的数字的gcd肯定为1 
	if(n&1)//有奇数个数字,那么最后面还要配一个数字 
		printf("%d",maxn-2*i);
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值