結果

問題 No.2136 Dice Calendar?
ユーザー CuriousFairy315
提出日時 2022-11-22 20:24:08
言語 Kotlin
(2.1.0)
結果
AC  
実行時間 2,992 ms / 5,000 ms
コード長 2,374 bytes
コンパイル時間 14,545 ms
コンパイル使用メモリ 445,940 KB
実行使用メモリ 288,692 KB
最終ジャッジ日時 2024-09-23 04:50:32
合計ジャッジ時間 39,422 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*

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(0b111111111)
		val uniqueCheck = BitSet(1 shr N + 9) // 64MB程度
		for (dice in S) { // diceを追加した新しい多重集合を求める
			val checkQueue = queue.toList()
			queue.clear()
			checkQueue.forEach { next(it, dice, uniqueCheck, queue) }
			uniqueCheck.clear()
		}
		val ans = queue.map { multichoose(factorial, it) }.fold(0L) { l, r -> (l + r) % MOD }
		println(ans)
	}
}

val deBrujin32 = intArrayOf(0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9)

fun numberOfTrailingZeros(i: Int) =
	if (i == 0) 32 else deBrujin32[i * 0x077CB531 ushr 27] // 丁度1bit立っている値に対してその立っている位置を返す

fun calcPartition(multiSet: Int): Long { // 与えられた多重集合に対して、立っているbitの位置を保持する数列Pを返す
	var x = multiSet
	var partition = 0L
	for (i in 1..9) {
		val lob = x and -x
		partition += 1L + numberOfTrailingZeros(lob) shl i * 5
		x -= lob
	}
	return partition
}

fun getPartition(partition: Long, index: Int) = // multiSetでindex番目に立っているbitの位置を求める
	(partition shr 5 * index and 31).toInt()

fun multichoose(factorial: LongArray, multiSet: Int): Long { // multiSetで与えられた多重集合を並べてできる組合せ
	val partition = calcPartition(multiSet)
	var multichoose = factorial[getPartition(partition, 9) - 9]
	for (i in 0..8) multichoose /= factorial[getPartition(partition, i + 1) - getPartition(partition, i) - 1]
	return multichoose
}

fun next(multiSet: Int, dice: IntArray, set: BitSet, queue: MutableList<Int>) { // diceを追加したときにできる新たな多重集合のうち、新しく発見したものをqueueに入れる
	val partition = calcPartition(multiSet)
	for (result in dice) {
		val mask = (1 shl getPartition(partition, result)) - 1
		val next = (multiSet and mask.inv()) shl 1 or (multiSet and mask)
		if (!set[next]) {
			set.set(next)
			queue.add(next)
		}
	}
}
0