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

The only one you can truly trust is yourself.

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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์–ต์–ต๋‹จ์„ ์™ธ์šฐ์ž

๐ŸŽฎinspirer9 2023. 2. 22. 01:01
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์˜์šฐ๋Š” ์ฒœํ•˜์ œ์ผ ์•”์‚ฐ๋Œ€ํšŒ๋ฅผ ์•ž๋‘๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์•”์‚ฐ๋ณด๋‹ค๋Š” ์•”๊ธฐ์— ์ผ๊ฐ€๊ฒฌ์ด ์žˆ๋Š” ์˜์šฐ๋Š” ๊ตฌ๊ตฌ๋‹จ์„ ํ™•์žฅํ•˜์—ฌ ์–ต์–ต๋‹จ์„ ๋งŒ๋“ค๊ณ  ์™ธ์›Œ๋ฒ„๋ฆฌ๊ธฐ๋กœ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์–ต์–ต๋‹จ์€ 1์–ต x 1์–ต ํฌ๊ธฐ์˜ ํ–‰๋ ฌ์ž…๋‹ˆ๋‹ค. ์–ต์–ต๋‹จ์„ ์™ธ์šฐ๋˜ ์˜์šฐ๋Š” ์นœ๊ตฌ ์ˆ˜์—ฐ์—๊ฒŒ ํ€ด์ฆˆ๋ฅผ ๋‚ด๋‹ฌ๋ผ๊ณ  ๋ถ€ํƒํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ˆ˜์—ฐ์€ ํ‰๋ฒ”ํ•˜๊ฒŒ ๋ฌธ์ œ๋ฅผ ๋‚ด๋ด์•ผ ์˜์šฐ๊ฐ€ ๋„ˆ๋ฌด ์‰ฝ๊ฒŒ ๋งžํžˆ๊ธฐ ๋•Œ๋ฌธ์— ์ข€ ์–ด๋ ต๊ฒŒ ํ€ด์ฆˆ๋ฅผ ๋‚ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ ๋‹นํ•œ ์ˆ˜ e๋ฅผ ๋จผ์ € ์ •ํ•˜์—ฌ ์•Œ๋ ค์ฃผ๊ณ  e ์ดํ•˜์˜ ์ž„์˜์˜ ์ˆ˜ s๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์–˜๊ธฐํ•ฉ๋‹ˆ๋‹ค. ์˜์šฐ๋Š” ๊ฐ s์— ๋Œ€ํ•ด์„œ s๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ  e ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ ์ค‘์—์„œ ์–ต์–ต๋‹จ์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ์ˆ˜๋ฅผ ๋‹ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๊ฐ€์žฅ ๋งŽ์ด ๋“ฑ์žฅํ•œ ์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๋ฉด ๊ทธ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ๋‹ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ˆ˜์—ฐ์€ ์˜์šฐ๊ฐ€ ์ •๋‹ต์„ ๋งํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋‹น์‹ ์—๊ฒŒ ํ”„๋กœ๊ทธ๋žจ ์ œ์ž‘์„ ์˜๋ขฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. e์™€ s์˜ ๋ชฉ๋ก starts๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ ํ€ด์ฆˆ์˜ ๋‹ต ๋ชฉ๋ก์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ e ≤ 5,000,000
  • 1 ≤ starts์˜ ๊ธธ์ด ≤ min {e,100,000}
  • 1 ≤ starts์˜ ์›์†Œ ≤ e
  • starts์—๋Š” ์ค‘๋ณต๋˜๋Š” ์›์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

e starts result
8 [1,3,7] [6,6,8]

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

์–ต์–ต๋‹จ์—์„œ 1 ~ 8์ด ๋“ฑ์žฅํ•˜๋Š” ํšŸ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1๋ฒˆ ๋“ฑ์žฅ : 1
2๋ฒˆ ๋“ฑ์žฅ : 2, 3, 5, 7
3๋ฒˆ ๋“ฑ์žฅ : 4
4๋ฒˆ ๋“ฑ์žฅ : 6, 8

