Problem:
 Score : 600 points    Problem Statement Nagase is a top student in high school. One day, she's analyzing some properties of special sets of positive integers.  She thinks that a set S = \{a_{1}, a_{2}, ..., a_{N}\} of distinct positive integers is called special if for all 1 \leq i \leq N , the gcd (greatest common divisor) of a_{i} and the sum of the remaining elements of S is not  1 .  Nagase wants to find a special set of size N . However, this task is too easy, so she decided to ramp up the difficulty. Nagase challenges you to find a special set of size N such that the gcd of all elements are 1 and the elements of the set does not exceed 30000 .      Constraints  3 \leq N \leq 20000         Input Input is given from Standard Input in the following format:  N       Output Output N space-separated integers, denoting the elements of the set S . S must satisfy the following conditions :   The elements must be distinct positive integers not exceeding 30000 .  The gcd of all elements of S is 1 , i.e. there does not exist an integer d > 1 that divides all elements of S .  S is a special set.   If there are multiple solutions, you may output any of them. The elements of S may be printed in any order. It is guaranteed that at least one solution exist under the given contraints.        Sample Input 1 3      Sample Output 1 2 5 63  \{2, 5, 63\} is special because gcd(2, 5 + 63) = 2, gcd(5, 2 + 63) = 5, gcd(63, 2 + 5) = 7 . Also, gcd(2, 5, 63) = 1 . Thus, this set satisfies all the criteria.  Note that \{2, 4, 6\} is not a valid solution because gcd(2, 4, 6) = 2 > 1 .       Sample Input 2 4      Sample Output 2 2 5 20 63  \{2, 5, 20, 63\} is special because gcd(2, 5 + 20 + 63) = 2, gcd(5, 2 + 20 + 63) = 5, gcd(20, 2 + 5 + 63) = 10, gcd(63, 2 + 5 + 20) = 9 . Also, gcd(2, 5, 20, 63) = 1 . Thus, this set satisfies all the criteria.

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    NI = lambda : int(sys.stdin.readline())

    N = NI()

    a = []
    for i in range(3,30001):
        if i % 2 == 0 or i % 3 == 0 or i % 5 == 0:
            a.append(i)

    ans = []
    s = 0
    for x in a[:N-2]:
        ans.append(x)
        s += x
    i = N-2
    while (s + a[i]) % 2:
        i += 1
    ans.append(a[i])
    s += a[i]

    i += 1
    while (s + a[i]) % 30 > 0:
        i += 1
    ans.append(a[i])

    print(*ans)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
M = 30001
if N == 3:
  print(2, 5, 63)
  exit()

L = [3, 9]

N -= 2
if N % 2 == 1:
  L.append(6)
  N -= 1

if N != 0:
  for i in range(2, M, 2):
    if i % 6 == 0: continue
    L.append(i)
    N -= 1
    if N == 0:
      break
  if N % 2 == 1:
    N += 1
    L.pop()

if N != 0:
  for i in range(15, M, 6):
    L.append(i)
    N -= 1
    if N == 0:
      break
if N % 2 == 1:
  N += 1
  L.pop()

if N != 0:
  for i in range(12, M, 6):
    N -= 1
    L.append(i)
    if N == 0:
      break

if N == 1:
  L.append(6)

print(" ".join(list(map(str, L))))

Solution (Python):






def main():
    MAX_S = 30000
    N = int(input())
    if N == 3:
        print('2 3 25')
        return
    multi_2 = [2, 4]  
    multi_3 = [3, 9]  
    multi_6 = []  
    cnt = 4
    i = 4
    while cnt + 2 <= N and 2 * (i + 1) <= MAX_S:
        multi_2.extend([2 * i, 2 * (i + 1)])
        cnt += 2
        i += 3
    j = 5
    while cnt + 2 <= N and 3 * (j + 2) <= MAX_S:
        multi_3.extend([3 * j, 3 * (j + 2)])
        cnt += 2
        j += 4
    k = 1
    while cnt + 1 <= N and 6 * k <= MAX_S:
        multi_6.append(6 * k)
        cnt += 1
        k += 1
    print(' '.join(map(str, multi_2 + multi_3 + multi_6)))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from math import gcd

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def debug(res):
    tot = sum(res)
    for num in res:
        if gcd(tot - num, num) == 1:
            return False
    return True


