結果

問題 No.2689 Populous
ユーザー 👑 tute7627tute7627
提出日時 2024-03-10 16:06:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,655 bytes
コンパイル時間 2,198 ms
コンパイル使用メモリ 211,204 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-10 16:06:58
合計ジャッジ時間 3,457 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 1 ms
6,548 KB
testcase_02 AC 2 ms
6,548 KB
testcase_03 AC 1 ms
6,548 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,548 KB
testcase_06 WA -
testcase_07 AC 2 ms
6,548 KB
testcase_08 WA -
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 1 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 2 ms
6,548 KB
testcase_14 WA -
testcase_15 AC 2 ms
6,548 KB
testcase_16 WA -
testcase_17 AC 2 ms
6,548 KB
testcase_18 AC 2 ms
6,548 KB
testcase_19 AC 2 ms
6,548 KB
testcase_20 AC 2 ms
6,548 KB
testcase_21 AC 3 ms
6,548 KB
testcase_22 AC 3 ms
6,548 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 10 ms
6,548 KB
testcase_29 AC 10 ms
6,548 KB
testcase_30 AC 9 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/modint>
using namespace std;
#ifdef LOCAL
#include <debug_print.hpp>
#define OUT(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#define OUT(...) (static_cast<void>(0))
#endif
int main(){
    int h, w;
    cin >> h >> w;
    vector<int> a, b, c, d;
    for(int i = 0; i < w; i++){
        int v;
        cin >> v;
        a.push_back(v);
        if(i == 0)c.push_back(v);
        if(i == w - 1)d.push_back(v);
    }
    for(int i = 1; i < h - 1; i++){
        int vl, vr;
        cin >> vl >> vr;
        c.push_back(vl);
        d.push_back(vr);
    }
    for(int i = 0; i < w; i++){
        int v;
        cin >> v;
        b.push_back(v);
        if(i == 0)c.push_back(v);
        if(i == w - 1)d.push_back(v);
    }

    using modint = atcoder::modint998244353;
    modint ans = 1;
    for(int _ = 0; _ < 2; _++){
        swap(h, w);
        swap(a, c);
        swap(b, d);
        
        bool ok = true;
        for(int i = 0; i < w; i++){
            if(abs(a[i] - b[i]) >= h){
                ok = false;
            }
            if(i < w - 1){
                if(abs(a[i] - a[i + 1]) >= 2 || abs(b[i] - b[i + 1]) >= 2){
                    cout << -1 << endl;
                    return 0;
                }
            }
        }
        if(ok)continue;

        vector<modint>dp;
        for(int i = 0; i < w; i++){
            if(abs(a[i] - b[i]) < h){
                if(!dp.empty()){
                    ans *= accumulate(dp.begin(), dp.end(), modint(0));
                    dp.clear();
                }
            }
            else{
                if(dp.empty()){
                    dp.assign(h - 2, 1);
                }
                else{
                    vector<modint> ndp(h - 1, 0);
                    for(int j = 0; j < h - 2; j++){
                        int lj = max(0, j - 1);
                        int rj = min(h - 2, j + 2);
                        if(abs(b[i - 1] - a[i]) <= h){
                            ndp[0] += dp[j];
                            ndp[lj] -= dp[j];
                        }
                        ndp[lj] += dp[j];
                        ndp[rj] -= dp[j];
                        if(abs(a[i - 1] - b[i]) <= h - 1){
                            ndp[rj] += dp[j];
                            ndp[h - 2] -= dp[j];
                        }
                    }
                    for(int j = 0; j < h - 2; j++){
                        ndp[j + 1] += ndp[j];
                    }
                    ndp.pop_back();
                    dp.swap(ndp);
                }
            }
        }
    }

    cout << ans.val() << endl;
}
0