414A - Mashmokh and Numbers - CodeForces Solution


constructive algorithms number theory *1500

Please click on ads to support us..

Python Code:

import math

items = input().split(" ")
n = int(items[0])
k = int(items[1])    

cnt = n // 2
if n == 1 or k < cnt:
        if n == 1 and k == 0:
        print(1)
    else:
        print(-1)
elif k == cnt:
        ans = [i + 1 for i in range(n)]
    print(*ans)
else:
    left = k - (cnt - 1)
            
    
    ans = [left, 2 * left]
    i = 1
    while len(ans) < n:
        if i != ans[0] and i != ans[1] and math.gcd(i, ans[-1]) == 1:
            ans.append(i)
        i += 1
    print(*ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,k;
    cin>>n>>k;
    if(n == 1 && k == 0)
    {
        cout<<1;
    }
    if(k<n/2 || (n == 1 && k>0))
    {
        cout<<-1<<endl;
    }
    else if(k == n/2)
    {
        for(int i = 1;i<=n;i++)
        {
            cout<<i<<" ";
        }
    }
    else
    {
        long long int x = n/2;
        long long int a = k-x+1;
        long long int b = 2*(k-x+1);
        cout<<a<<" "<<b<<" ";
        n-=2;
        while(n>0)
        {
            b++;
            cout<<b<<" ";
            n--;
        }
    }
}


Comments

Submit
0 Comments
More Questions

461A - Appleman and Toastman
320B - Ping-Pong (Easy Version)
948A - Protect Sheep
387A - George and Sleep
53A - Autocomplete
1729G - Cut Substrings
805B - 3-palindrome
805C - Find Amir
676C - Vasya and String
1042B - Vitamins
1729F - Kirei and the Linear Function
25D - Roads not only in Berland
1694A - Creep
659F - Polycarp and Hay
1040A - Palindrome Dance
372A - Counting Kangaroos is Fun
1396B - Stoned Game
16A - Flag
1056A - Determine Line
670B - Game of Robots
1418C - Mortal Kombat Tower
1382B - Sequential Nim
1272C - Yet Another Broken Keyboard
808A - Lucky Year
1245A - Good ol' Numbers Coloring
58B - Coins
1041C - Coffee Break
507A - Amr and Music
1041D - Glider
1486A - Shifting Stacks