結果

問題 No.2159 Filling 4x4 array
ユーザー 👑 NachiaNachia
提出日時 2022-12-07 08:54:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 664 ms / 5,000 ms
コード長 4,389 bytes
コンパイル時間 1,341 ms
コンパイル使用メモリ 102,124 KB
実行使用メモリ 35,172 KB
最終ジャッジ日時 2024-10-14 23:02:51
合計ジャッジ時間 31,826 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 650 ms
35,044 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 628 ms
34,916 KB
testcase_03 AC 632 ms
34,916 KB
testcase_04 AC 626 ms
34,916 KB
testcase_05 AC 627 ms
35,044 KB
testcase_06 AC 629 ms
35,004 KB
testcase_07 AC 638 ms
35,048 KB
testcase_08 AC 645 ms
34,968 KB
testcase_09 AC 640 ms
35,172 KB
testcase_10 AC 643 ms
35,076 KB
testcase_11 AC 642 ms
35,044 KB
testcase_12 AC 658 ms
34,868 KB
testcase_13 AC 644 ms
34,916 KB
testcase_14 AC 642 ms
35,104 KB
testcase_15 AC 650 ms
34,988 KB
testcase_16 AC 639 ms
35,044 KB
testcase_17 AC 651 ms
35,044 KB
testcase_18 AC 644 ms
35,040 KB
testcase_19 AC 650 ms
35,072 KB
testcase_20 AC 640 ms
34,916 KB
testcase_21 AC 630 ms
35,032 KB
testcase_22 AC 640 ms
35,040 KB
testcase_23 AC 638 ms
35,044 KB
testcase_24 AC 633 ms
35,044 KB
testcase_25 AC 639 ms
35,044 KB
testcase_26 AC 635 ms
35,048 KB
testcase_27 AC 653 ms
35,108 KB
testcase_28 AC 661 ms
34,988 KB
testcase_29 AC 637 ms
34,916 KB
testcase_30 AC 642 ms
34,920 KB
testcase_31 AC 647 ms
35,024 KB
testcase_32 AC 648 ms
35,004 KB
testcase_33 AC 647 ms
35,060 KB
testcase_34 AC 636 ms
35,048 KB
testcase_35 AC 646 ms
34,916 KB
testcase_36 AC 647 ms
35,044 KB
testcase_37 AC 655 ms
35,040 KB
testcase_38 AC 663 ms
35,044 KB
testcase_39 AC 651 ms
35,040 KB
testcase_40 AC 647 ms
35,044 KB
testcase_41 AC 637 ms
35,040 KB
testcase_42 AC 661 ms
34,956 KB
testcase_43 AC 664 ms
35,044 KB
testcase_44 AC 648 ms
35,036 KB
testcase_45 AC 2 ms
6,816 KB
testcase_46 AC 2 ms
6,816 KB
testcase_47 AC 1 ms
6,820 KB
testcase_48 AC 2 ms
6,816 KB
testcase_49 AC 2 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <atcoder/modint>
using namespace std;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
using Modint = atcoder::static_modint<998244353>;

constexpr const int STATE_NUM = 7*7*7*7*7*7*7*7;

vector<vector<int>> OPERATORS = {
    { 0, 0, 4 },
    { 0, 1, 4 },
    { 0, 2, 4 },
    { 0, 3, 4 },
    { 1, 4 },
    { 0, 0, 5 },
    { 0, 1, 5 },
    { 0, 2, 5 },
    { 0, 3, 5 },
    { 1, 5 },
    { 0, 0, 6 },
    { 0, 0, 7 },
    { 1, 0 },
    { 0, 1, 6 },
    { 0, 1, 7 },
    { 1, 1 },
    { 0, 2, 6 },
    { 0, 2, 7 },
    { 1, 2 },
    { 0, 3, 6 },
    { 1, 6 },
    { 0, 3, 7 },
    { 1, 3 },
    { 1, 7 }
};

vector<unsigned int> TABLE_SIZE;
vector<unsigned int> TABLE_DELTA;
vector<vector<unsigned int>> TABLE_TO;

unsigned int TABLE_IDX(const vector<unsigned int>& z, const vector<unsigned int>& a){
    unsigned int i = 0;
    for(int j=(int)z.size()-1; j>=0; j--) i = i * z[j] + a[j];
    return i;
}

