結果
問題 | No.2047 Path Factory |
ユーザー | 箱星 |
提出日時 | 2022-04-25 22:52:36 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 1,257 ms / 2,000 ms |
コード長 | 3,554 bytes |
コンパイル時間 | 13,619 ms |
コンパイル使用メモリ | 452,436 KB |
実行使用メモリ | 166,636 KB |
最終ジャッジ日時 | 2024-10-15 13:27:50 |
合計ジャッジ時間 | 32,667 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 278 ms
50,212 KB |
testcase_01 | AC | 280 ms
50,144 KB |
testcase_02 | AC | 291 ms
49,840 KB |
testcase_03 | AC | 278 ms
50,008 KB |
testcase_04 | AC | 278 ms
49,916 KB |
testcase_05 | AC | 281 ms
49,928 KB |
testcase_06 | AC | 277 ms
50,000 KB |
testcase_07 | AC | 759 ms
101,076 KB |
testcase_08 | AC | 622 ms
92,924 KB |
testcase_09 | AC | 693 ms
100,168 KB |
testcase_10 | AC | 728 ms
104,184 KB |
testcase_11 | AC | 622 ms
92,888 KB |
testcase_12 | AC | 551 ms
89,068 KB |
testcase_13 | AC | 575 ms
89,252 KB |
testcase_14 | AC | 644 ms
93,204 KB |
testcase_15 | AC | 670 ms
99,972 KB |
testcase_16 | AC | 603 ms
96,424 KB |
testcase_17 | AC | 600 ms
95,552 KB |
testcase_18 | AC | 665 ms
99,152 KB |
testcase_19 | AC | 663 ms
104,216 KB |
testcase_20 | AC | 798 ms
120,208 KB |
testcase_21 | AC | 676 ms
98,488 KB |
testcase_22 | AC | 444 ms
66,736 KB |
testcase_23 | AC | 463 ms
70,796 KB |
testcase_24 | AC | 457 ms
67,884 KB |
testcase_25 | AC | 922 ms
166,636 KB |
testcase_26 | AC | 1,257 ms
153,212 KB |
testcase_27 | AC | 1,069 ms
120,116 KB |
testcase_28 | AC | 874 ms
117,748 KB |
testcase_29 | AC | 287 ms
51,024 KB |
ソースコード
import java.io.PrintWriter import java.util.* import kotlin.math.* fun PrintWriter.solve() { val n = nextInt() val adj = Array(n) { mutableListOf<Int>() } for (i in 0 until n - 1) { val v1 = nextInt() - 1 val v2 = nextInt() - 1 adj[v1].add(v2) adj[v2].add(v1) } val dp = Array(n) { ModIntArray(3) { ModInt(0) } } fun dfs(v: Int, par: Int) { var p = ModInt(1) var s = ModInt(0) var t = ModInt(0) for (w in adj[v]) { if (w != par) { dfs(w, v) t = t * (dp[w][1] + dp[w][2]) + s * (dp[w][0] + dp[w][1]) s = s * (dp[w][1] + dp[w][2]) + p * (dp[w][0] + dp[w][1]) p *= dp[w][1] + dp[w][2] } } dp[v][0] = p dp[v][1] = s dp[v][2] = t } dfs(0, -1) println(dp[0][1] + dp[0][2]) } // region ModInt class ModInt(x: Long) { companion object { const val MOD = 998244353L //const val MOD = 1000000007L } constructor(y: Int) : this(y.toLong()) val x = (x % MOD + MOD) % MOD operator fun plus(other: ModInt): ModInt { return ModInt(x + other.x) } operator fun minus(other: ModInt): ModInt { return ModInt(x - other.x) } operator fun times(other: ModInt): ModInt { return ModInt(x * other.x) } operator fun div(other: ModInt): ModInt { return this * other.inv() } fun pow(exp: Long): ModInt { if (exp == 0L) return ModInt(1L) var a = pow(exp shr 1) a *= a if (exp and 1L == 0L) return a return this * a } fun inv(): ModInt { return this.pow(MOD - 2) } override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false other as ModInt if (x != other.x) return false return true } override fun hashCode(): Int { return x.hashCode() } override fun toString(): String { return "$x" } } class ModIntArray(n: Int) { private val a = LongArray(n) { 0L } constructor(n: Int, init: (Int) -> ModInt) : this(n) { for (i in 0 until n) { a[i] = init(i).x } } operator fun set(i: Int, x: ModInt) { a[i] = x.x } operator fun get(i: Int): ModInt { return ModInt(a[i]) } fun joinToString(s: String = ", "): String { return a.joinToString(s) } } fun Long.toModInt(): ModInt { return ModInt(this) } fun Int.toModInt(): ModInt { return ModInt(this) } fun binomSimple(n: Long, k: Long): ModInt { var numer = ModInt(1) var denom = ModInt(1) for (i in 0 until k) { numer *= ModInt(n - i) denom *= ModInt(k - i) } return numer / denom } // endregion fun main() { Thread(null, { val writer = PrintWriter(System.out, false) writer.solve() writer.flush() }, "solve", abs(1L.shl(26))) .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } } .apply { start() }.join() } // region Scanner private var st = StringTokenizer("") private val br = System.`in`.bufferedReader() fun next(): String { while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) return st.nextToken() } fun nextInt() = next().toInt() fun nextLong() = next().toLong() fun nextLine() = br.readLine() fun nextDouble() = next().toDouble() // endregion