๐Ÿง ๋ฐฑ์ค€ 2581 (์†Œ์ˆ˜์˜ ์›๋ฆฌ ์ด์šฉ)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 

๐Ÿค” ์†Œ์ˆ˜์˜ ์›๋ฆฌ 
์†Œ์ˆ˜์˜ ์›๋ฆฌ => ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ (1๊ณผ ์ž๊ธฐ์ž์‹ )

๐Ÿค” Algorithm ๊ณผ์ • 
1. num์ด๋ผ๋Š” ๋นˆ ๋ฆฌ์ŠคํŠธ์— ์ž…๋ ฅ๋œ M ~ N๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ๋„ฃ๋Š”๋‹ค.
2. ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” cnt๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ, 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
3. ์†Œ์ˆ˜๋ฅผ ๋‹ด์„ ๋ฆฌ์ŠคํŠธ dec์„ ์„ ์–ธํ•œ๋‹ค.
4. num๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ๋ฅผ ์ฐจ๋ก€๋กœ ๋Œ๋ฉด์„œ i % j == 0 ์ฆ‰, ์•ฝ์ˆ˜์ผ ๋•Œ, cnt๊ฐ’์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
5. ์ด๋ ‡๊ฒŒ ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ด๊ธด cnt๋ณ€์ˆ˜์— ๋Œ€ํ•ด ๋งŒ์•ฝ ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ๋ผ๋ฉด(cnt == 2) ์†Œ์ˆ˜์ธ๊ฒƒ!
๋”ฐ๋ผ์„œ ์†Œ์ˆ˜๋ผ๋ฉด dec ๋ฆฌ์ŠคํŠธ์— ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  cnt==0์ด๋ผ๋ฉด ์†Œ์ˆ˜๊ฐ€ ์—†๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 0์„ ์ถœ๋ ฅ!


๐Ÿคซ  solution_2581

 
M, N = int(input()), int(input())
num = []
for i in range(N-M+1):
    num.append(i+M)

dec = []
for i in num:
    cnt = 0
    for j in range(1, i+1):
        if i % j == 0:
            cnt += 1
    if cnt == 2:
        dec.append(i)
if len(dec) == 0:
    print(-1)
else:
    print(sum(dec))
    print(min(dec))

 

๐Ÿง ๋ฐฑ์ค€ 1929 (์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด) _ ์•ž์œผ๋กœ ๊ณ„์† ์ด ๋ฌธ์ œ์—์„œ ํŒŒ์ƒํ•˜์—ฌ ๋ฌธํ•ญ์„ ํ•ด๊ฒฐํ•จ

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 

๐Ÿค” ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด (Sieve of Eratosthenes)
 
์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ์›๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
1. ์†Œ์ˆ˜๊ฐ€ ์•„๋‹Œ 1์„ ์ง€์šด๋‹ค.
2. 2์˜ ๋ฐฐ์ˆ˜๋ฅผ ์ง€์šด๋‹ค.
3. ์•ž์„œ ์ง€์šด 2์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฒƒ๋“ค ์ค‘ ์ตœ์†Œ๊ฐ’์ธ 3์˜ ๋ฐฐ์ˆ˜๋ฅผ ์ง€์šด๋‹ค.
4. ์•ž์„œ ์ง€์šด 2, 3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹Œ ๊ฐ’๋“ค ์ค‘ ์ตœ์†Œ๊ฐ’์ธ 5์˜ ๋ฐฐ์ˆ˜๋ฅผ ์ง€์šด๋‹ค.
5. ์ด ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ ๊ฑธ๋Ÿฌ์ง€๋Š” ๊ฒƒ๋“ค์ด ๋น„๋กœ์†Œ ์†Œ์ˆ˜๊ฐ€ ๋œ๋‹ค.
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

์ด๋Ÿฌํ•œ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋Š” "ํŠน์ • ๊ตฌ๊ฐ„์ด ์ฃผ์–ด์กŒ์„ ๋•Œ" ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ์†Œ์ˆ˜๋ฅผ ๋„์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
์œ„์˜ 2581๋ฒˆ์ฒ˜๋Ÿผ ํ’€๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(n)์œผ๋กœ ๋น„๊ต์  ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฐ๋‹ค.
ํ•˜์ง€๋งŒ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(nlog(log n))์œผ๋กœ ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ๋„์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
๋˜ํ•œ ์•ฝ์ˆ˜์˜ ๊ฒฝ์šฐ, 2*4, 4*2 ์ฒ˜๋Ÿผ ๋ฐ˜๋ณต์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์—
๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ ๋ฒ”์œ„์˜ ๋งˆ์ง€๋ง‰ ์ˆ˜์˜ ๋ฃจํŠธ ๊ฐ’๊นŒ์ง€๋งŒ ๋ฐ˜๋ณตํ•จ์œผ๋กœ ์‹œ๊ฐ„์„ ๋”์šฑ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค!