[1, 8] ๋ฒ”์œ„์—์„œ๋Š” 6๊ณผ 8์ด ๊ฐ๊ฐ 4๋ฒˆ์”ฉ ๋“ฑ์žฅํ•˜์—ฌ ๊ฐ€์žฅ ๋งŽ์€๋ฐ 6์ด ๋” ์ž‘์€ ์ˆ˜์ด๋ฏ€๋กœ 6์ด ์ •๋‹ต์ž…๋‹ˆ๋‹ค.
[3, 8] ๋ฒ”์œ„์—์„œ๋„ ์œ„์™€ ๊ฐ™์œผ๋ฏ€๋กœ 6์ด ์ •๋‹ต์ž…๋‹ˆ๋‹ค.
[7, 8] ๋ฒ”์œ„์—์„œ๋Š” 7์€ 2๋ฒˆ, 8์€ 4๋ฒˆ ๋“ฑ์žฅํ•˜๋ฏ€๋กœ 8์ด ์ •๋‹ต์ž…๋‹ˆ๋‹ค.

ํ’€์ด

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

  • ๊ทผ๋ฐ ๋ฌธ์ œ ์ œํ•œ์‚ฌํ•ญ์„ ๋ณด๋ฉด ์–ต์–ต๋‹จ์ด๋ผ๊ธธ๋ž˜ ์–ตx์–ต์ธ ์ค„ ์•Œ์•˜๋Š”๋ฐ,
    • 1 ≤ e ≤ 5,000,000
    • e๋Š” 500๋งŒ๊นŒ์ง€๋‹ค. ์ฆ‰, ์–ต์–ต๋‹จ์ด ์•„๋‹ˆ๋ผ 5๋ฐฑ๋งŒx5๋ฐฑ๋งŒ๋‹จ
  • ๊ทธ๋ฆฌ๊ณ  starts ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” ์ตœ์†Œ 10๋งŒ.
    • 1 ≤ starts์˜ ๊ธธ์ด ≤ min {e,100,000}
    • 10๋งŒ๊ฐœ์˜ ๋ฐฐ์—ด์„ ์นด์šดํŒ…ํ•œ๋‹ค? ๋‹น๊ทผ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋‹ค.
  • ๊ทธ๋Ÿฌ๋ฉด ๋ฐฉ๋ฒ•์€ ๋ญ๋‹ค? ์ผ๋ฐ˜ํ•ญ์„ ์ฐพ๋Š” ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.
    • 1๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1
    • 2๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1,2x2
    • 3๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1,2x2,3x2
    • 4๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1,2x2,3x2,4x3
    • 5๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1,2x2,3x2,4x3,5x2
    • 6๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1,2x2,3x2,4x3,5x2,6x4
    • 7๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1,2x2,3x2,4x3,5x2,6x4,7x2
    • 8๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1,2x2,3x2,4x3,5x2,6x4,7x2,8x4
    • 9๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1,2x2,3x2,4x3,5x2,6x4,7x2,8x4,9x3
    • 10๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ˆ˜ : 1x1,2x2,3x2,4x3,5x2,6x4,7x2,8x4,9x3,10x4
  • ๊ทœ์น™์„ ๋ชจ๋ฅด๊ฒ ์–ด.... ใ… .ใ… 
    • 1, 2, 2, 3, 2, 4, 2, 4, 3, 4, ....
  • ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ณต์‹ ๊ฐ™์€ ๊ฑด ์—†๋‚˜...?
    • ๋ณผํ”„๋žŒ ์•ŒํŒŒ... ๋„์™€์ค˜์š”.

  • ์žˆ๋‹ค. ๋””๋ฆฌํด๋ ˆ ์ƒ์„ฑ ํ•จ์ˆ˜
 

๋””๋ฆฌํด๋ ˆ(Dirichlet) ํ•จ์ˆ˜ ๋˜๋Š” ์ฃผ๊ธฐ์  sinc ํ•จ์ˆ˜ - MATLAB diric - MathWorks ํ•œ๊ตญ

์ด ์˜ˆ์ œ์˜ ์ˆ˜์ •๋œ ๋ฒ„์ „์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ํŽธ์ง‘ํ•œ ๋‚ด์šฉ์„ ๋ฐ˜์˜ํ•˜์—ฌ ์ด ์˜ˆ์ œ๋ฅผ ์—ฌ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?

