結果
問題 | No.2136 Dice Calendar? |
ユーザー |
![]() |
提出日時 | 2022-10-14 13:02:42 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 2,069 ms / 5,000 ms |
コード長 | 2,523 bytes |
コンパイル時間 | 2,676 ms |
コンパイル使用メモリ | 81,064 KB |
実行使用メモリ | 397,192 KB |
最終ジャッジ日時 | 2024-06-26 12:35:44 |
合計ジャッジ時間 | 17,831 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
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)); } } } }