結果

問題 No.827 総神童数
ユーザー rutilicusrutilicus
提出日時 2020-10-18 11:22:42
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,088 ms / 2,000 ms
コード長 1,715 bytes
コンパイル時間 13,741 ms
コンパイル使用メモリ 430,400 KB
実行使用メモリ 91,520 KB
最終ジャッジ日時 2023-09-28 08:52:10
合計ジャッジ時間 42,450 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 285 ms
52,868 KB
testcase_01 AC 287 ms
52,896 KB
testcase_02 AC 284 ms
53,136 KB
testcase_03 AC 283 ms
53,092 KB
testcase_04 AC 281 ms
52,924 KB
testcase_05 AC 292 ms
52,836 KB
testcase_06 AC 287 ms
53,052 KB
testcase_07 AC 275 ms
52,640 KB
testcase_08 AC 288 ms
52,928 KB
testcase_09 AC 1,011 ms
91,520 KB
testcase_10 AC 662 ms
66,380 KB
testcase_11 AC 413 ms
57,208 KB
testcase_12 AC 544 ms
60,116 KB
testcase_13 AC 883 ms
78,640 KB
testcase_14 AC 472 ms
57,856 KB
testcase_15 AC 643 ms
66,520 KB
testcase_16 AC 874 ms
78,480 KB
testcase_17 AC 914 ms
89,360 KB
testcase_18 AC 659 ms
68,532 KB
testcase_19 AC 1,040 ms
89,776 KB
testcase_20 AC 881 ms
77,760 KB
testcase_21 AC 792 ms
71,596 KB
testcase_22 AC 940 ms
77,944 KB
testcase_23 AC 331 ms
53,184 KB
testcase_24 AC 1,005 ms
84,096 KB
testcase_25 AC 873 ms
75,824 KB
testcase_26 AC 1,017 ms
86,348 KB
testcase_27 AC 888 ms
74,080 KB
testcase_28 AC 870 ms
73,188 KB
testcase_29 AC 758 ms
70,620 KB
testcase_30 AC 568 ms
60,312 KB
testcase_31 AC 720 ms
66,684 KB
testcase_32 AC 677 ms
66,524 KB
testcase_33 AC 1,088 ms
85,948 KB
testcase_34 AC 1,049 ms
86,084 KB
testcase_35 AC 754 ms
66,988 KB
testcase_36 AC 722 ms
70,528 KB
testcase_37 AC 877 ms
75,316 KB
testcase_38 AC 1,024 ms
86,268 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:57: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(ans)
            ^

ソースコード

diff #

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