結果

問題 No.2136 Dice Calendar?
ユーザー CuriousFairy315CuriousFairy315
提出日時 2022-10-14 13:02:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,923 ms / 5,000 ms
コード長 2,523 bytes
コンパイル時間 3,217 ms
コンパイル使用メモリ 83,416 KB
実行使用メモリ 408,076 KB
最終ジャッジ日時 2023-09-08 19:40:08
合計ジャッジ時間 17,287 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 155 ms
83,200 KB
testcase_01 AC 154 ms
83,672 KB
testcase_02 AC 175 ms
83,752 KB
testcase_03 AC 150 ms
83,636 KB
testcase_04 AC 151 ms
83,300 KB
testcase_05 AC 161 ms
83,316 KB
testcase_06 AC 154 ms
83,424 KB
testcase_07 AC 160 ms
83,528 KB
testcase_08 AC 172 ms
83,356 KB
testcase_09 AC 184 ms
83,252 KB
testcase_10 AC 179 ms
83,604 KB
testcase_11 AC 210 ms
84,020 KB
testcase_12 AC 226 ms
85,664 KB
testcase_13 AC 198 ms
83,304 KB
testcase_14 AC 232 ms
88,456 KB
testcase_15 AC 399 ms
112,592 KB
testcase_16 AC 426 ms
112,056 KB
testcase_17 AC 334 ms
121,668 KB
testcase_18 AC 696 ms
173,548 KB
testcase_19 AC 918 ms
226,924 KB
testcase_20 AC 1,008 ms
234,404 KB
testcase_21 AC 1,195 ms
328,316 KB
testcase_22 AC 1,295 ms
320,672 KB
testcase_23 AC 1,923 ms
408,076 KB
testcase_24 AC 170 ms
146,932 KB
testcase_25 AC 253 ms
154,352 KB
testcase_26 AC 1,450 ms
341,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			int N = sc.nextInt();
			int[][] S = new int[N][6];
			for (int i = 0;i < N;++ i) for (int j = 0;j < 6;++ j) S[i][j] = sc.nextInt() - 1;
			long[] factorial = new long[N + 1];
			factorial[0] = 1;
			for (int i = 1;i < factorial.length;++ i) factorial[i] = factorial[i - 1] * i;
			
			BitSet checkUnique = new BitSet(1 << N + 9); // 64MB程度
			ArrayList<DiceSet> nowQueue = new ArrayList<>(3200000), nextQueue = new ArrayList<>(3200000);
			nowQueue.add(new DiceSet(0b111111111, 0b01001_01000_00111_00110_00101_00100_00011_00010_00001_00000L)); // 初項M_0を求める
			for (int[] dice : S) {
				for (DiceSet multiSet : nowQueue) multiSet.next(dice, checkUnique, nextQueue); // M_iからM_{i+1}を求める
				ArrayList<DiceSet> swap = nowQueue;
				nowQueue = nextQueue;
				nextQueue = swap;
				nextQueue.clear();
			}
			
			int ans = 0;
			for (DiceSet multiSet : nowQueue) ans = (int)((ans + multiSet.multichoose(factorial)) % 998_244_353);
			System.out.println(ans);
		}
	}
	
	/**
	 * ダイスから作られる整数の多重集合を管理するクラスです。
	 * @param multiSet 多重集合を、仕切りの考え方で見なした時のbit列
	 * @param partition multiSetで立っているbitの位置、5bitごとに管理
	 */
	static record DiceSet(int multiSet, long partition) {
		int getPartition(int index) { // multiSetでindex番目に立っているbitの位置を求める
			return (int)(partition >> 5 * index & 0b11111);
		}
		long multichoose(long[] factorial) { // この多重集合を並べてできる組合せ
			long ans = factorial[getPartition(9) - 9];
			for (int i = 0;i < 9;++ i) ans /= factorial[getPartition(i + 1) - getPartition(i) - 1];
			return ans;
		}
		void next(int[] dice, BitSet checkUnique, ArrayList<DiceSet> nextQueue) { // diceを追加したときにできる新たな多重集合のうち、新しく発見したものをnextQueueに入れる
			for (int result : dice) {
				int mask = (1 << getPartition(result)) - 1;
				int nextSet = (multiSet & ~mask) << 1 | (multiSet & mask);
				if (checkUnique.get(nextSet)) continue;
				checkUnique.set(nextSet);
				long nextPartition = partition + (0b00001_00001_00001_00001_00001_00001_00001_00001_00001_00001L & ~((0x20L << result * 5) - 1));
				nextQueue.add(new DiceSet(nextSet, nextPartition));
			}
		}
	}
}
0