結果
| 問題 |
No.2384 Permutations of Permutations
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-18 17:49:51 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
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