結果

問題 No.2384 Permutations of Permutations
ユーザー 👑 箱
提出日時 2023-02-18 17:49:51
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 379 ms / 2,000 ms
コード長 4,879 bytes
コンパイル時間 19,739 ms
コンパイル使用メモリ 438,000 KB
実行使用メモリ 59,624 KB
最終ジャッジ日時 2023-10-13 14:02:00
合計ジャッジ時間 28,693 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 299 ms
54,724 KB
testcase_01 AC 298 ms
52,912 KB
testcase_02 AC 291 ms
55,128 KB
testcase_03 AC 295 ms
52,908 KB
testcase_04 AC 297 ms
52,904 KB
testcase_05 AC 326 ms
57,336 KB
testcase_06 AC 325 ms
55,912 KB
testcase_07 AC 293 ms
54,812 KB
testcase_08 AC 294 ms
53,100 KB
testcase_09 AC 299 ms
53,156 KB
testcase_10 AC 379 ms
57,152 KB
testcase_11 AC 345 ms
57,180 KB
testcase_12 AC 350 ms
59,624 KB
testcase_13 AC 350 ms
58,756 KB
testcase_14 AC 348 ms
57,084 KB
testcase_15 AC 320 ms
56,000 KB
testcase_16 AC 310 ms
55,188 KB
testcase_17 AC 331 ms
57,488 KB
testcase_18 AC 313 ms
56,648 KB
testcase_19 AC 295 ms
52,804 KB
testcase_20 AC 295 ms
53,392 KB
testcase_21 AC 308 ms
57,308 KB
testcase_22 AC 297 ms
52,936 KB
testcase_23 AC 323 ms
57,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.PrintWriter
import java.util.*
import kotlin.math.*

// 置換の積を計算
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 PrintWriter.solve() {
    val n = nextInt()
    val k = nextInt()
    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 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
        //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"
    }
}

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
0