codefoeces 414A (构造)

A. 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的序列(数字不重复),然后每次从前面拿出两个数,使得每次

两个数的gcd和==k.

找出一大堆质数,然后不断的放质数(k-1),一直到最后放k*2和k*2(如果最后k剩下1)

还是放两个质数.然后n是奇数的话还要在最后放一个数.

#include <bits/stdc++.h>
using namespace std;
#define maxn 5111111

int n, k;
vector <int> ans;
int num[maxn], cnt;
int is_prime[maxn];

void init () {
    cnt = 0;
    memset (is_prime, 1, sizeof is_prime);
    for (int i = 2; i < maxn; i++) {
        if (is_prime[i]) {
            num[cnt++] = i;
            for (int j = i+i; j < maxn; j += i) {
                is_prime[j] = 0;
            }
        }
    }
}

int main () {
    init ();
    while (cin >> n >> k) {
        if (n == 1) {
            cout << (k == 0 ? 1 : -1) << endl;
            continue;
        }
        int cnt = 0;
        ans.clear ();
        if (k < n/2) {
            printf ("-1\n");
            continue;
        }
        int tot = n/2;
        for (int i = 1; i < tot; i++) {
            ans.push_back (num[cnt++]);
            ans.push_back (num[cnt++]);
            k--;
        }
        if (k > 1) {
            ans.push_back (k*2);
            ans.push_back (k*3);
        }
        else {
            ans.push_back (num[cnt++]);
            ans.push_back (num[cnt++]);
        }
        if (n&1)
            ans.push_back (1);
        for (int i = 0; i < ans.size (); i++)
            cout << ans[i] << (i == ans.size ()-1 ? '\n' : ' ');
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值