Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 17425๋ฒˆ - ์•ฝ์ˆ˜์˜ ํ•ฉ

๊ฐ์ž ๐Ÿฅ” 2022. 12. 23. 13:00
๋ฐ˜์‘ํ˜•

์ด๊ฒŒ ์™œ ๊ณจ๋“œ์ง€? ํ–ˆ๋‹ค๊ฐ€ ์•„์ฃผ๊ทธ๋ƒฅ ํ˜ธ์˜ค์˜จ๋‚ฌ๋˜ ๋ฌธ์ œ!!

 

๐ŸŸ  ๋ฌธ์ œ

https://www.acmicpc.net/problem/17425

 

17425๋ฒˆ: ์•ฝ์ˆ˜์˜ ํ•ฉ

๋‘ ์ž์—ฐ์ˆ˜ A์™€ B๊ฐ€ ์žˆ์„ ๋•Œ, A = BC๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ž์—ฐ์ˆ˜ C๋ฅผ A์˜ ์•ฝ์ˆ˜๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 2์˜ ์•ฝ์ˆ˜๋Š” 1, 2๊ฐ€ ์žˆ๊ณ , 24์˜ ์•ฝ์ˆ˜๋Š” 1, 2, 3, 4, 6, 8, 12, 24๊ฐ€ ์žˆ๋‹ค. ์ž์—ฐ์ˆ˜ A์˜ ์•ฝ์ˆ˜์˜ ํ•ฉ์€ A์˜ ๋ชจ๋“  ์•ฝ์ˆ˜๋ฅผ ๋”

www.acmicpc.net

๋ญ์•ผ! ์ด์ „์— ํ’€์—ˆ๋˜ '์•ฝ์ˆ˜์˜ํ•ฉ2'๋ฌธ์ œ๋ž‘ ๋˜‘๊ฐ™์ž–์•„? ๊ทผ๋ฐ ๊ทธ๋ƒฅ , ์ž…๋ ฅ๋งŒ ๋‹ค๋ฅด๊ฒŒ ํ•˜๋ฉด ๋˜๋Š”๊ฑฐ์ž–์•„!!!

 

๐ŸŸ  ์‹œ๊ฐ„์ดˆ๊ณผ ^___^

์‹ฌ์ง€์–ด ์ž…๋ ฅ ๋ฐฉ์‹๋„ ์ž˜๋ชป์•Œ์•˜๋‹ค. ์ดˆ๋ฐ˜์— ์ž…๋ ฅ๋˜๋Š” 5๋ฅผ ๋ชป๋ณด๊ณ , ๊ทธ๋ƒฅ ๋์—†์ด ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•œ? ๋ฌธ์ œ์ธ์ค„ ์•Œ๊ณ  while์œผ๋กœ ์ž‘์„ฑํ–ˆ๋‹ค.

while let N = Int(readLine()!) {
    print(solution(N))
}

private func solution(_ N: Int) -> Int {
    let N = N ?? 0
    var sum = 0

    for i in 1..<N+1 {
        sum += (N / i) * i
    }

    return sum
}

์—ฅ,, ์‹œ๊ฐ„์ดˆ๊ณผ๋„ค? ์ผ๋‹จ, ์ดˆ๋ฐ˜์— N๊ฐœ๋ฅผ ์ž…๋ ฅํ• ๊ฑฐ๋ผ๋Š” T๋ฅผ ์ž…๋ ฅ๋ฐ›์•˜์–ด์•ผํ–ˆ๋Š”๋ฐ, ๋‚˜๋Š” ๋์—†์ด ์ž…๋ ฅ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅด ์ž‘์„ฑํ•ด์„œ ํ‹€๋ฆฐ์ค„ ์•Œ์•˜๋‹ค. ๊ทผ๋ฐ ,, ์ด๋ฌธ์ œ๋„ ์•„๋‹ˆ์—ˆ๋‹ค.!!

์ž…๋ ฅ์„ ์ œ๋Œ€๋กœ ๋ฐ›์•„๋„ ์‹œ๊ฐ„์ดˆ๊ณผ์˜€๋‹ค.

๐Ÿ”– ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ณด์ž

์šฐ์„ , ์˜ˆ์ œ๋ฅผ๋ณด๋ฉด, T = 5 ์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋‚ด ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด, ๋งŒ์•ฝ 5๋ฒˆ์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด, 5๋ฒˆ ๋ชจ๋‘ g(N)์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

์˜ˆ์ œ์ƒํ™ฉ์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด๋ฉด
g(1) ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ f(1) ๊ตฌํ•˜๊ณ  sum์ธ f(1) ๊ตฌํ•จ
g(2) ๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ f(1), f(2) ๊ตฌํ•˜๊ณ  sum ์ธ f(1) + f(2) ๊ตฌํ•จ
g(10)์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ f(1), f(2), f(3), f(4), f(5)....f(10) ๊ตฌํ•˜๊ณ , sum ์ธ f(1)+f(2)+f(3)+...+f(10) ๊ตฌํ•จ
.......
์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณต๋œ๋‹ค.

์“ธ๋ฐ์—†๋Š” ์—ฐ์‚ฐ์ด ๋งŽ์•„์กŒ๋‹ค. f(2)๋ฅผ ์ด์ „์— ๊ตฌํ•ด๋†จ๋Š”๋ฐ, ์ด๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆœ ์—†์„๊นŒ? ๋˜, ์ง€๊ธˆ ์˜ˆ์ œ์—์„œ๋งŒ ์ด์ •๋„์ง€, ๋งŒ์•ฝ T = 100000 ์„ ์ž…๋ ฅ๋ฐ›๊ฒŒ ๋˜๋ฉด, 10๋งŒ๋ฒˆ์„ ์—ฐ์‚ฐํ•ด์ค˜์•ผํ•œ๋‹ค. ์ด ์—ฐ์‚ฐ์„ ์ค„์ด๊ธฐ ์œ„ํ•ด์„œ๋Š”,,, dp ํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผ๊ฒ ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.

