結果

問題 No.2136 Dice Calendar?
ユーザー CuriousFairy315CuriousFairy315
提出日時 2022-10-10 15:28:20
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 4,328 ms / 5,000 ms
コード長 2,773 bytes
コンパイル時間 15,762 ms
コンパイル使用メモリ 428,056 KB
実行使用メモリ 308,692 KB
最終ジャッジ日時 2023-09-06 15:25:53
合計ジャッジ時間 47,862 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 345 ms
54,048 KB
testcase_01 AC 355 ms
53,920 KB
testcase_02 AC 419 ms
57,148 KB
testcase_03 AC 351 ms
54,252 KB
testcase_04 AC 355 ms
54,228 KB
testcase_05 AC 381 ms
54,456 KB
testcase_06 AC 379 ms
54,968 KB
testcase_07 AC 377 ms
54,572 KB
testcase_08 AC 394 ms
58,300 KB
testcase_09 AC 410 ms
57,276 KB
testcase_10 AC 416 ms
57,252 KB
testcase_11 AC 452 ms
59,532 KB
testcase_12 AC 510 ms
63,064 KB
testcase_13 AC 457 ms
59,368 KB
testcase_14 AC 547 ms
64,796 KB
testcase_15 AC 846 ms
96,652 KB
testcase_16 AC 920 ms
98,544 KB
testcase_17 AC 813 ms
91,968 KB
testcase_18 AC 1,595 ms
177,644 KB
testcase_19 AC 1,874 ms
201,136 KB
testcase_20 AC 2,125 ms
212,604 KB
testcase_21 AC 2,671 ms
265,284 KB
testcase_22 AC 3,386 ms
294,148 KB
testcase_23 AC 4,328 ms
302,300 KB
testcase_24 AC 366 ms
60,908 KB
testcase_25 AC 566 ms
111,540 KB
testcase_26 AC 3,731 ms
308,692 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 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()
	}
}
0