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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 6064๋ฒˆ - ์นด์ž‰๋‹ฌ๋ ฅ (์ž์„ธํ•œ ์„ค๋ช…)

๊ฐ์ž ๐Ÿฅ” 2023. 1. 1. 20:17
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ

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

 

6064๋ฒˆ: ์นด์ž‰ ๋‹ฌ๋ ฅ

์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ž…๋ ฅ์€ T๊ฐœ์˜ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. ์ž…๋ ฅ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋Š” ํ•œ ์ค„๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

www.acmicpc.net

 

๐ŸŸ  ์ฒซ๋ฒˆ์งธ ํ’€์ด - ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค

var n = Int(readLine()!)!

for _ in 0..<n {
    let mnxy = readLine()!.split(separator: " ").map{ Int($0)! }
    var year = 1
    let maxYear = 40000

    while true {
        if (year - mnxy[2]) % mnxy[0] == 0, (year - mnxy[3]) % mnxy[1] == 0 {
            print(year)
            break
        }
        
        if year == maxYear {
            print(-1)
            break
        }
        year += 1
    }
}

์ฒซ๋ฒˆ์žฌ ํ’€์ด๋Š” ๋‹ต์„ ํ‹€๋ ธ๋‹ค. ๊ทธ๋ƒฅ ๋‚ด๊ฐ€ maxYear์„ 40000์œผ๋กœ ๋‘๊ณ  ํ’€์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์นด์ž‰๋‹ฌ๋ ฅ์—์„œ ๋งŒ์•ฝ M๊ณผ N์ด 10, 12๋กœ ์ฃผ์–ด์ง„๋‹ค๋ฉด, ์ตœ๋Œ€ ๋…„๋„๋Š” 40000์ด ์•„๋‹ˆ๋‹ค. x์™€ y๊ฐ€ 10, 12๋กœ ์ถœ๋ ฅ๋˜๋Š” 60์ด ์ตœ๊ณ  ๋…„๋„๊ฐ€ ๋œ๋‹ค. ๋ฌด์Šจ ๊ณตํ†ต์ ์ด๋ƒ๋ฉด, ์ฃผ์–ด์ง€๋Š” M๊ณผN์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๊ฐ€ ํ•ด๋‹น ์นด์ž‰๋‹ฌ๋ ฅ์˜ ์ตœ๋Œ€๋…„๋„๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์„ ์ „ํ˜€ ๊ณ ๋ คํ•˜์ง€ ์•Š์€์ฑ„ ์ฝ”๋“œ๋ฅผ ์งœ์„œ ๋ฌธ์ œ๋ฅผ ํ‹€๋ ธ๋‹ค.

๐ŸŸ  ๋‘๋ฒˆ์งธ ํ’€์ด - ์‹œ๊ฐ„์ดˆ๊ณผ

์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๊ณต์‹์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

var n = Int(readLine()!)!

for _ in 0..<n {
    let mnxy = readLine()!.split(separator: " ").map{ Int($0)! }
    var year = 1
    let gcd = mnxy[0] > mnxy[1] ? gcd(mnxy[0], mnxy[1]) : gcd(mnxy[1], mnxy[0])
    let maxYear = (mnxy[0] * mnxy[1]) / gcd

    while true {
        if (year - mnxy[2]) % mnxy[0] == 0, (year - mnxy[3]) % mnxy[1] == 0 {
            print(year)
            break
        }
        if year == maxYear {
            print(-1)
            break
        }
        year += 1
    }
}

private func gcd(_ n: Int, _ m: Int) -> Int {
    return m == 0 ? n : gcd(m, n%m)
}

gcd ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๋จผ์ € ๊ตฌํ•ด์ค€๋‹ค์Œ, ๋‘ ์ˆ˜์˜ ๊ณฑ์„ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋กœ ๋‚˜๋ˆ ์ค€ ๊ฒƒ์ด ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๊ฐ€ ๋˜๋Š” ์„ฑ์งˆ์„ ์ด์šฉํ•ด์„œ maxYear์„ ๊ตฌํ–ˆ๋‹ค. 