๐Ÿค” Algorithm ๊ณผ์ • 
์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ์ •์€ ์œ„์—์„œ ์„ค๋ช…ํ•˜๋Š” ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ๋ฐฉ์‹์„ ์ฐจ์šฉํ•˜์˜€๋‹ค.
๋˜ํ•œ ์ฝ”๋“œ์— ๋Œ€ํ•œ ์„ค๋ช…์€ ์•„๋ž˜ ๊ทธ๋ฆผ์œผ๋กœ ๋Œ€์ฒดํ•˜๊ฒ ๋‹ค.

๐Ÿคซ  solution_1929

m, n = map(int, input().split())
num = [i for i in range(max(2, m), n+1)]  # m ~ n์‚ฌ์ด ์ˆซ์ž๋ฅผ ๋„ฃ์€ ๋ฆฌ์ŠคํŠธ (๋‹จ, 1์€ ์ œ์™ธ)
dec_sqr_n = []   # n์˜ ์ œ๊ณฑ๊ทผ๊นŒ์ง€์˜ ๋ฒ”์œ„์•ˆ์—์„œ ์†Œ์ˆ˜๋ฅผ ์ง‘์–ด ๋„ฃ์„ ๋ฆฌ์ŠคํŠธ

for i in range(2, int(n**0.5) + 1):   # 2์—์„œ n์˜ ์ œ๊ณฑ๊ทผ๊นŒ์ง€๋งŒ ๋ฐ˜๋ณต
    if i in num:
        dec_sqr_n.append(i)
    num = [j for j in num if j % i != 0]

print(*(dec_sqr_n + num), sep = "\n")

 

 

 

