結果

問題 No.827 総神童数
コンテスト
ユーザー rutilicus
提出日時 2020-10-18 11:22:42
言語 Kotlin
(2.3.20)
コンパイル:
kotlinc _filename_ -include-runtime -d main.jar
実行:
kotlin main.jar
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,715 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 8,951 ms
コンパイル使用メモリ 443,852 KB
最終ジャッジ日時 2026-04-03 03:15:05
合計ジャッジ時間 9,554 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge5_1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
Main.kt:57:13: error: 'fun StringBuilder.appendln(value: Long): 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(ans)
            ^^^^^^^^

ソースコード

diff #
raw source code

import java.util.ArrayDeque

fun main() {
    val builder = StringBuilder()

    val mod = 1000000007L

    // 解説読むとなんでこんな単純な数え上げもできないのかと嫌になりますね

    val n = readInputLine().toInt()
    val edges = Array(n) { mutableListOf<Int>() }

    repeat(n - 1) {
        val (u, v) = readInputLine().split(" ").map { it.toInt() - 1 }
        edges[u].add(v)
        edges[v].add(u)
    }

    // 1~nの階乗、逆元
    val factorial = LongArray(n + 1)
    val inverse = LongArray(n + 1)
    val factorialInverse = LongArray(n + 1)
    factorial[0] = 1L
    factorial[1] = 1L
    inverse[0] = 1L
    inverse[1] = 1L
    factorialInverse[0] = 1L
    factorialInverse[1] = 1L
    for (i in 2..n) {
        factorial[i] = (factorial[i - 1] * i.toLong() % mod)
        inverse[i] = (mod - inverse[mod.toInt() % i] * (mod / i.toLong()) % mod)
        factorialInverse[i] = (factorialInverse[i - 1] * inverse[i] % mod)
    }

    fun nCm(n: Int, m: Int): Long {
        return factorial[n] * (factorialInverse[m] * factorialInverse[n - m] % mod) % mod
    }

    // (node, parent, depth)
    val queue = ArrayDeque<Triple<Int, Int, Int>>()
    queue.add(Triple(0, -1, 1))

    var ans = 0L

    while (queue.isNotEmpty()) {
        val (node, parent, depth) = queue.poll()!!
        ans += nCm(n, depth) * factorial[depth - 1] % mod * factorial[n - depth]
        ans %= mod
        for (e in edges[node]) {
            if (e == parent) {
                continue
            }
            queue.add(Triple(e, node, depth + 1))
        }
    }

    builder.appendln(ans)

    print(builder.toString())
}

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