結果

問題 No.2136 Dice Calendar?
ユーザー CuriousFairy315CuriousFairy315
提出日時 2022-10-21 08:41:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,803 ms / 5,000 ms
コード長 1,939 bytes
コンパイル時間 235 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 319,508 KB
最終ジャッジ日時 2024-06-30 20:28:46
合計ジャッジ時間 30,745 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
S = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
factorial = [1]
for i in range(N): factorial.append(factorial[i] * (i + 1))

partition = [0] * 10
partition[9] = N + 9

deBrujin32 = [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]

def numberOfTrailingZeros(i): return deBrujin32[i * 0x077CB531 >> 27 & 0b11111] # 丁度1bit立っている値に対してその立っている位置を返す

def calcPartition(multiSet): # 与えられた多重集合に対して、立っているbitの位置を保持する数列Pを返す
	for i in range(1, 9):
		lob = multiSet & -multiSet
		partition[i] = numberOfTrailingZeros(lob) + 1
		multiSet -= lob

def multichoose(multiSet): # multiSetで与えられた多重集合を並べてできる組合せ
	calcPartition(multiSet)
	ret = factorial[N]
	for i in range(9): ret //= factorial[partition[i + 1] - partition[i] - 1]
	return ret

def nextSet(multiSet, dice, uniqueCheck, nextList): # diceを追加したときの多重集合をnextListに入れる
	calcPartition(multiSet)
	for result in dice: # 出目がresultだった時
		mask = (1 << partition[result]) - 1
		nextSet = (multiSet & 0x1FFFFFFF - mask) << 1 | (multiSet & mask)
		if (uniqueCheck[nextSet >> 6] & 1 << (nextSet & 0x3F)) == 0: # まだこの多重集合を計算対象にしていないなら
			uniqueCheck[nextSet >> 6] |= 1 << (nextSet & 0x3F)
			nextList.append(nextSet)

nowList = [0b11111111] # 初項M_0を求める
uniqueCheck = [0] * (1 << N + 2) # 既に調べた多重集合を管理するためのBitSet、32MB程度
for dice in S:
	for i in nowList: uniqueCheck[i >> 6] = 0
	nextList = []
	for multiSet in nowList: nextSet(multiSet, dice, uniqueCheck, nextList) # M_iからM_{i+1}を求める
	nowList = nextList

ans = 0
for multiSet in nowList: ans += multichoose(multiSet)
ans %= 998_244_353
print(ans)
0