結果
問題 |
No.2136 Dice Calendar?
|
ユーザー |
![]() |
提出日時 | 2022-10-10 15:28:20 |
言語 | Kotlin (2.1.0) |
結果 |
AC
|
実行時間 | 3,514 ms / 5,000 ms |
コード長 | 2,773 bytes |
コンパイル時間 | 17,423 ms |
コンパイル使用メモリ | 443,008 KB |
実行使用メモリ | 302,044 KB |
最終ジャッジ日時 | 2024-06-24 09:50:21 |
合計ジャッジ時間 | 42,611 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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 MOD = 998_244_353 val factorial = LongArray(N + 1) { 1L } (1..N).forEach { factorial[it] = factorial[it - 1] * it } /** 多重集合を管理、初期値は0要素の多重集合 */ val queue = mutableListOf(DiceSet(0b11111111, 0b01001_01000_00111_00110_00101_00100_00011_00010_00001_00000L)) val uniqueCheck = BitSet(1 shr 28) for (dice in S) { // diceを追加した新しい多重集合を求める val checkQueue = queue.toList() queue.clear() checkQueue.forEach { it.next(dice, uniqueCheck, queue) } uniqueCheck.clear() } val ans = queue.map { it.multiChoose(factorial) }.fold(0L) { l, r -> (l + r) % MOD } 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 ((1L shl (result * 5 + 5)) - 1).inv()) nextQueue.add(DiceSet(nextSet, nextPartition)) } } override fun toString(): String { val set = mutableListOf<Int>() (1..9).forEach { i -> repeat(getPartition(i) - getPartition(i - 1) - 1) { set.add(i) } } return set.toString() } }