Problem:
Score: 400 points

Problem Statement

The Kingdom of Takahashi has N towns, numbered 1 through N.

There is one teleporter in each town. The teleporter in Town i (1 \leq i \leq N) sends you to Town A_i.

Takahashi, the king, loves the positive integer K. The selfish king wonders what town he will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Help the king by writing a program that answers this question.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq N

1 \leq K \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the integer representing the town the king will be in if he starts at Town 1 and uses a teleporter exactly K times from there.

Sample Input 1

4 5
3 2 4 1

Sample Output 1

4

If we start at Town 1 and use the teleporter 5 times, our travel will be as follows: 1 \to 3 \to 4 \to 1 \to 3 \to 4.

Sample Input 2

6 727202214173249351
6 5 2 5 3 2

Sample Output 2

2

Solution in Kotlin (1.3.71) (judge verdict: Accepted)

import java.io.PrintWriter

fun main() = abc167d()

@OptIn(ExperimentalStdlibApi::class)
fun abc167d() = IO_abc167d().exec {
    val (n, k) = readLongList(2)
    val aList = readIntList(n.toInt()).map { it - 1 }.toIntArray()

    val visited = mutableSetOf(0)
    val indices = mutableListOf(0)
    var outerLoopLength = 0
    var currIndex = 0

    while (true) {
        currIndex = aList[currIndex]
        if (!visited.add(currIndex)) break

        outerLoopLength++
        indices.add(currIndex)
    }

    if (outerLoopLength >= k) return@exec println(indices[k.toInt()] + 1)

    val visitedInLoop = mutableSetOf<Int>()
    var loopLength = 0L
    val path = LongArray(n.toInt()) { -1L }

    while (true) {
        if (!visitedInLoop.add(currIndex)) break
        path[currIndex] = loopLength
        loopLength++
        currIndex = aList[currIndex]
    }

    val answer = path.indexOfFirst { it == (k - outerLoopLength - 1) % loopLength } + 1

    println(answer)
}

//region
@Suppress("unused", "ClassName", "SpellCheckingInspection", "ConvertToStringTemplate")
private class IO_abc167d {
    private val input = System.`in`
    private val buffer = ByteArray(1024)
    private var pointer = 0
    private var bufferLength = 0
    private val pw = PrintWriter(System.out)
    private fun Byte.isPrintable() = this in 33..126
    private fun Byte.isNumeric() = this in '0'.toByte()..'9'.toByte()
    private fun Byte.toNumVal() =
        if (this.isNumeric()) this - '0'.toByte() else error(this.toString() + " is not numeric")

    private fun hasNextByte(): Boolean {
        return if (pointer < bufferLength) true else {
            pointer = 0
            bufferLength = input.read(buffer)
            bufferLength > 0
        }
    }

    private fun readByte(): Byte = if (hasNextByte()) buffer[pointer++] else -1
    private fun skipUnprintable() = run { while (hasNextByte() && !buffer[pointer].isPrintable()) pointer++ }
    private fun hasNext(): Boolean = run { skipUnprintable() }.run { hasNextByte() }
    private fun hasNextOrError() = if (!hasNext()) error("has no next element.") else Unit

    fun readString(): String {
        hasNextOrError()
        val sb = StringBuilder()
        var b = readByte()
        while (b.isPrintable()) {
            sb.appendCodePoint(b.toInt())
            b = readByte()
        }
        return sb.toString()
    }

    fun readLong(): Long {
        hasNextOrError()
        var n = 0L
        var negative = false
        var b = readByte()
        if (b == '-'.toByte()) {
            negative = true
            b = readByte()
        }
        if (!b.isNumeric()) error(b.toString() + " is not numeric.")
        while (true) {
            when {
                b.isNumeric() -> n = n * 10 + b.toNumVal()
                b.toInt() == -1 || !b.isPrintable() -> return if (negative) -n else n
                else -> error("failed to parse. [n=" + n + ", b=" + b + "]")
            }
            b = readByte()
        }
    }

    fun readInt() = readLong()
        .let { if (it in Int.MIN_VALUE..Int.MAX_VALUE) it.toInt() else error(it.toString() + " is not in range of Int.") }

    fun readDouble(): Double {
        var n = 0.0
        var div = 1.0
        var negative = false
        var b = readByte()
        if (b == '-'.toByte()) {
            negative = true
            b = readByte()
        }
        do n = n * 10 + b.toNumVal()
        while (run { b = readByte() }.run { b.isNumeric() })
        if (b == '.'.toByte()) {
            while (run { b = readByte() }.run { b.isNumeric() })
                n += b.toNumVal() / (run { div *= 10 }.run { div })
        }
        return if (negative) -n else n
    }

    fun readStringList(size: Int) = readList(size) { readString() }
    fun readIntList(size: Int) = readList(size) { readInt() }
    fun readLongList(size: Int) = readList(size) { readLong() }
    fun readDoubleList(size: Int) = readList(size) { readDouble() }
    fun readIntArray(size: Int) = IntArray(size) { readInt() }
    fun readLongArray(size: Int) = LongArray(size) { readLong() }
    fun readDoubleArray(size: Int) = DoubleArray(size) { readDouble() }
    inline fun <reified T> readArray(size: Int, init: () -> T) = Array(size) { init() }
    inline fun <T> readList(size: Int, init: () -> T) = List(size) { init() }
    fun println() = pw.println()
    fun print(o: Any?) = pw.print(o)
    fun println(o: Any?) = pw.println(o)
    fun exec(code: IO_abc167d.() -> Unit) = run { code() }.run { pw.flush() }
}
// endregion