๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์  ์ฐ๊ธฐ

๐ŸŽฎinspirer9 2023. 2. 14. 23:07
728x90
๋ฐ˜์‘ํ˜•

์ข€ ์‰ฌ์›Œ๋ณด์ด๋Š” ๋ฌธ์ œ๋ฅผ ๊ณจ๋ผ๋ณด์•˜๋‹ค.

1/4 ํฌ๊ธฐ๋กœ ์›์„ ๊ทธ๋ฆฌ๋ฉด ๊ทธ ์•ˆ์— k ๋ฐฐ์ˆ˜ ๋‹จ์œ„๋กœ ์ฐ์„ ์ˆ˜ ์žˆ๋Š” ์ ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ. ๊ปŒ์ด๋„ค...

๋ฌธ์ œ ์„ค๋ช…

์ขŒํ‘œํ‰๋ฉด์„ ์ข‹์•„ํ•˜๋Š” ์ง„์ˆ˜๋Š” x์ถ•๊ณผ y์ถ•์ด ์ง๊ตํ•˜๋Š” 2์ฐจ์› ์ขŒํ‘œํ‰๋ฉด์— ์ ์„ ์ฐ์œผ๋ฉด์„œ ๋†€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ง„์ˆ˜๋Š” ๋‘ ์–‘์˜ ์ •์ˆ˜ k, d๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ์„ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค.

  • ์›์ (0, 0)์œผ๋กœ๋ถ€ํ„ฐ x์ถ• ๋ฐฉํ–ฅ์œผ๋กœ a*k(a = 0, 1, 2, 3 ...), y์ถ• ๋ฐฉํ–ฅ์œผ๋กœ b*k(b = 0, 1, 2, 3 ...)๋งŒํผ ๋–จ์–ด์ง„ ์œ„์น˜์— ์ ์„ ์ฐ์Šต๋‹ˆ๋‹ค.
  • ์›์ ๊ณผ ๊ฑฐ๋ฆฌ๊ฐ€ d๋ฅผ ๋„˜๋Š” ์œ„์น˜์—๋Š” ์ ์„ ์ฐ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, k๊ฐ€ 2, d๊ฐ€ 4์ธ ๊ฒฝ์šฐ์—๋Š” (0, 0), (0, 2), (0, 4), (2, 0), (2, 2), (4, 0) ์œ„์น˜์— ์ ์„ ์ฐ์–ด ์ด 6๊ฐœ์˜ ์ ์„ ์ฐ์Šต๋‹ˆ๋‹ค.

์ •์ˆ˜ k์™€ ์›์ ๊ณผ์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ d๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ ์ด ์ด ๋ช‡ ๊ฐœ ์ฐํžˆ๋Š”์ง€ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ k ≤ 1,000,000
  • 1 ≤ d ≤ 1,000,000

์ž…์ถœ๋ ฅ ์˜ˆ

