Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

64-tgyuuAn #217

Merged
merged 2 commits into from
Jul 26, 2024
Merged

64-tgyuuAn #217

merged 2 commits into from
Jul 26, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Jul 12, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

K์ง„ ํŠธ๋ฆฌ

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40๋ถ„

๋ชฐ๋ผ์„œ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋ณด๊ณ  ํ•ด๊ฒฐ!

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ด ๋ฌธ์ œ... ํ’€๋ ค๊ณ  ํ•œ 40๋ถ„ ์žก๊ณ  ์žˆ์—ˆ๋Š”๋ฐ ๋„์ €ํžˆ ๋ชจ๋ฅด๊ฒ ์–ด์„œ

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„๋ฅ˜๋ฅผ ์ผœ๋ณด๋‹ˆ๊นŒ ์ตœ์†Œ ๊ณตํ†ต ์กฐ์ƒ์ด๋ผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋”๋ผ๊ณ ์š”.

์ด๊ฒŒ ๋˜๊ฒŒ ๊ฐ„๋‹จํ•œ๊ฑด๋ฐ ์‹ ๊ธฐํ•˜๊ณ  ์œ ์šฉํ–ˆ์–ด์š”.




์ตœ์†Œ ๊ณตํ†ต ์กฐ์ƒ (LCA, Lowest Common Ancestor) ๋ž€ .....?

๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์€ ํŠธ๋ฆฌ๊ตฌ์กฐ์—์„œ,

13, 11 ๋…ธ๋“œ์— ๊ณตํ†ต ์กฐ์ƒ์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค๊ณ  ํ•ด๋ณด์ž.

image

๊ทธ๋Ÿผ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?




๋งž๋‹ค. 13, 11๋…ธ๋“œ๋ถ€ํ„ฐ ๋™์‹œ์— ๋ถ€๋ชจ๋…ธ๋“œ๋กœ ํ•˜๋‚˜์”ฉ ๊ฐ™์ด ์˜ฌ๋ผ๊ฐ€๋ฉด ๋œ๋‹ค. ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค.







๊ทธ๋Ÿผ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•  ๊ฒƒ์ธ๊ฐ€?

์œ„ ์˜ˆ์ œ์˜ 13, 11 ๋…ธ๋“œ์˜ ๊ฒฝ์šฐ๋Š” ์‹œ์ž‘ํ•  ๋•Œ ๊ฐ™์€ depth์˜€๊ธฐ ๋•Œ๋ฌธ์— ๋™์‹œ์— ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ์˜ฌ๋ผ๊ฐ€๋ฉด ๊ณตํ†ต ์กฐ์ƒ์„ ๋งŒ๋‚  ์ˆ˜ ์žˆ์ง€๋งŒ,

์•„๋ž˜์˜ ์˜ˆ์ œ ๊ฐ™์€ 13, 15๋…ธ๋“œ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ์‹œ์ž‘ depth๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋™์‹œ์— ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ํƒ€๊ณ  ์˜ฌ๋ผ๊ฐ€๋‹ค๊ฐ€,

image




์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์—‡๊ฐˆ๋ฆฌ๊ฒŒ ๋œ๋‹ค.

image







์ด๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์„๊นŒ?

..







์‚ฌ์‹ค ์ด๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋‹ค.

๋™์‹œ์— ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ์˜ฌ๋ผ๊ฐ€๋Š” ๋กœ์ง์„ ํ•˜๊ธฐ ์ „, ์ฐพ์œผ๋ ค๋Š” ๋‘ ๋…ธ๋“œ์˜ depth๋ฅผ ๋จผ์ € ๊ณ„์‚ฐํ•œ ๋’ค,

๋” ๊นŠ์€ ๊ณณ์˜ ๋…ธ๋“œ๋ฅผ ๋ถ€๋ชจ๋กœ ํ•œ ๋‹จ๊ณ„์”ฉ ์˜ฌ๋ ค depth๋ฅผ ๊ฐ™๋„๋ก ๋งž์ถฐ์ฃผ๋Š” ์ž‘์—…์„ ํ•œ ๋’ค,

๋™์‹œ์— ๋ถ€๋ชจ๋กœ ๊ฐ™์ด ์˜ฌ๋ผ๊ฐ€๋Š” ๋กœ์ง์„ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค.







๊ทธ๋Ÿผ ์šฐ๋ฆฌ๋Š” depth๋ฅผ ์›€์ง์ธ ํšŸ์ˆ˜์˜ ์ด ํ•ฉ์„ answer๋กœ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋ !







์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ $O(depth)$๊ฐ€ ๋˜๋Š”๋ฐ,

์ด ๋ฌธ์ œ์—์„œ ์ค‘์š”ํ•œ ์ ์€ K์˜ ๊ฐ’์ด 1์ผ ๋•Œ, depth๊ฐ€ N์ธ $10^{15}$๊ฐ€ ๋˜์–ด์„œ ํ„ฐ์ ธ๋ฒ„๋ฆฌ๊ฒŒ ๋œ๋‹ค.

์‚ฌ์‹ค K๊ฐ€ 1์ผ ๋•Œ๋งŒ ์ ˆ๋Œ€๊ฐ’์œผ๋กœ ๊ณ„์‚ฐํ•ด์ฃผ๋ฉด ํ•ด๊ฒฐ๋˜๋Š” ๋ถ€๋ถ„์ธ๋ฐ, ์ด๋ฅผ ์บ์น˜ํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋ด๋ฒ„๋ ธ๋‹ค.

์ƒˆ๋กœ์šด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ฐฐ์›Œ์„œ ์žฌ๋ฏธ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

@tgyuuAn tgyuuAn added tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ์ž‘์„ฑ ์ค‘ โฑ๏ธ labels Jul 12, 2024
@tgyuuAn tgyuuAn requested review from alstjr7437 and SeongHoonC July 12, 2024 07:25
@tgyuuAn tgyuuAn self-assigned this Jul 12, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review July 14, 2024 12:01
Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํƒœ๊ทœ์ƒ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐ•์˜๋Š” ์˜ค๋Š˜๋„ ํฅ๋ฏธ๋กญ๊ตฐ์š”. ๋˜ ํ•˜๋‚˜ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค..

10^15 ๊นŒ์ง€๋ผ์„œ ํƒ€์ž…์„ ๋” ์‹ ๊ฒฝ์จ์•ผํ–ˆ๋Š”๋ฐ ๋ช‡ ๋ฒˆ์˜ ์˜ค๋ฅ˜๋์— ์„ฑ๊ณตํ–ˆ๋„ค์š”!

import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.math.abs

private lateinit var br: BufferedReader
fun main() {
    br = BufferedReader(InputStreamReader(System.`in`))
    val (n, k, q) = br.readLine().split(" ").map { it.toLong() }

    repeat(q.toInt()) {
        println(findDest(n, k))
    }

}

private fun findDest(n: Long, k: Long): Long {
    var (start, destination) = br.readLine().split(" ").map { it.toLong() }
    var cost = 0L

    if (k == 1L) {
        return abs(start - destination)
    }

    while (start != destination) {
        if (start > destination) {
            start = (start - 2) / k + 1
            cost += 1
        }
        if (start < destination) {
            destination = (destination - 2) / k + 1
            cost += 1
        }
    }
    return cost
}

@tgyuuAn tgyuuAn merged commit 2238bf6 into main Jul 26, 2024
@tgyuuAn tgyuuAn deleted the 64-tgyuuAn branch July 26, 2024 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ๋ฆฌ๋ทฐ ์™„๋ฃŒ โœ”๏ธ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants