結果

問題 No.673 カブトムシ
ユーザー rutilicus
提出日時 2020-12-20 11:27:04
言語 Kotlin
(2.1.0)
結果
WA  
実行時間 -
コード長 1,240 bytes
コンパイル時間 12,620 ms
コンパイル使用メモリ 436,120 KB
実行使用メモリ 56,988 KB
最終ジャッジ日時 2024-09-21 11:39:19
合計ジャッジ時間 19,902 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12 WA * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:9:17: warning: 'appendln(Long): kotlin.text.StringBuilder /* = java.lang.StringBuilder */' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
        builder.appendln((b % mod) * (d % mod) % mod)
                ^
Main.kt:13:17: warning: 'appendln(Long): kotlin.text.StringBuilder /* = java.lang.StringBuilder */' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator.
        builder.appendln(((b % mod * numerator % mod * denominator % mod) + mod - (b % mod)) % mod)
                ^

ソースコード

diff #

fun main() {
    val builder = StringBuilder()

    val mod = 1000000007L

    val (b, c, d) = readInputLine().split(" ").map { it.toLong() }

    if (c == 1L) {
        builder.appendln((b % mod) * (d % mod) % mod)
    } else {
        val numerator = (pow(c % mod, d + 1L, mod) + mod - 1L) % mod
        val denominator = modInv((c - 1L) % mod, mod) % mod
        builder.appendln(((b % mod * numerator % mod * denominator % mod) + mod - (b % mod)) % mod)
    }

    print(builder.toString())
}

fun readInputLine(): String {
    return readLine()!!
}

// a^-1 mod mod
fun modInv(a: Long, mod: Long): Long {
    var aLoc = a
    var b = mod
    var u = 1L
    var v = 0L
    while (b != 0L) {
        val t = aLoc / b
        aLoc -= t * b

        val swapTmpB = b
        b = aLoc
        aLoc = swapTmpB

        u -= t * v

        val swapTmpU = u
        u = v
        v = swapTmpU
    }

    u %= mod
    if (u < 0) {
        u += mod
    }
    return u
}

fun pow(x: Long, n: Long, mod: Long): Long {
    var ret = 1L
    var base = x
    var nTmp = n

    while (nTmp != 0L) {
        if (nTmp % 2L != 0L) {
            ret = ret * base % mod
        }
        base = base * base % mod
        nTmp /= 2L
    }

    return ret
}
0