結果

問題 No.2384 Permutations of Permutations
ユーザー 箱星箱星
提出日時 2023-02-18 17:49:51
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 360 ms / 2,000 ms
コード長 4,879 bytes
コンパイル時間 18,726 ms
コンパイル使用メモリ 456,420 KB
実行使用メモリ 57,296 KB
最終ジャッジ日時 2024-09-15 10:49:07
合計ジャッジ時間 28,135 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 314 ms
51,228 KB
testcase_01 AC 317 ms
51,264 KB
testcase_02 AC 325 ms
51,316 KB
testcase_03 AC 322 ms
51,132 KB
testcase_04 AC 306 ms
51,176 KB
testcase_05 AC 335 ms
57,184 KB
testcase_06 AC 331 ms
57,296 KB
testcase_07 AC 305 ms
51,168 KB
testcase_08 AC 304 ms
51,140 KB
testcase_09 AC 306 ms
51,152 KB
testcase_10 AC 355 ms
55,080 KB
testcase_11 AC 354 ms
55,308 KB
testcase_12 AC 354 ms
55,152 KB
testcase_13 AC 355 ms
55,416 KB
testcase_14 AC 360 ms
55,384 KB
testcase_15 AC 337 ms
53,348 KB
testcase_16 AC 330 ms
53,096 KB
testcase_17 AC 339 ms
55,308 KB
testcase_18 AC 327 ms
52,888 KB
testcase_19 AC 316 ms
51,380 KB
testcase_20 AC 314 ms
51,236 KB
testcase_21 AC 325 ms
52,800 KB
testcase_22 AC 313 ms
51,364 KB
testcase_23 AC 345 ms
54,776 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