[BOJ][๐ŸŸก3][๋ฐฑ์ค€#01644] ์†Œ์ˆ˜์˜ ์—ฐ์†ํ•ฉ

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #1644


๋ฌธ์ œ

ํ•˜๋‚˜ ์ด์ƒ์˜ ์—ฐ์†๋œ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ์ž์—ฐ์ˆ˜๋“ค์ด ์žˆ๋‹ค. ๋ช‡ ๊ฐ€์ง€ ์ž์—ฐ์ˆ˜์˜ ์˜ˆ๋ฅผ ๋“ค์–ด ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

3 : 3 (ํ•œ ๊ฐ€์ง€) 41 : 2+3+5+7+11+13 = 11+13+17 = 41 (์„ธ ๊ฐ€์ง€) 53 : 5+7+11+13+17 = 53 (๋‘ ๊ฐ€์ง€)

ํ•˜์ง€๋งŒ ์—ฐ์†๋œ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์—†๋Š” ์ž์—ฐ์ˆ˜๋“ค๋„ ์žˆ๋Š”๋ฐ, 20์ด ๊ทธ ์˜ˆ์ด๋‹ค. 7+13์„ ๊ณ„์‚ฐํ•˜๋ฉด 20์ด ๋˜๊ธฐ๋Š” ํ•˜๋‚˜ 7๊ณผ 13์ด ์—ฐ์†์ด ์•„๋‹ˆ๊ธฐ์— ์ ํ•ฉํ•œ ํ‘œํ˜„์ด ์•„๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•œ ์†Œ์ˆ˜๋Š” ๋ฐ˜๋“œ์‹œ ํ•œ ๋ฒˆ๋งŒ ๋ง์…ˆ์— ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, 3+5+5+7๊ณผ ๊ฐ™์€ ํ‘œํ˜„๋„ ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค. ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์ž์—ฐ์ˆ˜๋ฅผ ์—ฐ์†๋œ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 โ‰ค N โ‰ค 4,000,000)


์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N์„ ์—ฐ์†๋œ ์†Œ์ˆ˜์˜ ํ•ฉ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์˜ˆ์ œ 1

์ž…๋ ฅ

20


์ถœ๋ ฅ

0


์˜ˆ์ œ 2

์ž…๋ ฅ

3


์ถœ๋ ฅ

1


์˜ˆ์ œ 3

์ž…๋ ฅ

41


์ถœ๋ ฅ

3


์˜ˆ์ œ 4

์ž…๋ ฅ

53


์ถœ๋ ฅ

2


My Sol

import sys
input = sys.stdin.readline

def is_prime(M):
    if M <= 10:
        return base_primes[M]
    for i in range(2, int(M ** (1 / 2)) + 1):
        if not M % i: return 0
    return 1

def next_prime(M):
    while not is_prime(M):
        M += 1
    return M

def make_primes(M):
    l = 0
    arr = [1] * M
    primes = []
    for i in range(2, int(M ** (1 / 2)) + 1):
        if arr[i]:
            l += 1
            primes.append(i)
            for j in range(i * 2, M, i):
                arr[j] = 0

    for i in range(int(M ** (1 / 2)) + 1, M):
        if arr[i]:
            l += 1
            primes.append(i)

    return primes, l

base_primes = [0, 0, 1, 1, 0, 1, 0, 1, 0, 0 ,0]
M = int(input())
primes, l = make_primes(M)

primes.append(next_prime(M))
l += 1

accs = [0]
for p in primes:
    accs.append(accs[-1]+p)

def main(N):
    s, e = 0, 1
    cnt = 0
    while e <= l:
        if s >= e:
            e += 1
            continue

        diff = accs[e] - accs[s]
        # ๋ˆ„์ ํ•ฉ์ด N๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ
        if diff < N:
            e += 1

        # ๋ˆ„์ ํ•ฉ์ด N๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
        elif diff > N:
            s += 1

        # ๋ˆ„์ ํ•ฉ์ด N์ธ ๊ฒฝ์šฐ
        else:
            cnt += 1
            e += 1
            s += 1
    return cnt

cnt = main(M)
if is_prime(M) and primes and primes[-1] < M:
    cnt += 1
print(cnt)

์†Œ์ˆ˜ ํŒ์ •๊ณผ ํˆฌํฌ์ธํ„ฐ๋ฅผ ์œตํ•ฉํ•˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์šฐ์„  ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด ์†Œ์ˆ˜๋ฅผ ํŒ์ •ํ•˜๋Š”๋ฐ, ์ž…๋ ฅ๋œ ์ˆ˜ M๊นŒ์ง€ ์†Œ์ˆ˜ํŒ์ •์„ ๋ชจ๋‘ ํ•ด์ฃผ์—ˆ๋‹ค.

next_prime ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด M ์ดํ›„์˜ ์†Œ์ˆ˜ ํ•˜๋‚˜๊นŒ์ง€ ์†Œ์ˆ˜๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ ๊ฒƒ์€ M์„ ๋„˜๊ธด ์ฒซ ์†Œ์ˆ˜๊ฐ€ ์ด์ „ ์†Œ์ˆ˜์™€์˜ ํ•ฉ์ด M์ด ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•œ ๊ฒƒ์ด๋‹ค.

์ด ์†Œ์ˆ˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์™„์„ฑ๋˜๋ฉด, accs ๋ผ๋Š” ์†Œ์ˆ˜ ๋ˆ„์ ํ•ฉ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ , ์‹œ์ž‘ ์ธ๋ฑ์Šค์™€ ๋ ์ธ๋ฑ์Šค๋ฅผ ๋‚˜๋ˆ„์–ด ํˆฌํฌ์ธํŠธ๋ฅผ ๋Œ๋ฆฌ๋ฉฐ ๊ฐ’์˜ ์ฐจ๋ฅผ M๊ณผ ๋น„๊ตํ•˜๋ฉฐ ํฌ์ธํ„ฐ๋ฅผ ์˜ฎ๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค.

์ดํ›„ M ์ž์ฒด๊ฐ€ ์†Œ์ˆ˜์ด๋ฉฐ, ์†Œ์ˆ˜ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๊ฐ’์ด M๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ์— cnt๋ฅผ 1 ์ถ”๊ฐ€ํ•ด์ค€๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๊ทธ ์†Œ์ˆ˜ ํ•˜๋‚˜ ์ž์ฒด๊ฐ€ ์นด์šดํŠธ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

import sys

num = int(sys.stdin.readline())


def range_prime(n):
    m = int(n ** 0.5) + 1
    s = [1] * (n + 1)
    for i in range(2,m):
        if s[i] == 1:
            for j in range(i + i,n + 1,i):
                s[j] = 0
    return [i for i in range(2,n + 1) if s[i] == 1]


left, answer, acc = 0, 0, 0
lst = range_prime(num)
for i in range(len(lst)):
    acc += lst[i]
    if acc == num:
        answer += 1
    elif acc > num:
        while acc > num:
            acc -= lst[left]
            left += 1
        if acc == num:
            answer += 1

print(answer)

์—ฐ์‚ฐ์‹œ๊ฐ„์€ ๋‚˜๋ณด๋‹ค ์†Œํญ ๊ธธ์ง€๋งŒ, ๋ฌธ์ œ ํ’€์ด๊ฐ€ ๊ฝค๋‚˜ ์งง์€ ํ’€์ด๊ฐ€ ์žˆ์–ด ๋ถ„์„ํ•˜๊ณ ์ž ํ•œ๋‹ค.

๋ˆ„์ ํ•ฉ ๋ฆฌ์ŠคํŠธ๋Š” ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๋งค๋ฒˆ ์†Œ์ˆ˜ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ’์„ ๋”ํ•˜๊ณ  ๋นผ๋ฉฐ ๋น„๊ตํ•˜๊ณ ์ž ํ•˜๋Š” num๊ณผ ๋น„๊ต ๊ฒฐ๊ณผ๋ฅผ answer์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์ธ์ƒ์ ์ธ ๋ถ€๋ถ„์€ ๋งค ๋ฐ˜๋ณต๋งˆ๋‹ค lst[i]๋ฅผ acc์— ๋”ํ•ด์ฃผ๋ฉฐ ์™ผ์ชฝ ์ธ๋ฑ์Šค๋ฅผ acc๊ฐ€ num๋ณด๋‹ค ํฐ ๋™์•ˆ ๊ณ„์† ์ด๋™์‹œ์ผœ ์ฃผ๋Š” ๋ฐฉ์‹์„ ํ™œ์šฉํ•˜์˜€๋‹ค.

๊ทธ๋ฆฌ๊ณ  prime list๋ฅผ returnํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ list comprehension์„ ์ด์šฉํ•ด ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜์…จ๋‹ค. ์ข‹์€ ๋กœ์ง์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