๐Ÿง ๋ฐฑ์ค€ 4948 (์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 

๐Ÿค” ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด (Sieve of Eratosthenes)

๐Ÿค” Algorithm ๊ณผ์ • 
์œ„ 1929์™€ ๊ฑฐ์˜ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ์ง€๋งŒ ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
1. n์˜ ๋ฒ”์œ„ ์‚ฌ์ด์˜ ์ž์—ฐ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” num ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. (1 ≤ n ≤ 123456)
2. ๋ฆฌ์ŠคํŠธ num์„ 1929๋ฒˆ ํ’€์ด์ฒ˜๋Ÿผ ์†Œ์ˆ˜๋งŒ ์กด์žฌํ•˜๋„๋ก ๋งŒ๋“ค์–ด์ค€๋‹ค.
3. 1929์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ n์˜ ๋ฒ”์œ„์‚ฌ์ด์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ์†Œ์ˆ˜๊ฐ€ ๋‹ด๊ธด decimal์ด๋ผ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.
4. 0์ด ๋“ค์–ด์˜ค๊ธฐ ์ „๊นŒ์ง€ while๋ฌธ์„ ์ด์šฉํ•ด ์ž…๋ ฅ์„ ๋ฐ›์•„์ฃผ๊ณ 
5. decimal ๋ฆฌ์ŠคํŠธ์—์„œ ์ž…๋ ฅ์˜ ๋ฒ”์œ„์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด ์ถœ๋ ฅํ•ด์ค€๋‹ค.

 

๐Ÿคซ  solution_4948 (์‹œ๊ฐ„์ดˆ๊ณผ)

def decimal(N):
    n = N+1
    num = [i for i in range(n, 2*N + 1)]
    dec_half = []
    for i in range(2, int((2*N)**0.5) + 1):
        if i in num:
            dec_half.append(i)
        num = [j for j in num if j % i != 0]
    return len(dec_half+num)

num = -1
while True:
    num = int(input())
    if num == 0:
        break
    else:
        print(decimal(num))

 

์ฒ˜์Œ ์ง  ์ฝ”๋“œ๋กœ 1929์™€ ๋น„์Šทํ•˜๊ฒŒ ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํ’€์—ˆ์œผ๋‚˜ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.
์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์—ผ๋‘ํ•˜์ง€ ์•Š๊ณ  ๋จธ๋ฆฌ๋ฅผ ์žฅ์‹์œผ๋กœ ๋‘๊ณ  ๋ฌธ์ œ์— ์ ‘๊ทผํ•œ ๊ฒฐ๊ณผ๋ผ ์ƒ๊ฐํ•  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.
ํ•จ์ˆ˜์—์„œ 2์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์„ while๋ฌธ์—์„œ ๊ณ„์† ๋ฐ›์œผ๋‹ˆ ๊ทธ๋Ÿด ์ˆ˜ ๋ฐ–์—...


๐Ÿคซ  solution_4948

from math import sqrt

# 1 <= n <= 123456 ์‚ฌ์ด ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
num = [i for i in range(2, 123456 * 2)]

# ๋ฆฌ์ŠคํŠธ num์„ ์ด์ „์ฒ˜๋Ÿผ ์†Œ์ˆ˜๋งŒ ์žˆ๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๊ธฐ.
dec_half = []
for i in range(2, int(sqrt(123456*2)) + 1):
    if i in num:
        dec_half.append(i)
    num = [j for j in num if j%i != 0]

decimal = dec_half + num

n = -1
while True:
    n = int(input())
    if n == 0:
        break
    # decimal ๋ฆฌ์ŠคํŠธ์—์„œ ์ž…๋ ฅ์˜ ๋ฒ”์œ„์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’๋“ค์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด ์ถœ๋ ฅ
    print(len([i for i in decimal if n < i <= 2*n]))

 

๐Ÿง ๋ฐฑ์ค€ 9020 (์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด)

๐Ÿคซ ํ•ด๊ฒฐ์˜ ์‹ค๋งˆ๋ฆฌ 

๐Ÿค” ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด (Sieve of Eratosthenes)

๐Ÿค” Algorithm ๊ณผ์ • 
๋‘ ์†Œ์ˆ˜์˜ ์ฐจ์ด๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ๊ฒƒ์˜ ์˜๋ฏธ๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
์†Œ์ˆ˜๊ฐ„์˜ ์ฐจ์ด๊ฐ€ ์ž‘์„์ˆ˜๋ก ์ตœ๋Œ€๊ฐ’์˜ ์ ˆ๋ฐ˜๊ณผ ๊ฐ€๊นŒ์›Œ์ง„๋‹ค! 
16์œผ ์˜ˆ๋กœ ๋“ค์ž๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
[3, 13], [5, 11]  =>  3  5  (8)  11  13

๋”ฐ๋ผ์„œ ํ•ต์‹ฌ์€ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๊ฑธ๋ฆฌ์ง€ ์•Š์œผ๋ ค๋ฉด ์†Œ์ˆ˜์˜ ์ ˆ๋ฐ˜๋งŒ ํ›‘์–ด์•ผ ํ•œ๋‹ค.

 

๐Ÿคซ  solution_9020 (์‹œ๊ฐ„์ดˆ๊ณผ)

T = int(input())
n = [int(input()) for _ in range(T)]

num = [i for i in range(2, 10001)]
dec_half = []
for i in range(2, 100 + 1):
    if i in num:
        dec_half.append(i)
    num = [j for j in num if j % i != 0]
decimal = dec_half + num

for i in n:
    dec = []
    for j in range(len(decimal)):
        for k in range(j, len(decimal)):
            if i == decimal[j] + decimal[k]:
                dec.append([decimal[j], decimal[k]])
    print(*dec[-1])

์ฒ˜์Œ ์ง  ์ฝ”๋“œ๋กœ ๋ฌธ์ œ์ ์€ ์†Œ์ˆ˜์˜ ์ „์ฒด๋ฅผ ๊ณ„์† ํ›‘์œผ๋ฉด์„œ ๋น„๊ต๋ฅผ ์ง„ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋ณด๋‹ค 2๋ฐฐ ์ด์ƒ์˜ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค.
๋จผ์ € ์œ„์˜ ์ฝ”๋“œ๋ฅผ ์ง  ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
1. ๋จผ์ € ๋ฒ”์œ„์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์†Œ์ˆ˜๋ฅผ ๋‹ด๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  
2. ๊ทธ ๋ฆฌ์ŠคํŠธ ์•ˆ์—์„œ ๋‘ ์†Œ์ˆ˜๊ฐ„์˜ ๋ง์…ˆ์ด ์ž…๋ ฅ๊ฐ’๊ณผ ๋™์ผํ•œ ๊ฒƒ์„ ์ฐพ์•„์„œ dec์ด๋ผ๋Š” ๋ฆฌ์ŠคํŠธ์— ๋„ฃ๋Š”๋‹ค.
3. dec์—๋Š” [[3, 5]], [[3, 7], [5, 5]], [[3, 13], [5, 11]] ์ฒ˜๋Ÿผ ๋“ค์–ด๊ฐ„ ๊ฒƒ์ด๋ฏ€๋กœ 
4. ๊ฐ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฐจ์ด๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์†Œ์ˆ˜ ๋ฆฌ์ŠคํŠธ์ธ๊ฒƒ.
์ด๋ฅผ for๋ฌธ์„ ์ด์šฉํ•ด ์ถœ๋ ฅํ•˜์˜€๋‹ค.


๐Ÿคซ  solution_9020

num = [i for i in range(2, 10000 + 1)]
dec_half = []
for i in range(2, 100 + 1):
    if i in num:
        dec_half.append(i)
    num = [j for j in num if j%i != 0]
decimal = dec_half + num

T = int(input())
for i in range(T):
    n = int(input())
    n_half = n // 2
    for j in range(n_half, 1, -1):
        if j in decimal and n - j in decimal:
            print(j, n - j)
            break

 

+ Recent posts