def resolve():
    n = int(input())

    if n == 3:
        res = [2, 5, 63]
    elif n == 4:
        res = [2, 5, 20, 63]
    elif n == 5:
        res = [2, 5, 20, 30, 63]
    else:
        nums = [num for num in range(2, 30001) if num % 2 == 0 or num % 3 == 0]
        res = [nums[i] for i in range(n)]
        total = sum(res)
        if total % 6 == 2:
            res.pop(res.index(8))
            for i in range(n, len(nums)):
                if nums[i] % 6 == 0:
                    res.append(nums[i])
                    break
        elif total % 6 == 3:
            res.pop(res.index(9))
            for i in range(n, len(nums)):
                if nums[i] % 6 == 0:
                    res.append(nums[i])
                    break
        elif total % 6 == 5:
            res.pop(res.index(9))
            for i in range(n, len(nums)):
                if nums[i] % 6 == 4:
                    res.append(nums[i])
                    break
    print(*res)
    


if __name__ == '__main__':
    resolve()

Solution (Python):
n = int(input())
if n == 3:
    print(2,5,63)
elif n == 4:
    print(2,5,20,63)
else:
    l = set([2,4,3,9])
    x = n-4
    for i in range(1,min(5000,x//2)):
        l.add(6*i+2)
        l.add(6*i+4)
    
    if len(l) != n:
        x = n-len(l)
        if x > 4998:
            for j in range(x-4998):
                l.add(6*(j+1))
            x = 4998
        elif x%2:
            l.add(6)
            x -= 1
        for i in range(2,x+2):
            l.add(6*i+3)
    print(*l)

Solution (Python):
N=int(input())
if N==3:
  print("2 5 63")
  exit()
if N<=15000:
  L=list(range(2,2*(N-1),2))
  if N<=10000:
    if (N-1)%2==0:
      k=N-2
    else:
      k=N-1
    L.append(k)
    L.append(k*3)
    R=list()
  else:
    if (N-1)%2==0:
      k=N-1
    else:
      k=N-2
    while k%2==0:
      k//=2
    L.append(k)
    L.append(k*3)
    R=list()
else:
  if N%2==0:
    L=list(range(2,30002,2))
    N-=15000
    R=list(range(3,3+6*N,6))
  else:
    L=list(range(2,30000,2))
    N-=15000
    R=list(range(3,3+6*(N+1),6))
s=L+R
print(" ".join(list(map(str,s))))

Solution (Python):
def slv(n):
    if n==3:return [2,5,63]
    ary=[]
    sumb=0
    for i in range(2,30001,2):
        ary.append(i)
        sumb+=i
        if len(ary)==n-2:break
    if sumb%3==0:
        pass
    elif sumb%3==1:
        ary.remove(4)
        sumb-=4
    elif sumb%3==2:
        ary.remove(8)
        sumb-=8
    for i in range(3,30001,3):
        if i%2!=0:
            ary.append(i)
            sumb+=i
        if len(ary)==n+1:break
    if len(ary)==n+1 and sumb%2==1:
        sumb-=9
        ary.remove(9)
    elif len(ary)==n+1:
        sumb-=6
        ary.remove(6)
    elif sumb%2==1:
        sumb-=9
        ary.remove(9)
        sumb+=29997
        ary.append(29997)

    return ary

n=int(input())
ary=slv(n)
print(*ary,sep=' ')

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    N = int(readline())

    if N == 3:
        print(2, 5, 63)
    elif N == 4:
        print(2, 5, 20, 63)
    else:
        res = []
        p = N // 8
        q = N % 8
        b = [2, 3, 4, 6, 8, 9, 10, 12]

        for i in range(p):
            res.extend(map(lambda x: x + 12 * i, b))

        d = {0: [], 1: [6], 2: [3, 9], 3: [3, 9, 6], 4: [3, 9, 2, 4], 5: [3, 9, 6, 2, 4],
             6: [3, 9, 6, 2, 4, 12], 7: [3, 9, 6, 2, 4, 8, 10]}
        res.extend(map(lambda x: x + 12 * p, d[q]))
        res.sort()

        print(*res)


if __name__ == '__main__':
    main()

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n = int(ipt())

    if n == 3:
        print("2 3 25")
        exit()
    elif n == 4:
        print("2 5 20 63")
        exit()
    tn = (n//8)*8
    sm = 0
    nm = 0
    ans = []
    la = 0
    for i in range(1,30001):
        if i%2 == 0 or i%3 == 0:
            ans.append(i)
            sm += i
            nm += 1
        if nm == tn:
            if ans:
                la = ans[-1]
            break

    if n%8 == 0:
        pass
    elif n%8 == 1:
        ans.append(la+6)
    elif n%8 == 2:
        ans.append(la+2)
        ans.append(la+4)
    elif n%8 == 3:
        ans.append(la+2)
        ans.append(la+4)
        ans.append(la+6)
    elif n%8 == 4:
        ans.append(la+2)
        ans.append(la+4)
        ans.append(la+6)
        ans.append(la+12)
    elif n%8 == 5:
        ans.append(la+2)
        ans.append(la+3)
        ans.append(la+4)
        ans.append(la+6)
        ans.append(la+9)
    elif n%8 == 6:
        ans.append(la+2)
        ans.append(la+3)
        ans.append(la+4)
        ans.append(la+9)
        ans.append(la+8)
        ans.append(la+10)
    elif n%8 == 7:
        ans.append(la+2)
        ans.append(la+3)
        ans.append(la+4)
        ans.append(la+9)
        ans.append(la+8)
        ans.append(la+10)
        ans.append(la+12)


    print(" ".join(map(str,ans)))


    return None

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
if n==3:exit(print(2,5,63))
l=[6,2,10,3,9,4,8,12]if n%2else[2,10,3,9,4,8,6,12]
for i in range(n):print(i//8*12+l[i%8],end=" ")

Solution (Python):
n = int(input())
ANS = []
A = [2, 10, 3, 9, 4, 8, 6, 12]
if n == 3:
    print(2, 5, 63)
else:
    p = n // 8
    r = n % 8
    ANS += [12 * i + a for a in A for i in range(p)]
    ANS += [12 * p + a for a in A[:r]]
    if r % 2 == 1:
        ANS.pop()
        ANS.append(12 * p + 12)
    print(*ANS)

Solution (Python):
n = int(input())
ANS = []
even = [2, 10, 3, 9, 4, 8, 6, 12]
odd = [12, 2, 10, 3, 9, 4, 8, 6]
if n == 3:
    print(2, 5, 63)
else:
    p = n // 8
    r = n % 8
    if r % 2 == 0:
        tmp = even
    else:
        tmp = odd
    ANS += [12 * i + x for x in tmp for i in range(p)]
    ANS += [12 * p + x for x in tmp[:r]]
    print(*ANS)

Solution (Python):

import math
import collections
N = int(input())

ans = [2, 3, 25]
if N <= 1003:
    ans += [i for i in range(30, (N-3)*30+1, 30)]
    
else:

    D = N - len(ans)
    for i in range(12, min(30000, (D//10)*20) + 1, 30):
        ans.append(i)
        ans.append(i+2)
        ans.append(i+4)
        ans.append(i+6)
        ans.append(i+8)
        ans.append(i+10)
        ans.append(i+12)
        ans.append(i+14)
        ans.append(i+16)
        ans.append(i+18)
    count = 0
    d = N - len(ans)
    for i in range(32, min(30000, (D//5)*10) + 1, 30):
        if i + 30 > min(30000, (D//5)*10) and (d - count*5) % 2 == 0:
            break

        count += 1
        ans.append(i)
        ans.append(i+2)
        ans.append(i+4)
        ans.append(i+6)
        ans.append(i+8)

    d = N - len(ans)
    
    for i in range(33, min(30000, (d//4)*30 + 30)+1, 30):
        ans.append(i)
        ans.append(i+6)
        ans.append(i+18)
        ans.append(i+24)
    d = N - len(ans)
    for i in range(15, min(30000, (d//2)*60)+1, 60):
        ans.append(i)
        ans.append(i+30)

    d = N - len(ans)
    for i in range(325, min(30000, (d//2 - 1)*300 + 325)+1, 300):
        ans.append(i)
        ans.append(i+100)

print(*sorted(ans[:N]))

Solution (Python):
import math

def _print(S):
  print(S)
  return
  S = list(map(int, S.split()))
  print(len(S))
  for s in S:
    if math.gcd(s, sum(S)-s) == 1:
      print("FAIL!!")
      
  

def main():
  N = int(input())
  
  
  
  
  
  

  
  if N > 8:
    n = N % 8
    t = (8 - n)%8
    S = {2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24}
    if t >= 4:
      S -= {4, 8, 14, 16}
      t -= 4
    elif t >= 2:
      S -= {4, 8}
      t -= 2
    while t:
      S -= {6*t}
      t -= 1
    
    i = 2
    
    while N > len(S):
      S.update({i*12+j for j in [2,3,4,6,8,9,10,12]})
      i += 1
      
    _print(' '.join(f'{s}' for s in S))
    
   
    
  elif N == 8:
    _print('2 3 4 6 8 9 10 12')
  elif N == 7:
    _print('2 3 4 8 9 10 12')
  elif N == 6:
    _print('2 3 4 8 9 10')
  elif N == 5:
    _print('2 3 9 10 12')
  elif N == 4:
    _print('2 5 20 63')
  elif N == 3:
    _print('2 5 63')
  
  
main()

Solution (Python):
N = int(input())

if N==2:
    print(3,5)
    exit()
if N==3:
    print(2,5,63)
    exit()
if N==4:
    print(2,5,20,63)
    exit()

if N==5:
    print(2,5,20,30,63)
    exit()

S=set()
i=2
while len(S)<N:
    if i%2==0 or i%3==0:
        S.add(i)
    i+=1

i+=1
r=sum(S)

if r%6==0:
    pass
elif r%6==2:
    S.remove(8)
    while len(S)<N:
        if i%6==0:
            S.add(i)
            break
        i+=1
elif r%6==3:
    S.remove(9)
    while len(S)<N:
        if i%6==0:
            S.add(i)
            break
        i+=1
elif r%6==5:
    S.remove(9)
    while len(S)<N:
        if i%6==4:
            S.add(i)
            break
        i+=1

assert len(S)==N,len(S)
print(" ".join(map(str,S)))

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
if n==3:
    ans = [2,3,25]
elif n==4:
    ans = [2,3,4,9]
else:
    n -= 1
    ans = []
    for i in range(2, 30001):
        if i%2==0 or i%3==0:
            ans.append(i)
        if len(ans)>=(n//4)*4:
            if n%4==1:
                ans.append(i+3)
            elif n%4==2:
                ans.append(i+3)
                ans.append(i+6)
            elif n%4==3:
                ans.append(i+2)
                ans.append(i+3)
                ans.append(i+4)
            break
    s = sum(ans)
    if n==19998:
        ans[-2] = 29996
        ans.append(25)
    else:
        for i in range(30000, 1, -1):
            if i%2==0 or i%3==0:
                if i%6==(6 - s%6)%6 and i not in ans:
                    ans.append(i)
                    break

write(" ".join(map(str,ans)))

Solution (Python):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())

if n==3:
    print("2 5 63")
elif n==4:
    print("2 5 20 63")
elif n==5:
    print("2 5 20 30 63")
else:
    anslst=[]
    for i in range(n):
        if i%4==0:
            anslst.append(i//4*6+2)
        elif i%4==1:
            anslst.append(i//4*6+3)
        elif i%4==2:
            anslst.append(i//4*6+4)
        elif i%4==3:
            anslst.append(i//4*6+6)
    if sum(anslst)%6==0:
        pass
    elif sum(anslst)%6==3:
        anslst.remove(9)
        if anslst[-1]%6==0:
            anslst.append(anslst[-1]+6)
        else:
            anslst.append(anslst[-1]+2)
    elif sum(anslst)%6==2:
        anslst.remove(8)
        anslst.append(i//4*6+6)
    elif sum(anslst)%6==5:
        anslst.remove(9)
        anslst.append(i//4*6+4)
    print(*anslst)

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
import math

if n==3:
    print(2,5,63)
    exit()

a = [2,3,5]
now = 30000

for i in range(n-3):
    if i == n-4:
        total = sum(a)
        m = now
        while True:
            if (total+now)%30 == 0 and math.gcd(total,now) > 1:
                a.append(now)
                break
            if now < 6:
                if a[-1]%2==0:
                    a[-1]-=2
                elif a[-1]%3==0:
                    a[-1]-=3
                elif a[-1]%5==0:
                    a[-1]-=5
                total = sum(a)
                now = m+1
            now-=1
    else:
        while True:
            if now%2 == 0 or now%3 == 0 or now%5 == 0:
                a.append(now)
                now-=1
                break
            now-=1

print(*a)

Solution (Python):
n=int(input())
vec=[]
if n==3:
    vec=[2,5,63]
elif n==4:
    vec=[2,3,4,9]
elif n==5:
    vec=[2,3,4,6,9]
elif n==6:
    vec=[2,3,4,6,9,12]
elif n==7:
    vec=[2,3,4,6,8,9,10]
else:
    for i in range(1,30001):
        if (i%2)*(i%3)==0:
            vec.append(i)
            if len(vec)==n//8*8:
                break
    mx=vec[-1]
    n%=8
    if n==0:
        vec2=[]
    elif n==1:
        vec2=[6]
    elif n==2:
        vec2=[2,4]
    elif n==3:
        vec2=[2,4,6]
    elif n==4:
        vec2=[2,3,4,9]
    elif n==5:
        vec2=[2,3,4,6,9]
    elif n==6:
        vec2=[2,3,4,6,9,12]
    elif n==7:
        vec2=[2,3,4,6,8,9,10]
    for i in vec2:
        vec.append(i+mx)
print(*vec)

Solution (Python):
N=int(input())
if N==3:
  print(2,5,63)
  exit()
P=[]
X=[]
if N&1:
  X=[6,2,10,3,9,4,8,12]
else:
  X=[2,10,3,9,4,8,6,12]
for i in range(N):
  P.append(X[i%8]+12*(i//8))
print(*P)

Solution (Python):
n = int(input())


if n == 3:
    print('2 5 63')
    exit()

ans = [2, 4, 3, 9]
n -= 4
for x in range(8, 30001, 6):
    if n < 2:
        break
    ans.append(x)
    ans.append(x+2)
    n -= 2

for x in range(15, 30001, 12):
    if n < 2:
        break
    ans.append(x)
    ans.append(x+6)
    n -= 2

for x in range(6, 30001, 6):
    if n <= 0:
        break
    ans.append(x)
    n -= 1

ans.sort()
print(*ans)

Solution (Python):
N = int(input())

if N == 3:
    print("2 5 63")
    exit()

N -= 4
ls = [2, 3, 4, 9]
tmp = 1
while N > 1 and tmp < 5000:
    ls.append(6*tmp+2)
    ls.append(6*tmp+4)
    N -= 2
    tmp += 1
tmp = 2
while N > 1 and tmp < 5000:
    ls.append(6*tmp+3)
    ls.append(6*tmp+9)
    tmp += 2
    N -= 2
tmp = 1
while N > 0:
    ls.append(6*tmp)
    tmp += 1
    N -= 1

print(*ls)

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque


from collections import Counter

from itertools import accumulate

from heapq import heapify,heappop,heappush


from functools import lru_cache


def input(): return sys.stdin.readline()[:-1]
def printl(li): print(*li, sep="\n")
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 10**9+7
    
    
    N = int(input())
    
    
    
    
    if N==3:
        print("2 5 63")
        exit()
    if N==4:
        print("2 5 20 63")
        exit()
    N3=min(5000,N//2*2-2) 
    N2=N-N3
    if N2==0 or N2%3==1:
        N3-=2
        N2+=2
    ans=[3+i*6 for i in range(N3)]+[2*j+2 for j in range(N2)]
    print(*ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
if N == 3:
  print(2, 5, 63)
  quit()

N -= 4
ans = [2, 3, 4, 9]
odd = []; even = []; mul6 = []
for i in range(6, 30001):
  if i in ans:
    continue
  if i % 6 == 0:
    mul6.append(i)
  elif i % 3 == 0:
    odd.append(i)
  elif i % 2 == 0:
    even.append(i)

odd = odd[::-1]; even = even[::-1]
while N:
  if N >= 2:
    N -= 2
    if len(odd) >= 2:
      ans.append(odd.pop()); ans.append(odd.pop())
    elif len(even) >= 2:
      ans.append(even.pop()); ans.append(even.pop())
    else:
      ans.append(mul6.pop()); ans.append(mul6.pop())
  else:
    N -= 1
    ans.append(mul6.pop())

print(*ans)

Solution (Python):
N=int(input())
l=[]
for i in range(min(N//6,2500)):
    l+=[i*12+2,i*12+3,i*12+4,i*12+8,i*12+9,i*12+10]
for i in range(N-min(N//6,2500)*6):
    l.append(i*6+6)
if N<6:
    if N==3:
        l=[2,5,63]
    elif N==4:
        l=[2,5,20,63]
    else:
        l=[2,5,20,63,30]
print(*l)

Solution (Python):
N = int(input())
x3 = N//2
x2 = N - x3

if N==3:
    print(2, 5, 63)
else:
    f = lambda x:x*(x+1)//2
    s = set([3*i for i in range(1, x3)])
    v = x3*3
    while 1:
        if (f(x3-1)+v)%2==0:
            s.add(v)
            break
        v += 3

    v = 2
    cnt = 0
    s2 = 0
    while cnt<x2-1:
        if not v in s:
            s2 += v
            s.add(v)
            cnt += 1
        v += 2

    v = 2
    while 1:
        if (v+s2)%3==0 and v not in s:
            s.add(v)
            break
        v += 2

    print(*s)

Solution (Python):
n=int(input())

if n==3:
    print(2, 5 ,63)
elif n==4:
    print(2 ,5 ,20, 63)
elif n==5:
    print(2,5,20,30,63)
else:
    ans=[]
    k=0
    while len(ans)<n:
        ans.append(6*k+2)
        ans.append(6*k+3)
        ans.append(6*k+4)
        ans.append(6*k+6)
        k+=1
    
    ans=sorted(ans)[:n]
    
    nxt=6*(ans[-1]//6)
    if sum(ans)%6==2:
        ans.remove(8)
        ans.append(nxt+6)
    elif sum(ans)%6==3:
        ans.remove(9)
        ans.append(nxt+6)
    elif sum(ans)%6==5:
        ans.remove(9)
        ans.append(nxt+4)
    print(" ".join(list(map(str,ans))))

Solution (Python):
n = int(input())
if n == 3:
  print(2,5,63)
elif n == 4:
  print(2,5,20,63)
elif n == 6:
  print(2,4,6,12,3,9)
elif n == 19999:
  ans = []
  for i in range(5000):
    ans.append(3+6*i)
  for j in range(15000):
    m = 2+2*j
    if m != 30:
      ans.append(m)
  print(*ans)
else:
  m = 1
  if n > 15001:
    m = (n-15000)//2
  for j in range(m,2501):
    if (n-2*j)/3 == int((n-2*j)/3) and 0 < (n-2*j)//3 <= 5000:
      x = j; y = (n-2*x)//3
      
      break
  ans = []
  for i in range(2*x):
    ans.append(3+6*i)
  for j in range(3*y):
    ans.append(2+2*j)
    
  print(*ans)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    if(N==3):
        print(2,5,63)
    elif(N==4):
        print(2,5,20,63)
    elif(N==5):
        print(2,3,4,5,16)
    else:
        MAX = 30000
        ans = []
        for i in range(2,MAX+1):
            if(i%3==0 or i%2==0):
                ans.append(i)
                if(len(ans) == N):
                    break
        if(sum(ans)%6 == 0):
            pass
        else:
            if(6+(sum(ans)%6) in ans): 
                ans.remove(6 + (sum(ans)%6))
                v = ans[-1] + 1
                while True:
                    if(v%6==0):
                        ans.append(v)
                        break
                    v+=1
            else: 
                ans.remove(8)
                v = ans[-1]+1
                while True:
                    if((v+3)%6==0):
                        ans.append(v)
                        break
                    v+=1
        print(*ans)

if __name__ == '__main__':
    main()

Solution (Python):

def solve(n):
    if n == 3:
        return [2, 5, 63]
    elif n == 4:
        return [2, 5, 20, 63]
    elif n == 5:
        return [2, 3, 4, 6, 9]

    ans = []
    for i in range(1, 30001):
        if i % 2 == 0 or i % 3 == 0:
            ans.append(i)
        if len(ans) == n:
            amari = n % 8
            if amari == 1 or amari == 6:
                j = i + 1
                while j % 6 != 0:
                    j += 1
                ans[4] = j
            elif amari == 2 or amari == 5:
                j = i + 1
                while j % 6 != 3:
                    j += 1
                ans[4] = j
            elif amari == 3 or amari == 4:
                j = i + 1
                while j % 6 != 0:
                    j += 1
                ans[5] = j
            break

    return ans


if __name__ == '__main__':
    N = int(input())
    ans = solve(N)
    print(' '.join(map(str, ans)))

Solution (Python):
n = int(input())
if n == 3:
    print("2 5 63")
elif n == 4:
    print("2 5 20 63")
elif n == 5:
    print(2, 8, 14, 3, 9)
elif n <= 8:
    for i in [2, 3, 4, 8, 9, 10, 6, 12][:n]:
        print(i)
else:
    a = [2, 3, 4, 8, 9, 10, 6, 12]
    i = 0
    j = 0
    for _ in range(n // len(a) * len(a)):
        print(a[i] + j, end=" ")
        i += 1
        if i == len(a):
            i = 0
            j += 12

    i = n - n // len(a) * len(a)
    if i == 5:
        for k in [2, 4, 6, 3, 9]:
            print(j + k, end=" ")
    elif i == 4:
        for k in [2, 4, 3, 9]:
            print(j + k, end=" ")
    elif i == 3:
        for k in [6, 3, 9]:
            print(j + k, end=" ")
    else:
        if i >= 6:
            for k in [2, 3, 4, 8, 9, 10]:
                print(k+j, end=" ")
            i -= 6
        j += 6
        for _ in range(i):
            print(j, end=" ")
            j += 6

Solution (Python):
N = int(input())

A = []
B = []
C = []
D = []

for i in range(1, 30001):
    if i % 6 == 0:
        A.append(i)
    elif i % 6 == 2:
        B.append(i)
    elif i % 6 == 3:
        C.append(i)
    elif i % 6 == 4:
        D.append(i)
    else:
        pass

if N == 3:
    res = [2, 5, 63]
elif N == 4:
    res = [2, 4, 3, 9]
elif N == 5:
    res = [2, 4, 3, 9, 6]
elif N <= 15000:
    res = A[:N % 6] + B[:N // 6 * 2] + C[:N // 6 * 2] + D[:N // 6 * 2]
else:
    res = A[:N - 15000] + B + C + D

print(*res)

Solution (Python):
n = int(input())
if n == 3:
    print(2, 5, 63)
elif n == 4:
    print(2, 3, 4, 9)
else:
    l = [2, 3, 4, 9]
    a = 8
    b = 10
    while b <= 30000 and len(l) + 1 < n:
        l.append(a)
        l.append(b)
        a += 6
        b += 6
    c = 15
    d = 21
    while d <= 30000 and len(l) + 1 < n:
        l.append(c)
        l.append(d)
        c += 12
        d += 12
    t = 6
    while t <= 30000 and len(l) < n:
        l.append(t)
        t += 6
    print(' '.join(map(str, l)))

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))






N = I()

def output(X,Y):
    for y in Y:
        X.append(y[0])
        X.append(y[1])
    print(*X)
    return

X = []
Y = []
y2 = []
y3 = []
for i in range(2,30001):
    if i%6 == 0:
        X.append(i)
    else:
        if i%2 == 0:
            y2.append(i)
        elif i%3 == 0:
            y3.append(i)
        else:
            pass
        if len(y2) == 2:
            Y.append(y2)
            y2 = []
        if len(y3) == 2:
            Y.append(y3)
            y3 = []

a = 20000-N

if a == 19997:
    print(2,5,63)
elif a == 19996:
    print(2,5,20,63)
else:
    if a >= 5000:
        if a%2 == 0:
            for _ in range((a-5000)//2):
                Y.pop()
            output([],Y)
        else:
            for _ in range((a-4999)//2):
                Y.pop()
            output([6],Y)
    else:
        for _ in range(a-a//2*2):
            X.pop()
        for _ in range(a//2):
            Y.pop()
        output(X,Y)

Solution (Python):
N=int(input())
if N==3:
    print(2,5,63)
else:
    ans=[2,4,3,9]
    count=4
    i=1
    while N-count>=2 and 4999>=i:
        ans.append(2*(3*i+1))
        ans.append(2*(3*i+2))
        count+=2
        i+=1
    i=1
    while N-count>=2 and 2499>=i:
        ans.append(3*(4*i+1))
        ans.append(3*(4*i+3))
        count+=2
        i+=1
    i=1
    while N-count>0:
        ans.append(6*i)
        count+=1
        i+=1
    print(*ans)

Solution (Python):
N = int(input())
if N == 3:
    print(2, 5, 63)
    exit()
a = []
i = 2
while len(a) < N-N%2-2 and i + 2 <= 30000:
    a.append(i)
    a.append(i+2)
    i += 6
b = []
i = 3
while len(a) + len(b) < N-N%2 and i + 6 <= 30000:
    b.append(i)
    b.append(i+6)
    i += 12
c = []
i = 6
while len(a) + len(b) + len(c) < N:
    c.append(i)
    i += 6
print(' '.join(map(str, a+b+c)))

Solution (Python):
from random import sample

N = int(input())

if N == 3:
    print("2 5 63")
    exit()


L = [n for n in range(6, 30001) if (n % 2 == 0) or (n % 3 == 0)]

while True:
    ans = [2, 3, 4]
    ans += sample(L, N - 3)
    if sum(ans) % 6 == 0:
        break

print(*ans)

Solution (Python):
n = int(input())

base = [2,3,4,8,9,10]
ans = []

if(n==3):
    ans = [2,5,63]
elif(n == 4):
    ans = [2,3,4,9]
elif(n==5):
    ans = [2,3,4,9,6]
elif(n > 15000):
    for i in range(2500):
        for j in base:
            ans.append(j + i*12)
    for i in range(n - 15000):
        ans.append((i+1)*6)

else:
    for i in range(n//6):
        for j in base:
            ans.append(j + i*12)
    for i in range(n%6):
        ans.append((i+1)*6)
    

print(' '.join(map(str,ans)))

Solution (Python):
n = int(input())
l = [{}, {}, {}, {2, 5, 63}, {2, 5, 20, 63}, {2, 3, 4, 6, 9}]
if n <= 5:
  print(*l[n])
  exit(0)
l = [2, 3, 4, 6]
s = set()
for i in range(n):
  s.add(l[i%4] + 6*(i//4))
if sum(s) % 6 == 2:
  s.remove(8)
  s.add(30000)
if sum(s) % 6 == 3:
  s.remove(9)
  s.add(30000)
if sum(s) % 6 == 5:
  s.remove(9)
  s.add(29998)
print(*s)

Solution (Python):
N = int(input())

P2 = list(range(2,30001,2))
P3 = list(range(3,30001,6))
for i in range(1,5002):
  if (N-i*3)%2==0 and 0<(N-3*i)<=5000:
    x = 3*i
    y = (N-i*3)
    break
if N==5 or N>6:
  ans = P2[:x]+P3[:y]
elif N==3:
  ans = [2,5,63]
elif N==4:
  ans = [2,5,20,63]
elif N==6:
  ans = [2,3,4,185,7,9]
if N==19999:
  ans = P2[:14997]+P3[:5000]+[19,425]
print(*ans)

Solution (Python):
import sys
n = int(input())
ans = []




mod3 = 3


mod0 = 6


mod2 = 2


mod4 = 4





c = 0
ans = []


if n == 3:
    print(2, 3, 25)
    sys.exit()

while True:
    if c + 2 > n or mod4 > 30000:
        break
    if c % 4 and mod3 + 6 <= 30000:
        ans.append(mod3)
        ans.append(mod3 + 6)
        mod3 += 12
    else:
        ans.append(mod2)
        ans.append(mod4)
        mod2 += 6
        mod4 += 6

    c += 2

for _ in range(c, n):
    ans.append(mod0)
    mod0 += 6
print(" ".join([str(item) for item in ans]))