k d result
2 4 6
1 5 26

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ๋ณธ๋ฌธ์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • (0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (4, 0), (4, 1), (4, 2), (4, 3), (5, 0) ์œ„์น˜์— ์ ์„ ์ฐ์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด 26๊ฐœ ์ž…๋‹ˆ๋‹ค.

 

1์ฐจ ์‹œ๋„

  • ์‹œ๊ฐ„ ์ดˆ๊ณผ ใ…‹ใ…‹ใ…‹
  • ๊ทธ์น˜, ์ด๊ฒŒ ๋งž์ง€. ์ด๋ ‡๊ฒŒ ์‰ฌ์šธ๋ฆฌ ์—†์ง€.
from collections import Counter

def solution(k, d):
    s = []
    a = d // k + 1
    q = d ** 2
    for i in range(0,a*k,k):
        for j in range(i,a*k,k):
            t = i**2 + j **2
            if t <= q:
                s.append((i,j))
    ss = []
    for i in s:
        ss.append((i[1],i[0]))
    
    s = s + ss
    c = Counter(s)
    
    return len(c)
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.05ms, 10MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (6453.82ms, 1.23GB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1848.45ms, 380MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (2221.76ms, 553MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 11 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 14 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
  • ์ ์„ ์ง์ ‘ ์ฐ์œผ๋ฉด์„œ ๊ณ„์‚ฐํ•˜๋ฉด ์•ˆ๋˜๊ณ , ์› ์•ˆ์— ์ฐ์„ ์ˆ˜ ์žˆ๋Š” ์ ์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.
    • ์‚ฝ์ž… ์‚ญ์ œ๋„ ํ•˜์ง€๋ง๊ณ  ๊ทธ๋ƒฅ ์นด์šดํŠธ๋งŒ ํ•ด์•ผํ•  ๋“ฏ?

  • ์˜›๋‚ ์— ํ–ˆ๋˜ "Bresenham ์•Œ๊ณ ๋ฆฌ์ฆ˜"์—์„œ ์› ๊ทธ๋ฆฌ๊ธฐ ํ–ˆ๋˜ ๊ฑฐ๋ž‘ ๋น„์Šทํ•˜๋„ค...
    • ๋ธŒ๋ ˆ์ฆŒํ–„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ปดํ“จํ„ฐ ๊ทธ๋ž˜ํ”ฝ์Šค์—์„œ ๋ณต์žกํ•˜๊ณ  ๊ณ„์‚ฐ์„ ๋Š๋ฆฌ๊ฒŒ ๋งŒ๋“œ๋Š” ์‹ค์ˆ˜ ๊ณ„์‚ฐ์„ ๋ฐฐ์ œํ•˜๊ณ  ์ •์ˆ˜ ๊ณ„์‚ฐ๋งŒ์œผ๋กœ ์ง์„ ์„ ๊ทธ๋ฆฌ๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž…๋‹ˆ๋‹ค. ์ง์„ ์˜ ๊ณต์‹์„ ์ด์šฉํ•ด ๊ณ„์‚ฐ๋œ ์ขŒํ‘œ๊ฐ’์€ ๊ฒฐ๊ตญ ์Šคํฌ๋ฆฐ์— ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์†Œ์ˆ˜์  ์ดํ•˜๋ฅผ ๋ฒ„๋ฆผํ•œ๋‹ค๋˜์ง€ ๋ฐ˜์˜ฌ๋ฆผ ํ•ด์„œ ์ •์ˆ˜๋กœ ๋งŒ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  • ์›์„ ๊ทธ๋ฆด ๋•Œ, ์ˆ˜ํ‰ ์ˆ˜์ง์œผ๋กœ ์„ ์„ ๊ธ‹๋Š” ๋ฐฉ์‹์ด ์žˆ์—ˆ๋Š”๋ฐ ๊ธฐ์–ต ์•ˆ๋‚จ.
  • ๋„“์ด๋กœ ๋Œ€์ถฉ ๋‚˜๋ˆ„๋ฉด? ์•ˆ๋จ ใ…‹ใ…‹
import math

def solution(k, d):
    S = math.pi * d ** 2 / 4
    box = k**2
    answer = S // box
    print(S, box, answer)
    return answer
  • ๋‚„๋‚„... ์‰ฝ๊ฒŒ ๋ ๋ฆฌ๊ฐ€ ์—†์ง€ ใ…ก.ใ…ก;
  • ์›์˜ ํ…Œ๋‘๋ฆฌ๋งŒ ๊ณ„์‚ฐํ•˜๋ฉด ๋˜์ง€ ์•Š๋‚˜?
import math

def solution(k, d):
    answer = 0
    a = d//k + 1
    for i in range(a):
        x = i * k
        y = math.sqrt(d**2 - x**2)
        c = y//k
        answer += c + 1
        #print("x:",x,"y:",y, "c:", c)
    
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (2.23ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1.22ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (3.61ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (3.10ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (1.34ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (30.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (3.45ms, 10MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (6.45ms, 10.2MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (738.36ms, 10.4MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (362.22ms, 9.98MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (228.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ, math๋ฅผ ์ž„ํฌํŠธํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ๊ตฐ์š”.
  • ๋‚œ ์™œ math๋ฅผ ์ž„ํฌํŠธํ–ˆ์ง€? ... ์•„... ์›๋ฉด์  ๊ณ„์‚ฐํ•ด์„œ ๋‚˜๋ˆ„๋ ค๊ณ  pi ๋•Œ๋ฌธ์— ใ…Žใ…Žใ…Ž
def solution(k, d):
    c = 0
    for y in range(0, d, k):
        x = (d**2 - y**2)**0.5
        c += x//k
    return c + d//k + 1
  • ์‰ฝ๋„ค? (30๋ถ„ ๋„˜๊ฒŒ ๊ฑธ๋ฆผ)
728x90
๋ฐ˜์‘ํ˜•