結果

問題 No.2136 Dice Calendar?
ユーザー CuriousFairy315CuriousFairy315
提出日時 2022-10-14 12:36:07
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 4,398 ms / 5,000 ms
コード長 2,444 bytes
コンパイル時間 15,870 ms
コンパイル使用メモリ 424,476 KB
実行使用メモリ 308,864 KB
最終ジャッジ日時 2023-09-08 19:38:56
合計ジャッジ時間 47,570 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 358 ms
54,032 KB
testcase_01 AC 339 ms
54,412 KB
testcase_02 AC 401 ms
57,284 KB
testcase_03 AC 334 ms
54,120 KB
testcase_04 AC 332 ms
54,112 KB
testcase_05 AC 361 ms
54,484 KB
testcase_06 AC 364 ms
54,348 KB
testcase_07 AC 370 ms
54,744 KB
testcase_08 AC 391 ms
56,404 KB
testcase_09 AC 422 ms
57,312 KB
testcase_10 AC 428 ms
57,260 KB
testcase_11 AC 442 ms
59,236 KB
testcase_12 AC 494 ms
62,116 KB
testcase_13 AC 448 ms
59,328 KB
testcase_14 AC 505 ms
64,092 KB
testcase_15 AC 818 ms
87,604 KB
testcase_16 AC 922 ms
112,124 KB
testcase_17 AC 793 ms
118,412 KB
testcase_18 AC 1,584 ms
183,596 KB
testcase_19 AC 1,898 ms
227,588 KB
testcase_20 AC 2,164 ms
244,144 KB
testcase_21 AC 2,569 ms
308,864 KB
testcase_22 AC 3,111 ms
307,600 KB
testcase_23 AC 4,398 ms
307,900 KB
testcase_24 AC 417 ms
159,996 KB
testcase_25 AC 562 ms
164,316 KB
testcase_26 AC 3,608 ms
307,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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))
		}
	}
}
0