結果

問題 No.2384 Permutations of Permutations
ユーザー 👑 箱
提出日時 2023-04-09 15:55:56
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 360 ms / 2,000 ms
コード長 3,765 bytes
コンパイル時間 17,187 ms
コンパイル使用メモリ 430,392 KB
実行使用メモリ 57,788 KB
最終ジャッジ日時 2023-10-13 14:02:27
合計ジャッジ時間 25,596 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 295 ms
52,824 KB
testcase_01 AC 300 ms
53,016 KB
testcase_02 AC 298 ms
52,784 KB
testcase_03 AC 300 ms
52,912 KB
testcase_04 AC 298 ms
52,860 KB
testcase_05 AC 327 ms
55,784 KB
testcase_06 AC 331 ms
55,508 KB
testcase_07 AC 301 ms
53,072 KB
testcase_08 AC 299 ms
53,308 KB
testcase_09 AC 300 ms
52,796 KB
testcase_10 AC 360 ms
57,052 KB
testcase_11 AC 349 ms
57,788 KB
testcase_12 AC 346 ms
57,188 KB
testcase_13 AC 350 ms
57,108 KB
testcase_14 AC 350 ms
57,168 KB
testcase_15 AC 321 ms
55,720 KB
testcase_16 AC 317 ms
55,432 KB
testcase_17 AC 331 ms
55,792 KB
testcase_18 AC 314 ms
55,620 KB
testcase_19 AC 306 ms
52,892 KB
testcase_20 AC 302 ms
52,904 KB
testcase_21 AC 312 ms
55,364 KB
testcase_22 AC 301 ms
52,820 KB
testcase_23 AC 329 ms
55,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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
0