πŸ•΅πŸ»β€β™‚οΈ κ³¨λ“œλ°”νμ˜ μΆ”μΈ‘

λ°±μ€€ Silver1

πŸ“– μ•Œκ³ λ¦¬μ¦˜

πŸ‘‰πŸ» μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ μ²΄λŠ” [μ†Œμˆ˜ 검증]을 ν•  λ•Œ μ‚¬μš©ν•œλ‹€.

이 문제의 핡심은 νŠΉμ • μˆ«μžκ°€ μ†Œμˆ˜μΈμ§€ μ•„λ‹Œμ§€ νŒλ³„μ„ ν•΄μ•Όν•œλ‹€λŠ” 것이닀.

n = a+b일 λ•Œ, b-aκ°€ μ΅œλŒ€κ°€ λ˜λ„λ‘ 섀정을 ν•΄μ•Όν•˜λ―€λ‘œ 2λΆ€ν„° n-1κΉŒμ§€ μ†Œμˆ˜ νŒλ³„μ„ ν•΄μ•Όν•œλ‹€.

이해가 쉽도둝 예λ₯Ό 듀어보겠닀.

πŸ“ 섀계

α„‹α…§α†«α„‰α…³α†Έα„Œα…‘α†Ό-67

b-aκ°€ μ΅œλŒ€λ‘œ λ˜λ„λ‘ ν•˜λ €λ©΄ aλ₯Ό 2λΆ€ν„° μ‹œμž‘ν•΄μ„œ κ³¨λ“œλ°”νμ˜ 좔츑을 λ§Œμ‘±μ‹œν‚¬λ•Œ STOP을 ν•˜λ©΄ λœλ‹€.

λ²”μœ„κ°€ 2~n-1인 μ΄μœ λŠ” 1은 μ–΄μ°¨ν”Ό μ†Œμˆ˜κ°€ μ•„λ‹ˆκΈ° λ•Œλ¬Έμ΄λ‹€.

μ‹œκ°„ 초과 섀계

μ²˜μŒμ—λŠ” n이 λ“€μ–΄μ˜¬ λ•Œλ§ˆλ‹€ μ†Œμˆ˜ νŒλ³„ ν•¨μˆ˜λ₯Ό κ°€λ™μ‹œμΌœ 닡을 좜λ ₯ν•˜λ„λ‘ ν•˜μ˜€λŠ”λ° κ·Έλ ‡κ²Œ ν•˜λ©΄ μ‹œκ°„ μ΄ˆκ³Όκ°€ λ°œμƒν•œλ‹€.

κ·Έλ³΄λ‹€λŠ” μž…λ ₯ μ œν•œ λ²”μœ„κ°€ 이미 μ •ν•΄μ ΈμžˆμœΌλ‹ˆ 미리 λ°±λ§ŒκΉŒμ§€μ˜ μ†Œμˆ˜νŒλ³„ 배열을 μƒμ„±ν•œ λ‹€μŒ κ³¨λ“œλ°”νμ˜ 좔츑을 μ§„ν–‰ν•˜λŠ” 것이 λ”μš± νš¨μœ¨μ μ΄λ‹€.

πŸ‘¨πŸ»β€πŸ’» CODE

TLE

import sys
input = sys.stdin.readline

# μ†Œμˆ˜ 검증 ν•¨μˆ˜ - μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체
def is_prime(n):
    sieve = [True]*(n+1)

    m = int(n**0.5)

    for i in range(2,m+1):
        if sieve[i] == True:
            for j in range(2*i,n+1,i):
                sieve[j] = False

    return sieve

while True:
    n = int(input())

    if n == 0:
        break
    
    sieve = is_prime(n)
    for i in range(2,n-1):
        if sieve[i] == True and sieve[n-i] == True:
            print("{0} = {1} + {2}".format(n,i,n-i))
            break

μ •λ‹΅

import sys
input = sys.stdin.readline

# μ†Œμˆ˜ 검증 ν•¨μˆ˜ - μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체
def is_prime(n):
    sieve = [True]*(n+1)

    m = int(n**0.5)

    for i in range(2,m+1):
        if sieve[i] == True:
            for j in range(2*i,n+1,i):
                sieve[j] = False

    return sieve

sieve = is_prime(1000000)

while True:
    n = int(input())

    if n == 0:
        break
    
    for i in range(2,n-1):
        if sieve[i] == True and sieve[n-i] == True:
            print("{0} = {1} + {2}".format(n,i,n-i))
            break

Β© 2022. All rights reserved.

Powered by Hydejack v9.1.6