結果

問題 No.827 総神童数
ユーザー rutilicusrutilicus
提出日時 2020-10-18 11:17:43
言語 Kotlin
(1.9.23)
結果
RE  
実行時間 -
コード長 1,559 bytes
コンパイル時間 16,661 ms
コンパイル使用メモリ 419,028 KB
実行使用メモリ 80,100 KB
最終ジャッジ日時 2023-09-28 08:51:18
合計ジャッジ時間 46,067 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 284 ms
52,908 KB
testcase_01 AC 289 ms
52,916 KB
testcase_02 AC 288 ms
53,068 KB
testcase_03 AC 318 ms
52,980 KB
testcase_04 AC 291 ms
53,032 KB
testcase_05 AC 289 ms
53,192 KB
testcase_06 AC 286 ms
52,820 KB
testcase_07 AC 270 ms
52,716 KB
testcase_08 AC 287 ms
52,980 KB
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 421 ms
57,188 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 AC 1,027 ms
79,052 KB
testcase_20 AC 837 ms
75,208 KB
testcase_21 AC 747 ms
70,876 KB
testcase_22 AC 904 ms
75,288 KB
testcase_23 AC 330 ms
53,240 KB
testcase_24 AC 933 ms
77,876 KB
testcase_25 AC 850 ms
73,580 KB
testcase_26 AC 981 ms
77,664 KB
testcase_27 AC 830 ms
71,284 KB
testcase_28 AC 768 ms
72,556 KB
testcase_29 AC 706 ms
68,496 KB
testcase_30 AC 537 ms
60,200 KB
testcase_31 AC 664 ms
66,604 KB
testcase_32 AC 633 ms
64,516 KB
testcase_33 AC 975 ms
77,724 KB
testcase_34 AC 925 ms
77,360 KB
testcase_35 AC 653 ms
66,620 KB
testcase_36 AC 666 ms
68,384 KB
testcase_37 AC 830 ms
73,512 KB
testcase_38 AC 963 ms
77,844 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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