結果

問題 No.2136 Dice Calendar?
ユーザー CuriousFairy315CuriousFairy315
提出日時 2022-11-22 20:24:08
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 2,974 ms / 5,000 ms
コード長 2,374 bytes
コンパイル時間 14,918 ms
コンパイル使用メモリ 443,436 KB
実行使用メモリ 290,196 KB
最終ジャッジ日時 2023-10-24 12:30:37
合計ジャッジ時間 42,582 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 350 ms
55,180 KB
testcase_01 AC 347 ms
55,172 KB
testcase_02 AC 397 ms
57,392 KB
testcase_03 AC 348 ms
55,184 KB
testcase_04 AC 343 ms
55,188 KB
testcase_05 AC 365 ms
55,240 KB
testcase_06 AC 360 ms
55,256 KB
testcase_07 AC 370 ms
55,264 KB
testcase_08 AC 388 ms
55,260 KB
testcase_09 AC 399 ms
57,320 KB
testcase_10 AC 415 ms
58,192 KB
testcase_11 AC 440 ms
58,276 KB
testcase_12 AC 486 ms
60,460 KB
testcase_13 AC 444 ms
58,384 KB
testcase_14 AC 513 ms
64,560 KB
testcase_15 AC 718 ms
95,456 KB
testcase_16 AC 799 ms
95,956 KB
testcase_17 AC 706 ms
107,504 KB
testcase_18 AC 1,382 ms
174,012 KB
testcase_19 AC 1,554 ms
205,848 KB
testcase_20 AC 1,814 ms
258,392 KB
testcase_21 AC 2,093 ms
272,800 KB
testcase_22 AC 2,579 ms
273,868 KB
testcase_23 AC 2,974 ms
290,196 KB
testcase_24 AC 444 ms
161,228 KB
testcase_25 AC 591 ms
165,616 KB
testcase_26 AC 2,815 ms
284,012 KB
権限があれば一括ダウンロードができます

ソースコード

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