結果
| 問題 |
No.2136 Dice Calendar?
|
| コンテスト | |
| ユーザー |
CuriousFairy315
|
| 提出日時 | 2022-10-14 12:36:07 |
| 言語 | Kotlin (2.1.0) |
| 結果 |
AC
|
| 実行時間 | 3,881 ms / 5,000 ms |
| コード長 | 2,444 bytes |
| コンパイル時間 | 13,994 ms |
| コンパイル使用メモリ | 441,220 KB |
| 実行使用メモリ | 309,460 KB |
| 最終ジャッジ日時 | 2024-06-26 12:34:35 |
| 合計ジャッジ時間 | 42,769 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 24 |
ソースコード
import java.util.Scanner
import java.util.BitSet
fun main() {
Scanner(System.`in`).use { sc ->
val N = sc.nextInt()
val S = Array(N) { IntArray(6) { sc.nextInt() - 1 } }
val factorial = LongArray(N + 1) { 1L }
(1..N).forEach { factorial[it] = factorial[it - 1] * it }
val queue = mutableListOf(DiceSet(0b111111111, 0b01001_01000_00111_00110_00101_00100_00011_00010_00001_00000L)) // 初項M_0を求める
val uniqueCheck = BitSet(1 shr 29) // 64MB程度
for (dice in S) {
val checkQueue = queue.toList()
queue.clear()
checkQueue.forEach { it.next(dice, uniqueCheck, queue) } // M_iからM_{i+1}を求める
}
val ans = queue.map { it.multiChoose(factorial) }.fold(0L) { l, r -> (l + r) % 998_244_353 }
println(ans)
}
}
/**
* ダイスから作られる整数の多重集合を管理するクラスです。
* @property multiSet 多重集合を、仕切りの考え方で見なした時のbit列
* @property partition [multiSet]で立っているbitの位置、5bitごとに管理
*/
data class DiceSet(val multiSet: Int, val partition: Long) {
/** [multiSet]でi番目に立っているbitの位置を求める */
private fun getPartition(index: Int) = ((partition shr 5 * index) and 0b11111).toInt()
/** この多重集合を並べてできる組合せを求めます。
* @param factorial 階乗
* @return この多重集合を並べてできる組合せ
*/
fun multiChoose(factorial: LongArray) =
(0..8).map { factorial[getPartition(it + 1) - getPartition(it) - 1] }
.fold(factorial[getPartition(9) - 9]) { l, r -> l / r }
/**
* [dice]を追加したときにできる新たな多重集合のうち、新しく発見したものを[nextQueue]に入れます。
* @param dice 追加するサイコロ
* @param uniqueCheck 既に調べた多重集合か判定するためのBitSet
* @param nextQueue まだ調べていない次の多重集合を入れるためのキュー
*/
fun next(dice: IntArray, uniqueCheck: BitSet, nextQueue: MutableCollection<DiceSet>) {
for (result in dice) {
val mask = (1 shl getPartition(result)) - 1
val nextSet = ((multiSet and mask.inv()) shl 1) or (multiSet and mask)
if (uniqueCheck.get(nextSet)) continue
uniqueCheck.set(nextSet)
val nextPartition =
partition + (0b00001_00001_00001_00001_00001_00001_00001_00001_00001_00001L and ((0x20L shl result * 5) - 1).inv())
nextQueue.add(DiceSet(nextSet, nextPartition))
}
}
}
CuriousFairy315