結果
問題 | No.2384 Permutations of Permutations |
ユーザー | 箱星 |
提出日時 | 2023-04-09 15:55:56 |
言語 | Kotlin (1.9.23) |
結果 |
AC
|
実行時間 | 367 ms / 2,000 ms |
コード長 | 3,765 bytes |
コンパイル時間 | 15,523 ms |
コンパイル使用メモリ | 455,264 KB |
実行使用メモリ | 57,888 KB |
最終ジャッジ日時 | 2024-09-15 10:49:34 |
合計ジャッジ時間 | 24,922 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 313 ms
51,548 KB |
testcase_01 | AC | 319 ms
51,932 KB |
testcase_02 | AC | 319 ms
51,796 KB |
testcase_03 | AC | 317 ms
51,780 KB |
testcase_04 | AC | 314 ms
51,724 KB |
testcase_05 | AC | 346 ms
57,888 KB |
testcase_06 | AC | 343 ms
57,804 KB |
testcase_07 | AC | 318 ms
51,564 KB |
testcase_08 | AC | 313 ms
51,776 KB |
testcase_09 | AC | 315 ms
51,740 KB |
testcase_10 | AC | 367 ms
55,696 KB |
testcase_11 | AC | 361 ms
55,952 KB |
testcase_12 | AC | 361 ms
55,756 KB |
testcase_13 | AC | 363 ms
55,876 KB |
testcase_14 | AC | 365 ms
55,788 KB |
testcase_15 | AC | 341 ms
54,184 KB |
testcase_16 | AC | 333 ms
53,368 KB |
testcase_17 | AC | 344 ms
55,916 KB |
testcase_18 | AC | 343 ms
53,512 KB |
testcase_19 | AC | 322 ms
52,004 KB |
testcase_20 | AC | 313 ms
51,628 KB |
testcase_21 | AC | 333 ms
53,296 KB |
testcase_22 | AC | 319 ms
51,960 KB |
testcase_23 | AC | 339 ms
55,404 KB |
ソースコード
fun main() { val (n, k) = readln().split(" ").map { it.toInt() } var ans = ModInt(k) * (1..n - k).map { ModInt(it) }.fold(ModInt(1), ModInt::times) if (n == 6) { val c = (1..n).toMutableList() var count = 0 do { var ok = true val f = Array(n) { it + 1 } for (i in 0 until k - 1) { f[i] = i + 2 } f[k - 1] = 1 val g = image(conjugate(f, c.toTypedArray())) for (i in 0 until k - 1) { if (g[i] != i + 2) { ok = false break } } if (g[k - 1] != 1) ok = false if (ok) count++ } while (nextPermutation(c)) ans += ModInt(count) } else if (n == 2) { ans = ModInt(1) } println(ans) } // 置換の積を計算 fun prod(p: Array<Int>, q: Array<Int>): Array<Int> { val n = p.size val r = Array(n) { 0 } for (i in 0 until n) { r[i] = q[p[i] - 1] } return r } // 置換aに置換cを共役として作用させる fun conjugate(a: Array<Int>, c: Array<Int>): Array<Int> { val b = Array(6) { 0 } for (i in 0 until 6) { b[c[i] - 1] = c[a[i] - 1] } return b } // ある外部自己同型による像を計算 fun image(a: Array<Int>): Array<Int> { val n = 6 var p = Array(n) { it + 1 } val base = arrayOf( arrayOf(2, 1, 4, 3, 6, 5), // (1,2)の像 arrayOf(4, 5, 6, 1, 2, 3), // (2,3)の像 arrayOf(3, 4, 1, 2, 6, 5), // (3,4)の像 arrayOf(2, 1, 6, 5, 4, 3), // (4,5)の像 arrayOf(4, 3, 2, 1, 6, 5) // (5,6)の像 ) for (i in 0 until n) { for (j in 1 until n) { if (a[j] < a[j - 1]) { a[j] = a[j - 1].also { a[j - 1] = a[j] } p = prod(p, base[j - 1]) } } } return p } fun nextPermutation(array: MutableList<Int>): Boolean { for (i in array.size - 2 downTo 0) { if (array[i] < array[i + 1]) { for (j in array.size - 1 downTo i + 1) { if (array[j] > array[i]) { val tmp = array[i] array[i] = array[j] array[j] = tmp val tmpArray = array.takeLast(array.size - (i + 1)).sorted() tmpArray.forEachIndexed { index, e -> array[i + 1 + index] = e } return true } } } } return false } // region ModInt class ModInt(x: Long) { companion object { const val MOD = 998244353L } 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" } } // endregion