unsigned int CALC_TABLE_SIZE(const vector<unsigned int>& z){
    unsigned int i = 1;
    for(int j=(int)z.size()-1; j>=0; j--) i = i * z[j];
    return i;
}

void PRECALC(){
    vector<unsigned int> WIDTH = { 4,4,4,4, 4,4,4,4 };
    TABLE_SIZE.push_back(CALC_TABLE_SIZE(WIDTH));
    for(auto op : OPERATORS){
        vector<unsigned int> WIDTH_NEW = WIDTH;
        vector<unsigned int> to;
        unsigned int tableSize = 0;
        unsigned int tableDelta = 0;
        if(op[0] == 0){
            to.resize(TABLE_SIZE.back());
            for(size_t i=1; i<op.size(); i++) WIDTH_NEW[op[i]]++;
            vector<unsigned int> ptmp(8, 0);
            for(size_t i=1; i<op.size(); i++) ptmp[op[i]]++;
            tableDelta = TABLE_IDX(WIDTH_NEW, ptmp);
            tableSize = CALC_TABLE_SIZE(WIDTH_NEW);
            auto dfs = [WIDTH, WIDTH_NEW, &ptmp, &to](auto& dfs, int d) -> void {
                if(d == 8){
                    to[TABLE_IDX(WIDTH, ptmp)] = TABLE_IDX(WIDTH_NEW, ptmp);
                }
                else{
                    for(ptmp[d] = 0; ptmp[d] < WIDTH[d]; ptmp[d]++) dfs(dfs, d+1);
                }
            };
            dfs(dfs, 0);
        }
        else{
            for(size_t i=1; i<op.size(); i++) WIDTH_NEW[op[i]] /= 2;
            vector<unsigned int> ptmp(8, 0);
            for(size_t i=1; i<op.size(); i++) ptmp[op[i]]++;
            tableDelta = TABLE_IDX(WIDTH_NEW, ptmp);
            tableSize = CALC_TABLE_SIZE(WIDTH_NEW);
            to.resize(tableSize);
            auto dfs = [WIDTH, WIDTH_NEW, op, &ptmp, &to](auto& dfs, int d) -> void {
                if(d == 8){
                    vector<unsigned int> tmp2 = ptmp;
                    for(size_t i=1; i<op.size(); i++) tmp2[op[i]] *= 2;
                    to[TABLE_IDX(WIDTH_NEW, ptmp)] = TABLE_IDX(WIDTH, tmp2);
                }
                else{
                    for(ptmp[d] = 0; ptmp[d] < WIDTH_NEW[d]; ptmp[d]++) dfs(dfs, d+1);
                }
            };
            dfs(dfs, 0);
        }
        TABLE_SIZE.push_back(tableSize);
        TABLE_DELTA.push_back(tableDelta);
        TABLE_TO.push_back(move(to));
        WIDTH = WIDTH_NEW;
    }
}

int main(){
    vector<unsigned long long> A(8);
    rep(i,8) cin >> A[i];
    if(A[0] + A[1] + A[2] + A[3] != A[4] + A[5] + A[6] + A[7]){ cout << "0\n"; return 0; }
    rep(i,8) if(A[i] < 4){ cout << "0\n"; return 0; }
    rep(i,8) A[i] -= 4;

    PRECALC();

    vector<Modint> dp(TABLE_SIZE[0]);
    dp[0] = 1;

    for(int d=0; d<30; d++){
        for(size_t opuct=0; opuct<OPERATORS.size(); opuct++){
            auto op = OPERATORS[opuct];
            unsigned int tableSize = TABLE_SIZE[opuct+1];
            unsigned int tableDelta = TABLE_DELTA[opuct];
            unsigned int* tableTo = TABLE_TO[opuct].data();
            vector<Modint> tmp(tableSize);
            if(op[0] == 0){
                for(unsigned int i=0; i<dp.size(); i++) tmp[tableTo[i]] = dp[i];
                for(unsigned int i=0; i<dp.size(); i++) tmp[tableTo[i] + tableDelta] += dp[i];
            }
            if(op[0] == 1){
                if((A[op[1]]>>d)&1) for(unsigned int i=0; i<tmp.size(); i++) tmp[i] = dp[tableTo[i] + tableDelta];
                else                for(unsigned int i=0; i<tmp.size(); i++) tmp[i] = dp[tableTo[i]];
            }
            swap(dp, tmp);
        }
    }

    cout << dp[0].val() << '\n';
    return 0;
}
0