結果

問題 No.2136 Dice Calendar?
ユーザー CuriousFairy315
提出日時 2022-10-21 15:37:37
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 972 ms / 5,000 ms
コード長 2,743 bytes
コンパイル時間 3,541 ms
コンパイル使用メモリ 109,592 KB
最終ジャッジ日時 2025-02-08 09:44:06
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<vector>
#include<bitset>
using namespace std;
using ll = long long int;
typedef bitset<1 << 29> bs; // 64MB程度

int deBrujin32[32] = { 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 };

int lsb(unsigned int i) { // 丁度1bit立っている値に対してその立っている位置を返す
  return deBrujin32[i * 0x077CB531 >> 27];
}

ll calc_partition(int multiset) { // 与えられた多重集合に対して、立っているbitの位置を保持する数列Pを返す
  ll partition = 0;
  for (int i = 5; i <= 45; i += 5) {
    int lob = multiset & -multiset;
    partition += (1LL + lsb(lob)) << i;
    multiset -= lob;
  }
  return partition;
}

int get_partition(ll partition, int index) { // multiSetでindex番目に立っているbitの位置を求める
  return (int) (partition >> 5 * index & 0b11111);
}

ll multichoose(ll *factorial, int multiset) { // multiSetで与えられた多重集合を並べてできる組合せ
  ll partition = calc_partition(multiset);
  ll multichoose = factorial[get_partition(partition, 9) - 9];
  for (int i = 0; i < 9; ++i)
    multichoose /= factorial[get_partition(partition, i + 1) - get_partition(partition, i) - 1];
  return multichoose;
}

int next(int multiset, vector<int> &dice, bs &check_unique, int *stack, int length) {// diceを追加したときにできる新たな多重集合のうち、新しく発見したものをstackに入れる
  ll partition = calc_partition(multiset);
  for (int result : dice) {
    int mask = (1 << get_partition(partition, result)) - 1;
    int next = (multiset & ~mask) << 1 | (multiset & mask);
    if (check_unique[next]) continue;
    check_unique.set(next);
    stack[length++] = next;
  }
  return length;
}

int main() {
  int N;
  cin >> N;
  vector<vector<int>> S(N, vector<int>(6));
  for (int i = 0;i < N;++ i) for (int j = 0;j < 6;++ j) {
    cin >> S[i][j];
    -- S[i][j];
  }
  
  ll factorial[21] = {1};
  for (int i = 1;i <= 20;++ i) factorial[i] = factorial[i - 1] * i;
  
  int now_stack[3200000] = {0b111111111}, next_stack[3200000]; // 初項M_0を求める
  int now_index = 1, next_index = 0;
  bs unique_check(0);
  for (vector<int> dice : S) {
    for (int i = 0;i < now_index;++ i) next_index = next(now_stack[i], dice, unique_check, next_stack, next_index); // M_iからM_{i+1}を求める
    swap(now_stack, next_stack);
    now_index = next_index;
    next_index = 0;
  }
  int ans = 0;
  for (int i = 0;i < now_index;++ i) ans = (int)((ans + multichoose(factorial, now_stack[i])) % 998'244'353);
  cout << ans << endl;
}
0