codeforces C. Mashmokh and Numbers (细心?)

C. Mashmokh and Numbers

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

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 109.

Input

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

Output

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

Examples

input
5 2
output
1 2 3 4 5
input
5 3
output
2 4 3 7 1
input
7 2
output
-1

Note

gcd(x, y) is greatest common divisor of x and y.

题意简化
给你两个数字 n 与 k,需要你输出一个有 n 个不同数字的序列;该序列需从前端第一个数字开始往后每两个一组作最大公约数,直到不可分组(可分组数字数量小于2)为止,并且所有组的最大公约数加起来需要等于 k,如果不存在这样的序列则输出 -1。

分析

题目不难,但是不太好写,因为需要特判的地方稍稍比较多。

先判断输出 -1 的两种情况:
要么是 k<n/2,即所有的组别的最大公约数即使全为 1 也比 k 大;或者是 k 大于 0 的时候 n/2==0,即没有组别为 k 提供公约数。

然后就是剩下的输出序列的情况。
我们可以除了第一个组别以外的最大公约数都为 1 ,第一个组别的公约数加上后面全部的 1 等于 k 即可;

而最大公约数为 1 这只需要使两个数字互质即可,简单举例:连续的正整数之间都是互质的;
证明:假设 a 与 a+1 之间存在比 1 大的公约数 k,即 a = kb,a+1 = k(b+1/k),显然 b+1/k 需要为整数,此时 k 只能为 1 ,这和条件假定冲突,即得证a与a+1互质。

但是题目要求是不同数字,那每次输出前都判断一下是否与前面的重复,重复就加 1 即可;

下面给代码,因为在做的时候有点想偷工减料就用了 a 和 2a+1 以及数据直接从200001开始来进行互质数对输出。
结果代码反而更复杂了,错了几发 (;へ:)

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
#define MS(X) memset(X,0,sizeof(X))
#define MSC(X) memset(X,-1,sizeof(X))
#define rg register
typedef long long LL;
using namespace std;

int main(){
    int n,k,tn;
    bool chk=false;
    cin>>n>>k;
    tn=n/2;
    if(n%2) chk=true;
    if(tn>k||tn==0&&k!=0){
        puts("-1");
        return 0;
    }
    else if(k==0&&tn==0){
        puts("1");
        return 0;
    }
    else{
        int need=k-tn+1;
        int pul=0;
        printf("%d %d",need,2*need);
        for(int i=200001;i<tn+200000;i++){
            while((i+pul)==need||2*(i+pul)+1==need
            ||(i+pul)==2*need) pul++;
            
            printf(" %d %d",i+pul,2*(i+pul)+1);
        }
        if(chk) 
        	if(need!=1) printf(" 1\n");
      	  	else  printf(" 3\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值