์ด๋ ‡๊ฒŒ ํ‘ธ๋‹ˆ๊นŒ, ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค. ์–ด์จŒ๋“  ๋‚˜์˜ ์ฝ”๋“œ๋Š” year์— +1 ํ•ด์ฃผ๋ฉด์„œ, ์ž‘์„ฑ๋˜๋Š” ๋ชจ๋“  ์ˆ˜์— ๋Œ€ํ•˜์—ฌ ๊ฐ€์žฅ ์ตœ๋Œ€๋กœ for๋ฌธ์„ ๋Œ๋ฆฌ๊ฒŒ ๋œ๋‹ค. ์ด๋ถ€๋ถ„์—์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์—ฌ์•ผํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

maxYear์„ ์ „๋ถ€ ์‚ดํŽด๋ณด๋Š” ๋ฐฉ๋ฒ• ๋Œ€์‹ , ๊ฒฝ์šฐ์˜์ˆ˜๋ฅผ ์ค„์—ฌ์„œ ๋ณด๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์–ด์•ผํ•œ๋‹ค....

๐ŸŸ  ์„ธ๋ฒˆ์งธ ํ’€์ด - ๋งž์•˜์Šต๋‹ˆ๋‹ค!

๐Ÿ”– M๊ณผ N์„ ๊ตฌ๋ณ„ํ•ด์„œ ์ƒ๊ฐํ•ด๋ณด์ž.

๋‚ด๊ฐ€ ์ฒ˜์Œ ํ’€์ดํ•œ ๊ฒƒ์€, M๊ณผ N์„ ๋ชจ๋‘ ๋™์‹œ์— ๊ณ ๋ คํ•˜๋ฉด์„œ year์— +1์”ฉํ•ด์ฃผ์—ˆ๋‹ค. ์—ฌ๊ธฐ์„œ M๊ณผ N์„ ๋ถ„๋ฆฌํ•ด์„œ ๋‹จ๊ณ„๋ณ„๋กœ ์ƒ๊ฐํ•ด๋ณด์ž.

๐Ÿ’ฌ (์˜ˆ์ œ์ž…๋ ฅ1) M = 10, N = 12 

๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ด๋ ‡๊ฒŒ 10๊ฐœ, 12๊ฐœ์”ฉ ๋ฐ˜๋ณต๋œ๋‹ค. ๊ฒฐ๊ตญ ์šฐ๋ฆฌ์˜ ์นด์ž‰๋‹ฌ๋ ฅ์€ ์ตœ๋Œ€ 60๊ฐœ์˜ ๊ฐ’์„ ๊ฐ€์ง„๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด, ํ˜•๊ด‘ํŒฌ ์น ํ•œ ๋ถ€๋ถ„์—์„œ ๋นจ๊ฐ„๋ฉ์–ด๋ฆฌ๋Š” 6๋ฉ์–ด๋ฆฌ (60 / 10), ์—ฐ๋‘์ƒ‰ ํ˜•๊ด‘ํŒฌ ๋ฉ์–ด๋ฆฌ๋Š” 5๋ฉ์–ด๋ฆฌ (60 / 12)๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค. 

๐Ÿ’ฌ ๊ทธ๋Ÿผ X = 3 ์ธ ๊ฒฝ์šฐ๋Š” ๋ช‡๊ฐ€์ง€์ž„?

๋จผ์ € X๊ฐ€ 3์ธ ๊ฒฝ์šฐ๋งŒ ์‚ดํŽด๋ณด์ž. ์œ„์—์„œ ๋นจ๊ฐ„๋ฉ์–ด๋ฆฌ๋Š” 6๊ฐœ๊ฐ€ ๋‚˜์˜จ๋‹ค๊ณ  ํ–ˆ๋‹ค. ์ฆ‰, X๊ฐ€ 3์ธ ๊ฒฝ์šฐ๋„ 5๊ฐ€์ง€์ด๋‹ค.

X = 3 ์ธ ๊ฒฝ์šฐ -> ( maxYear / M) ๊ฐœ

 

๐Ÿ’ฌ ๊ทธ๋ ‡๋‹ค๋ฉด ๊ฒฐ๊ตญ, X=3์ด๋ฉด์„œ, Y=9์ธ ๊ฒฝ์šฐ๋ฅผ ์ฐพ์œผ๋ฉด ๋˜๋Š”๊ฑฐ ์•„๋‹˜?