์ฆ‰, ๋ชจ๋“  ๋‹ต์„ ๋ฏธ๋ฆฌ ๊ตฌํ•ด๋†“๊ณ , ๋งˆ์ง€๋ง‰์— ์ž…๋ ฅ๋ฐ›๋Š” i ์— ๋Œ€ํ•ด์„œ dp[i]๋ฅผ ๋ฐ”๋กœ ์ถœ๋ ฅํ•  ์ˆ˜ ์ž‡๊ฒŒ ๋งŒ๋“ค์–ด ๋†“๋Š” ๊ฒƒ์ด๋‹ค.

[ f(1), f(1)+f(2), f(1)+f(2)+f(3), f(1)+f(2)+f(3)+f(4), ,,,,,,, f(1)+..+f(100000) ]

์ด๋ ‡๊ฒŒ ๊ตฌํ•ด๋ณด์ž.

๐ŸŸ  ์ฒซ๋ฒˆ์งธ ์‹œ๋„

์ด๋ ‡๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. 

[ f(1), f(1)+f(2), f(1)+f(2)+f(3), f(1)+f(2)+f(3)+f(4), ,,,,,,, f(1)+..+f(100000) ]

์ด ํ˜•ํƒœ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ, div๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์—ˆ๊ณ , div๋Š” i์˜ ์•ฝ์ˆ˜์˜ ํ•ฉ์„ ๋„ฃ์–ด๋‘” ๊ฒƒ์ด๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋„ ์ƒ๊ฐ์ด ์กฐ๊ธˆ ํ•„์š”ํ–ˆ๋Š”๋ฐ, ์ธํ„ฐ๋„ท์„ ์กฐ๊ธˆ ,, ๋’ค์ ธ๋ดค๋‹ค ๋ฏธ๋ฆฌ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 1์˜ ๋ฐฐ์ˆ˜์— 1์”ฉ ๋”ํ•ด์ฃผ๊ณ , 2์˜ ๋ฐฐ์ˆ˜์—๋Š” 2๋ฅผ ๋”ํ•ด์ฃผ๊ณ , 3์˜ ๋ฐฐ์ˆ˜์—๋Š” 3์„ ๋”ํ•ด์ฃผ๊ณ ,,, ์ด๋Ÿฐ์‹์œผ๋กœ 100๋งŒ๊นŒ์ง€ ๊ฐ i์˜ ์•ฝ์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•ด์ฃผ์—ˆ๋‹ค. ๊ทธ ๋ฐฐ์—ด์ด ๋ฐ”๋กœ div

๊ทธ๋ฆฌ๊ณ  sum ๋ฐฐ์—ด์€ ๋ˆ„์ ํ•ฉ์„ ๊ตฌํ•ด์ฃผ์—ˆ๋‹ค. 

 

private func solution() {
    let accumulate = accumulateSum()
    let T = Int(readLine()!)!

    for _ in 0..<T {
        let num = Int(readLine()!)!
        print(accumulate[num])
    }
}

private func accumulateSum() -> [Int] {
    var div = Array(repeating: 0, count: 1000000)
    var sum = Array(repeating: 0, count: 1000000)

    for i in 1...1000000 {
        var j = 1
        while i*j < 1000000 {
            div[i*j] += j
            j += 1
        }
    }
    sum[1] = div[1]
    for i in 2..<div.count {
        sum[i] = sum[i-1] + div[i]
    }

    return sum
}

solution()

์ญ

๋Ÿฐํƒ€์ž„์—๋Ÿฌ..? ์–ด๋”” ์˜ˆ์™ธ๋ฅผ ๋ชป์žก์•˜๋‚˜๋ณด๋‹ค.. ๋„๋Œ€์ฒด ๋ญ˜๊นŒ!

 

๐ŸŸ  ์ •๋‹ต ์ฝ”๋“œ

๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ์˜ ๋ฌธ์ œ๋Š” ๋ฒ”์œ„์˜€๋‹ค. 

    var div = Array(repeating: 0, count: 1000000)
    var sum = Array(repeating: 0, count: 1000000)
    
    while i*j < 1000000 { .. }

div ์™€ sum์„ ์ด๋ ‡๊ฒŒ ์„ค์ •ํ•ด์ฃผ๊ณ  while๋ฌธ์„ 1000000 ๋ฏธ๋งŒ์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๊ฒŒ ๋˜๋‹ˆ๊นŒ, N์˜ ์ตœ๋Œ€๊ฐ’์ธ 1000000์„ ์ž…๋ ฅํ•  ์ˆ˜ ์—†์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฒ”์œ„๋ฅผ ์กฐ์ ˆํ•ด์ฃผ๋ฉด์„œ ๋Ÿฐํƒ€์ž„์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค!

private func solution() {
    let accumulate = accumulateSum()
    let T = Int(readLine()!)!

    for _ in 0..<T {
        let num = Int(readLine()!)!
        print(accumulate[num])
    }
}

private func accumulateSum() -> [Int] {
    var div = Array(repeating: 0, count: 1000001)
    var sum = Array(repeating: 0, count: 1000001)

    for i in 1...1000000 {
        var j = 1
        while i*j <= 1000000 {
            div[i*j] += j
            j += 1
        }
    }
    sum[1] = div[1]
    for i in 2..<div.count {
        sum[i] = sum[i-1] + div[i]
    }

    return sum
}

solution()

๋ฐ˜์‘ํ˜•