kr.mathworks.com

  • ํŒŒ์ด์ฌ ์ฝ”๋“œ๋„ ์žˆ์œผ๋‚˜ ๋””๊ฐ๋งˆ, ๋กœ๊ทธ๊ฐ๋งˆ ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
 

[Python] ๋””๋ฆฌํด๋ ˆ ๋ถ„ํฌ ์ถ”์ •ํ•˜๊ธฐ

ํ† ํ”ฝ ๋ชจ๋ธ๋ง ์ด๋ก ๋“ค์„ ๊ณต๋ถ€ํ•˜๋‹ค ๋ณด๋‹ˆ ์ข…์ข… ๊น์Šค ์ƒ˜ํ”Œ๋ง ์ดํ›„์— ๋””๋ฆฌํด๋ ˆ ๋ถ„ํฌ๋ฅผ ์ถ”์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š”๊ฑธ ๋ดค์—ˆ๋Š”๋ฐ, ๋งค๋ฒˆ ๋ด๋„ ์ž˜ ์ดํ•ด๋„ ๋ชปํ•˜๊ณ  ๊ณ„์† ๊นŒ๋จน๊ธธ๋ž˜ ์•„์˜ˆ ๊นŒ๋จน์ง€ ํฌ์ŠคํŒ…์„ ํ•˜๋‚˜

bab2min.tistory.com

  • ์ผ๋ฐ˜ ์ƒ์„ฑ ๊ธฐ๋Šฅ
    • ์—ฌ๊ธฐ๋„  ψ๊ธฐํ˜ธ์™€, (0) ๊ฐ™์€ ์˜๋ฏธ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ๋กœ ์˜ฎ๊ฒจ์•ผ ํ• ์ง€ ๋ชจ๋ฅด๊ฒ ์œผ๋‹ˆ... ๊ฑฐ์˜ ํฌ๊ธฐ ์ƒํƒœ.
    • sum_(n=0)^∞a_nx^n = (ψ_x^(0)(1) + log(1 - x))/log(x)
  • ๋„˜ํŒŒ์ด๋‚˜ ์‚ฌ์ดํŒŒ์ด๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  ํ•  ์ˆ˜ ์žˆ ๋ฐฉ๋ฒ•์€ ์—†์„๊นŒ?
    • ์ž ๊น? ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ๋„˜ํŒŒ์ด ์“ฐ๋Š” ์‚ฌ๋žŒ ๋ดค๋Š”๋ฐ? ํ ...
      • ๋””๋ฆฌํด๋ ˆ ํ™•๋ฅ  ๋ถ„ํฌ๋งŒ ๋˜๋„ค... ๊ทธ๋ƒฅ ๋žœ๋คํ•œ ๊ฐ’์ด ๋‚˜์˜จ๋‹ค.
    • ์•”ํŠผ ๊ณต๋ถ€๊ฐ€ ๋˜์—ˆ๋‹ค.
      • ์‹œ๊ทธ๋งˆ ์‹œํ€ธ์Šค, ํƒ€์šฐ ์‹œํ€ธ์Šค ๋“ฑ์œผ๋กœ ๋ถˆ๋ฆฌ๊ณ ,
      • ์ˆ˜์—ด An = DivisorSum[n, 1&]
      • ์ฆ‰, n์˜ ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜์—ด์ด๋ผ๊ณ  ํ•œ๋‹ค.

  • ํฌ๊ธฐ๋‹ค.
    • ์—ญ์‹œ ๋‚˜๋Š” ๋ญ๋‹ค?
    • ๋ฌด์ง€์„ฑ์ด๋‹ค!
