45G - Prime Problem - CodeForces Solution


number theory *2200

Please click on ads to support us..

Python Code:

def is_prime(val):
    if val <= 1:
        return False

    if val == 2:
        return True

    if (val & 1) == 0:
        return False

    d = 3
    while d*d <= val:
        if val % d == 0:
            return False
        d += 2
    return True

n = (int(input()))
f = [1]*(n+1)

def devide2prime(val):
    for i in range(2, n+1):
        if f[i] == 1 and is_prime(i) and is_prime(val - i):
            f[i] = 2
            return

tot = (n+1) * n // 2

def solve():
    if is_prime(tot):
        return

    if tot % 2 == 0:
        devide2prime(tot)
    else:
        if is_prime(tot - 2):
            f[2] = 2
            return

        f[3] = 3
        devide2prime(tot - 3)

solve()
for i in range(1, n+1):
    print(f[i], end = " ")


Comments

Submit
0 Comments
More Questions

1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones