๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 6359 - ๋งŒ์ทจํ•œ ์ƒ๋ฒ”

by YWTechIT 2021. 6. 18.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 6359 - ๋งŒ์ทจํ•œ ์ƒ๋ฒ”

๋ฐฑ์ค€ 6359 - ๋งŒ์ทจํ•œ ์ƒ๋ฒ”


โšก๏ธ ๋‚˜์˜ ํ’€์ด

์–ธ๋œป ๋ณด๋ฉด ์ €๋ฒˆ์— ํ’€์—ˆ๋˜ ๋ฐฑ์ค€ 1244 - ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ๋ฐ, ๋ฒ”์œ„ ์ „์ฒด๋ฅผ ์‚ดํŽด๋ณด๋ฉด์„œ ์ƒํƒœ๋ฅผ ๋ฐ”๊พผ๋‹ค๋Š” ์ ์ด ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค. (1244 ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ n์˜ ๋ฐฐ์ˆ˜๋งŒ ์ƒํƒœ๋ฅผ ๋ฐ”๊พธ๋ฉด ๋์Œ)

 

์ฃผ์–ด์ง„ ๋ฌธ์ œ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ ์ƒ๊ฐ์„ ์ž˜ ๋ชปํ–ˆ๋Š”๋ฐ 1 ~ 2 ๋ผ์šด๋“œ๋„ 3 ~ k ๋ผ์šด๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์—ด๋ ค์žˆ์œผ๋ฉด ์ž ๊ทธ๊ณ , ์ž ๊ฒจ์žˆ๋‹ค๋ฉด ์—ฌ๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด๋„ ๋‹ต์€ ๊ฐ™๊ฒŒ ๋‚˜์˜จ๋‹ค.


1, 0 ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ€ ๋•Œ ๐Ÿฏ tip์ธ๋ฐ if not gate[j]: gate[j] = 1 or if gate[j]: gate[j] = 0 ๋Œ€์‹  gate[j] = (gate[j] + 1) % 2๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์ง€๋งŒ ์ฝ”๋“œ๋Š” ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค. ์ด์ „ ์— ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์ด๋‹ค.

 

๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ๊นŒ ๊ฒฝ์•…์„ ๊ธˆ์น˜ ๋ชปํ–ˆ๋Š”๋ฐ ์ œ๊ณฑ๊ทผ์œผ๋กœ ๊ตฌํ–ˆ๋‹ค.(์–ด๋–ป๊ฒŒ ์ €๋Ÿฐ ์ƒ๊ฐ์„.. ๐Ÿคญ ๐Ÿคญ) 1 ~ 3: 1, 4 ~ 9: 2 ์ด๋Ÿฐ ๊ทœ์น™์„ ์ž˜ ๋ดค์–ด์•ผํ•˜๋Š”๋ฐ ์•„์‰ฝ๋‹ค!

 

์ฒ˜์Œ ํ‘ผ ํ๋ฆ„

  1. n๋งŒํผ ์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค. (-3333์€ index๋ฅผ ์‰ฝ๊ฒŒ ๊ณ„์‚ฐํ•˜๋ ค๊ณ  ๋„ฃ์—ˆ๋‹ค.)
  2. 1 ~ 2 ๋ผ์šด๋“œ์—์„œ 2์˜ ๋ฐฐ์ˆ˜๋งŒ ์ƒํƒœ๋ฅผ ๋ฐ”๊พผ๋‹ค.
  3. 3 ~ n ๋ผ์šด๋“œ๊นŒ์ง€ i์˜ ๋ฐฐ์ˆ˜ ์ž๋ฆฌ๋งŒ ์ƒํƒœ๋ฅผ ๋ฐ”๊พผ๋‹ค.

 

๋‚˜์ค‘์— ํ‘ผ ํ๋ฆ„

  1. n๋งŒํผ ์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค. (-3333์€ index๋ฅผ ์‰ฝ๊ฒŒ ๊ณ„์‚ฐํ•˜๋ ค๊ณ  ๋„ฃ์—ˆ๋‹ค.)
  2. 1 ~ n ๋ผ์šด๋“œ๊นŒ์ง€ i์˜ ๋ฐฐ์ˆ˜ ์ž๋ฆฌ๋งŒ ์ƒํƒœ๋ฅผ ๋ฐ”๊พผ๋‹ค.

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ

  1. ์ œ๊ณฑ๊ทผ์„ ๊ตฌํ•œ๋‹ค. int(n**0.5)
# ๋‚˜์˜ ์ฝ”๋“œ
T = int(input())

def game(n):
    gate = [-3333]    # declare garbage value for easy count index
    gate += [1] * n    # 0: close, 1: open

    for i in range(1, n + 1):    # 1 ~ 2 round
        if not i % 2:
            gate[i] = 0

    for i in range(3, n + 1):    # 3 round ~ n round
        for j in range(i, n + 1, i):  
            gate[j] = (gate[j] + 1) % 2

    return gate.count(1)    # count open gate

for _ in range(T):
    n = int(input())
    print(game(n))
# ๋‹จ์ถ• ์ฝ”๋“œ
T = int(input())

def game(n):
    gate = [-3333]    # declare garbage value for easy count index
    gate += [1] * n    # 0: open, 1: closed

    for i in range(1, n + 1):
        for j in range(i, n + 1, i): 
            gate[j] = (gate[j] + 1) % 2
        print(gate)

    return gate.count(0)    # count open gate

for _ in range(T):
    n = int(input())
    print(game(n))
# ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ
T = int(input())
for i in range(T):
    n = int(input())
    print(int(n ** 0.5))
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€