def divisor_sum(n):
    divisors = set()
    divisors.add(1)# 1์€ ๋ชจ๋“  ์ˆ˜์˜ ์•ฝ์ˆ˜์ด๋ฏ€๋กœ ๋ฏธ๋ฆฌ ์ถ”๊ฐ€ํ•ด๋‘”๋‹ค
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            divisors.add(i)
            if i != n // i:
                divisors.add(n // i)
    divisors.add(n)  # ์ž๊ธฐ ์ž์‹ ๋„ ์•ฝ์ˆ˜์ด๋ฏ€๋กœ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค
    return len(divisors)

def solution(e, starts):
    answer = []
    sequence_of_numbers_of_divisors = []
    m = min(starts)
    leng = e+1 - m
    for i in reversed(range(m, e+1)):
        sequence_of_numbers_of_divisors.append(divisor_sum(i))
    #print(sequence_of_numbers_of_divisors)
    for s in starts:
        temp, tmax = 0, 0
        for i in range(e-s+1):
            if tmax <= sequence_of_numbers_of_divisors[i]:
                temp = e-i
                tmax = sequence_of_numbers_of_divisors[i]
            #print(e-i,":",sequence_of_numbers_of_divisors[i],end=",  ")
        #print("")
        answer.append(temp)
    return answer
    
ํ…Œ์ŠคํŠธ 1
์ž…๋ ฅ๊ฐ’ ใ€‰	8, [1, 3, 7]
๊ธฐ๋Œ“๊ฐ’ ใ€‰	[6, 6, 8]
์‹คํ–‰ ๊ฒฐ๊ณผ ใ€‰	ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • ์™ ์ง€ ์†๋„ ์•ˆ๋‚˜์˜ฌ ๊ฒƒ ๊ฐ™๋‹ค.
  • ์—ญ์‹œ...
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.4MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.18ms, 10.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1.71ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (6.53ms, 10.4MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (186.16ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (292.61ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (9071.45ms, 11.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ๋Œ€์ถฉ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ ๊นŒ ์‹ถ์—ˆ์ง€๋งŒ (์†๋„๋งŒ ์•Œ๊ณ  ์‹ถ์—ˆ์Œ) ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์‹œ๊ฐ„ ์ดˆ๊ณผ
def divisor_sum(n):
    divisors = set()
    divisors.add(1)# 1์€ ๋ชจ๋“  ์ˆ˜์˜ ์•ฝ์ˆ˜์ด๋ฏ€๋กœ ๋ฏธ๋ฆฌ ์ถ”๊ฐ€ํ•ด๋‘”๋‹ค
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            divisors.add(i)
            if i != n // i:
                divisors.add(n // i)
    divisors.add(n)  # ์ž๊ธฐ ์ž์‹ ๋„ ์•ฝ์ˆ˜์ด๋ฏ€๋กœ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค
    return len(divisors)

def solution(e, starts):
    answer = []
    sequence_of_numbers_of_divisors = []
    m = min(starts)
    leng = e+1 - m
    for i in reversed(range(m, e+1)):
        sequence_of_numbers_of_divisors.append(divisor_sum(i))
    #print(sequence_of_numbers_of_divisors)
    copied_starts = starts.copy()
    copied_starts.sort(reverse=True)
    #print(copied_starts) ํฐ ์ˆ˜๋ถ€ํ„ฐ
    x, temp, tmax = 0, 0, 0
    for i in range(e-m+1):
        if tmax <= sequence_of_numbers_of_divisors[i]:
            temp = e-i
            tmax = sequence_of_numbers_of_divisors[i]
        if copied_starts[x] == e-i:
            answer.append(temp)
            x += 1
    #print(copied_starts, starts)
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	์‹คํŒจ (0.17ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (0.88ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (3.59ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (21.55ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	์‹คํŒจ (73.95ms, 10.5MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (435.30ms, 10.9MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ๋ชจ๋“  ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜๋ฉด ์•ˆ๋˜๋‚˜ ๋ด„...
    • ์—ฌ๊ธฐ๊นŒ์ง€ 11์‹œ 50๋ถ„.
    • ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ ๊ทธ๋งŒ ์ข€ ๋‚˜์™”์œผ๋ฉด ์ข‹๊ฒ ๋„ค...
  • ๊ฒฐ๊ตญ ๊ฒ€์ƒ‰์˜, ๊ฒ€์ƒ‰์˜, ๊ฒ€์ƒ‰์˜...
    • ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜๋ฅผ ํ†ต์œผ๋กœ ๋น ๋ฅด๊ฒŒ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•.
def solution(e, starts):
    divisor=[0 for i in range(e+1)]
    for i in range(2,e+1):
        for j in range(1,min(e//i+1,i)):
            divisor[i*j]+=2
    for i in range(1,int(e**(1/2))+1):
        divisor[i**2]+=1
  • ์œ„ ์ฝ”๋“œ๋กœ ์•ฝ์ˆ˜์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฐพ์•„๋†“๊ณ ,
  • starts๋ฅผ ์—ญ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ํฐ ๊ฐ’๋ถ€ํ„ฐ ์ž‘์€ ๊ฐ’์œผ๋กœ ๋‚ด๋ ค์˜ค๋ฉด์„œ ์ฐพ๋Š”๋‹ค.
  • ๋‹ค ๋๋‚˜๊ณ  ์œ„์น˜ ๋งž์ถ”๊ธฐ ํ•ด์•ผํ•จ. ์ค‘๋ณต ๋˜๋Š” ์›์†Œ๊ฐ€ ์—†๋‹ค๋‹ˆ๊นŒ max๋กœ ๊ฐ’์„ ์ฐพ๊ณ , ๊ทธ ๊ฐ’์˜ index๋ฅผ ์ฐพ์•„์„œ, ์œ„์น˜ ๋งž์ถฐ์ฃผ๋ฉด ๋  ๋“ฏ? ์–ด์šฐ์•ผ... ๋” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์—†๋‚˜? ๋ฐฐ์—ด ์ •๋ ฌํ•  ๋•Œ ์œ„์น˜๋ฅผ ๊ธฐ๋กํ•ด์ฃผ๊ฑฐ๋‚˜...
    • ๋Š” ๋„˜ํŒŒ์ด์— ์•„๊ทธ์†ŒํŠธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์ž!
  • ์‹คํŒจํ•˜๊ธด ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋กœ ํ„ฐ์ง€์ง€๋Š” ์•Š๋Š”๋‹ค.
    • ํฐ ์ˆ˜ ๋ถ€ํ„ฐ ์ฐพ์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ ํ•ด๋ณผ๊นŒ?
import numpy as np

def solution(e, starts):
    answer = []
    divisors = [0 for i in range(e+1)]
    
    for i in range(2, e+1): # ๊ฐœ๋น ๋ฆ„
        for j in range(1,min(e//i+1,i)):
            divisors[i*j] += 2
            
    for i in range(1, int(e**0.5)+1):
        divisors[i**2] += 1
    
    reversed_starts = np.sort(starts)[::-1]
    reversed_index = np.argsort(starts)[::-1]
    
    x, temp, tmax, m = 0, 0, 0, min(reversed_starts)
    
    for i in reversed(range(m,e+1)):
        if tmax <= divisors[i]:
            temp = i
            tmax = divisors[i]
            
        if reversed_starts[x] == i:
            answer.append(temp)
            x += 1
    
    answer_sorted = [answer[i] for i in reversed_index]
    return answer_sorted
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.04ms, 28.2MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	์‹คํŒจ (0.06ms, 28.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	์‹คํŒจ (0.14ms, 28.3MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	์‹คํŒจ (0.46ms, 28.5MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	์‹คํŒจ (1.01ms, 28.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	์‹คํŒจ (4.70ms, 28.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	์‹คํŒจ (12.22ms, 28.6MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	์‹คํŒจ (53.17ms, 28.8MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (1724.01ms, 40.1MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (9404.31ms, 74.7MB)
  • ๊ทธ๋ƒฅ ๋งจ ์ฒ˜์Œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๋ณด๊ธฐ
    • ์•ˆ๋Œ. ์‹œ๊ฐ„ ์ดˆ๊ณผ๋‚จ.
import numpy as np

def solution(e, starts):
    answer = []
    divisors = [0 for i in range(e+1)]
    
    for i in range(2, e+1): # ๊ฐœ๋น ๋ฆ„
        for j in range(1,min(e//i+1,i)):
            divisors[i*j] += 2
            
    for i in range(1, int(e**0.5)+1):
        divisors[i**2] += 1
    
    for s in starts:
        temp, tmax = 0, 0
        for i in reversed(range(s,e+1)):
            if tmax <= divisors[i]:
                temp = i
                tmax = divisors[i]
        answer.append(temp)
    
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 28.5MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 28.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.09ms, 28.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (1.40ms, 28.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (4.22ms, 28.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (83.54ms, 28.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (269.83ms, 28.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (7996.83ms, 29.3MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ๋ง‰ํž˜ ใ…‹ใ…‹
  • ํžŒํŠธ ํžŒํŠธ ํžŒํŠธ ใ… .ใ… 
  • ํžŒํŠธ ์ตœ๋Œ€ํ•œ ์•ˆ๋ณด๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ๋„๋ฌด์ง€ ๊ฐˆํ”ผ๋ฅผ ๋ชป ์žก๊ฒ ๊ณ , ์ž ์€ ์ž๊ณ  ์‹ถ๊ณ ...
  • ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ DP๋กœ ํ’€์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฑธ ๋ณด๊ณ ... ์•„... ํ–ˆ๋”ฐ.
def solution(e, starts):
    answer = []
    divisors = [0 for i in range(e+1)]
    answers = [0 for i in range(e+1)]
    temp = 0
    
    for i in range(2, e+1):
        for j in range(1,min(e//i+1,i)):
            divisors[i*j] += 2
    for i in range(1, int(e**0.5)+1):
        divisors[i**2] += 1
    
    for i in reversed(range(e+1)):
        if divisors[i] >= temp:
            temp = divisors[i]
            answers[i] = i
        else:
            divisors[i] = temp
            answers[i] = answers[i+1]
    
    for i in starts:
        answer.append(answers[i])
        
    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.12ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.37ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.36ms, 10.2MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (6.35ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (16.73ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (47.26ms, 11.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (1497.45ms, 28.5MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (8128.52ms, 91.6MB)
  • ํ’€๊ณ  ๋‚˜๋‹ˆ ๋ ˆ๋ฒจ 3 ๋ฌธ์ œ์˜€๋„ค?
  • ๋ ˆ๋ฒจ2 ์นด์นด์˜ค ๋ธ”๋ผ์ธ๋“œ ์ฑ„์šฉ ๋ฌธ์ œ ์ง€๊ฒจ์›Œ์„œ ๋‚œ์ด๋„ ์ฒดํฌ ๋ฐ”๊พธ๋‹ค๊ฐ€ ํ’€๋ฆฐ ๋“ฏ... ํฌํก...
  • ๊ณ ์ˆ˜๋“ค์˜ ํ’€์ด๋ฅผ ๋ณด๋„๋ก ํ•˜์ž.
    • ์ด๊ฑด ํ†ต๊ณผ๋œ ์ฝ”๋“œ์ธ๋ฐ ์ง€๊ธˆ์€ ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚œ๋‹ค.
def solution(e, starts):
    arr = [1]*(e+1)
    ans = [0]*(e+1)
    i = 2
    for i in range(2,e+1):
        for j in range(i*2,e+1,i):
            arr[j] += 1
    ans[e] = e
    for i in reversed(range(0,e)):
        if arr[i] >= arr[ans[i+1]]:
            ans[i] = i
        else:
            ans[i] = ans[i+1]
    return [ans[i] for i in starts]
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.10ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.68ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.52ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (7.90ms, 10.1MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (16.89ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (103.20ms, 11MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (2114.45ms, 28.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	์‹คํŒจ (์‹œ๊ฐ„ ์ดˆ๊ณผ)
  • ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ๋ฐฉ์‹, ์—„์ฒญ ๋น ๋ฆ„. ์™ค์ผ€ ๋น ๋ฅด์ง€?
    • e_sqrt ํ•œ๋ฒˆ๋งŒ ๊ณ„์‚ฐ? (๊ทผ๋ฐ ๋‚ด ์ฝ”๋“œ์—์„  ์ด๊ฑธ ๋”ฐ๋กœ ๋นผ๋ฉด ๋” ๋Š๋ ค์ง)
    • ๋ฉ”๋ชจ์ด์ œ์ด์…˜์œผ๋กœ ์•ฝ์ˆ˜ ๊ฐฏ์ˆ˜ ๊ตฌํ•จ.
    • ์•ฝ์ˆ˜๊ฐฏ์ˆ˜๊ฐ€ ๋งŽ์€ ์ˆซ์ž๋กœ ๊ฐฑ์‹ ํ•˜๋Š” ์ฝ”๋“œ๋„ ์ด์˜๋‹ค. ๊ทผ๋ฐ ์†๋„๋ž‘ ์ƒ๊ด€์—†์–ด ๋ณด์ด๋Š”๋ฐ...
    • ๋งˆ์ง€๋ง‰์— answer ๋ฆฌ์ŠคํŠธ์— append( )ํ•˜์ง€ ์•Š๊ณ  start์— ๊ฐ’ ๋ฎ์–ด์”Œ์›€.
    • ๊ฑฐ๊ธฐ๋„ memo[start] ๊ฐ’์„ ๋„ฃ์–ด๋ฒ„๋ฆฌ๋„ค... ํ•œ ์ค„ ํ•œ ์ค„์ด ์˜ˆ์ˆ ์ด๋‹ค. ๊ทผ๋ฐ ์†๋„๋ž‘ ๊ด€๋ จ์žˆ๋‚˜?
def solution(e, starts):
    memo = [0] * (e+1)
    e_sqrt = int(pow(e, 0.5))
    for n in range(1, e_sqrt+1):
        memo[n*n] += 1
        for i in range(n*(n+1), e+1, n):
            memo[i] += 2

    max_val = max_num = 0
    for i in range(e, 0, -1):
        if memo[i] >= max_val:
            max_val, max_num = memo[i], i
        memo[i] = max_num

    for i, start in enumerate(starts):
        starts[i] = memo[start]

    return starts
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.26ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.56ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (2.54ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (7.40ms, 10.2MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (39.25ms, 10.6MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (947.92ms, 19.9MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (5627.77ms, 52.7MB)
  • ์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ถ€๋ถ„์ด ๋” ๋น ๋ฅธ ๊ฑฐ์˜€๊ตฌ๋‚˜?
    • ์•ฝ์ˆ˜ ๊ตฌํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ๊ต์ฒดํ•˜๋‹ˆ๊นŒ ๋” ๋นจ๋ผ์ง
def solution(e, starts):
    answer = []
    divisors = [0 for i in range(e+1)]
    answers = [0 for i in range(e+1)]
    temp = 0

    '''
    for i in range(2, e+1):
        for j in range(1,min(e//i+1,i)):
            divisors[i*j] += 2
    for i in range(1, int(e**0.5)+1):
        divisors[i**2] += 1
    '''
    e_sqrt = int(pow(e, 0.5))
    for n in range(1, e_sqrt+1):
        divisors[n*n] += 1
        for i in range(n*(n+1), e+1, n):
            divisors[i] += 2
            
    for i in reversed(range(e+1)):
        if divisors[i] >= temp:
            temp = divisors[i]
            answers[i] = i
        else:
            divisors[i] = temp
            answers[i] = answers[i+1]

    for i in starts:
        answer.append(answers[i])

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.62ms, 10.1MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (4.71ms, 10.2MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (6.59ms, 10.5MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (30.91ms, 11.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (805.40ms, 28.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (5287.40ms, 91.4MB)
  • ๊ทธ๋ฆฌ๊ณ  e_sqrt๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ for๋ฌธ์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์€ ๋” ๋Š๋ฆผ.
    • ๊ทธ๋ƒฅ int(e**0.5)๋กœ ์ˆ˜์ •์‹œ ์กฐ๊ธˆ ๋” ๋นจ๋ผ์ง.
def solution(e, starts):
    answer = []
    divisors = [0 for i in range(e+1)]
    answers = [0 for i in range(e+1)]
    temp = 0

    for n in range(1, int(e**0.5)+1):
        divisors[n*n] += 1
        for i in range(n*(n+1), e+1, n):
            divisors[i] += 2
            
    for i in reversed(range(e+1)):
        if divisors[i] >= temp:
            temp = divisors[i]
            answers[i] = i
        else:
            divisors[i] = temp
            answers[i] = answers[i+1]

    for i in starts:
        answer.append(answers[i])

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.03ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.42ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (2.47ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (4.68ms, 10.4MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (26.23ms, 11.2MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (713.76ms, 28.5MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (4508.23ms, 91.5MB)
  • ์ข‹์€ ๊ฑฐ ๋งŽ์ด ์•Œ์•˜๋”ฐ...
728x90
๋ฐ˜์‘ํ˜•