X = 3์ผ ๋•Œ๋Š”, ๋ช‡๋…„์ผ๊นŒ?

3๋…„, 13๋…„, 23๋…„,,, ์ด๋ ‡๊ฒŒ ๊ฐˆ ๊ฒƒ์ด๋‹ค. ์ด๊ฒƒ์„ for๋ฌธ์œผ๋กœ ๋งŒ๋“ค์–ด์„œ ๊ทœ์น™์„ ์ฐพ์•„์ฃผ๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋  ๊ฒƒ์ด๋‹ค.

for i in 0..<maxYear/M {
	let year = i * M + X
}

year์€ ๊ฐ๊ฐ 3, 13, 23, 33, 43, 53 ์ด๋ ‡๊ฒŒ ์—ฌ์„ฏ๊ฐœ๊ฐ€ ๋‚˜์˜ค๊ฒ ์ง€!?

๊ทธ๋Ÿผ ๊ฐ๊ฐ 3, 13, 23,,,,์ผ๋•Œ y = 9 ์ธ ๊ฒฝ์šฐ๋ฅผ ์ฐพ์•„์ฃผ์ž

year๊ฐ€ ๋งŒ์•ฝ 3์ด๋ผ๋ฉด, y๋Š” 3์ด๋œ๋‹ค.
year๊ฐ€ ๋งŒ์•ฝ 13์ด๋ผ๋ฉด, y๋Š” 1์ด ๋œ๋‹ค. y๋Š” N๋งŒํผ์”ฉ ์ˆœํšŒํ•˜๊ธฐ ๋•Œ๋ฌธ์— 12๋กœ ๋‚˜๋ˆ ์ค€ ๋‚˜๋จธ์ง€๊ฐ’์„ ๊ฐ–๋Š”๋‹ค

์ด๊ฒƒ์„ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•ด๋ณด์ž.

for i in 0..<maxYear/M {
	let year = i * M + X
    let y = year % N
}

๊ทผ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด, ์˜ˆ์™ธ์‚ฌํ•ญ์ด ์ƒ๊ธด๋‹ค. ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ๊ฒฝ์šฐ y๋Š” 0์ด๋œ๋‹ค. ์‚ฌ์‹ค ์šฐ๋ฆฌ๋Š” 0์ด ์•„๋‹ˆ๋ผ N๊ทธ๋Œ€๋กœ๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‚˜๋จธ์ง€๊ฐ€ 0์ธ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค.

for i in 0..<maxYear/M {
	let year = i * M + X
    let y = year % N == 0 ? N : year % N
}

์ด๋ ‡๊ฒŒ y๊นŒ์ง€ ํŒ๋‹จํ•ด์ฃผ๊ณ , ์ด y๊ฐ€ ์ž…๋ ฅ๋ฐ›์€ Y(9)์™€ ๊ฐ™๋‹ค๋ฉด, break ํ•ด์„œ for๋ฌธ์„ ํƒˆ์ถœํ•ด์ฃผ์ž.

 

์ „์ฒด ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 
์ด์ „์—๋Š” ๋ฌด์กฐ๊ฑด 40000๋ฒˆ์„ ๋Œ์•˜๋˜๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ, ์ตœ์†Œ์˜ ๊ฒฝ์šฐ์˜์ˆ˜ ๋งŒํผ๋งŒ for๋ฌธ์„ ๋Œ๊ฒŒ ๋œ๋‹ค.

var n = Int(readLine()!)!

for _ in 0..<n {
    let mnxy = readLine()!.split(separator: " ").map{ Int($0)! }
    let gcd = mnxy[0] > mnxy[1] ? gcd(mnxy[0], mnxy[1]) : gcd(mnxy[1], mnxy[0])
    let maxYear = (mnxy[0] * mnxy[1]) / gcd
    var isinCal = false

    for i in 0..<maxYear / mnxy[0] {
        let year = i * mnxy[0] + mnxy[2]
        let y = year % mnxy[1] == 0 ? mnxy[1] : year % mnxy[1]

        if y == mnxy[3] {
            print(year)
            isinCal = true
            break
        }
    }

    if !isinCal {
        print(-1)
    }
}

private func gcd(_ n: Int, _ m: Int) -> Int {
    return m == 0 ? n : gcd(m, n%m)
}
๋ฐ˜์‘ํ˜•