/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 95ms 19.578 MiB
#2 Time Exceeded ≥1602ms ≥21.512 MiB
#3 Time Exceeded ≥1603ms ≥21.93 MiB

Code

import sys
import random
import math

input = sys.stdin.readline

def is_valid(perm):
    for i in range(len(perm)-1):
        a, b = perm[i], perm[i+1]
        if math.gcd(a, b) != 1 or abs(a - b) <= 1:
            return False
    return True

def solve():
    T = int(input())
    for _ in range(T):
        N = int(input())
        if N in (2,3,4,6):
            print(-1)
            continue

        perm = list(range(1, N+1))
        # keep shuffling until we find a valid one
        while True:
            random.shuffle(perm)
            if is_valid(perm):
                print(*perm)
                break

if __name__ == "__main__":
    solve()

Information

Submit By
Type
Submission
Problem
P1203 D. Roy Loves Permutation
Language
PyPy 3 (Python 3.9.18 PyPy 7.3.15)
Submit At
2025-06-09 07:16:33
Judged At
2025-06-09 07:16:33
Judged By
Score
2
Total Time
≥1603ms
Peak Memory
≥21.93 MiB