結果

問題 No.827 総神童数
ユーザー rutilicus
提出日時 2020-10-18 11:17:43
言語 Kotlin
(2.1.0)
結果
RE  
実行時間 -
コード長 1,559 bytes
コンパイル時間 15,836 ms
コンパイル使用メモリ 442,280 KB
実行使用メモリ 106,112 KB
最終ジャッジ日時 2024-07-21 03:21:49
合計ジャッジ時間 44,539 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27 RE * 9
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:49:13: 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(dfs(0, -1, 1))
            ^

ソースコード

diff #

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
    }

    fun dfs(node: Int, parent: Int, depth: Int): Long {
        var ret = nCm(n, depth) * factorial[depth - 1] % mod * factorial[n - depth]
        for (e in edges[node]) {
            if (e == parent) {
                continue
            }
            ret += dfs(e, node, depth + 1)
            ret %= mod
        }
        return ret
    }

    builder.appendln(dfs(0, -1, 1))

    print(builder.toString())
}

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