結果

問題 No.2230 Good Omen of White Lotus
ユーザー 👑 AngrySadEightAngrySadEight
提出日時 2022-12-08 20:50:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,777 bytes
コンパイル時間 1,064 ms
コンパイル使用メモリ 84,568 KB
最終ジャッジ日時 2025-02-09 06:29:57
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21 MLE * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;

ll my_pow(ll x, ll n, ll mod){
    ll ret;
    if (n == 0){
        ret = 1;
    }
    else if (n % 2 == 1){
        ret = (x * my_pow((x * x) % mod, n / 2, mod)) % mod;
    }
    else{
        ret = my_pow((x * x) % mod, n / 2, mod);
    }
    return ret;
}

ll inv(ll x, ll mod){
    return my_pow(x, mod - 2, mod);
}

ll mod2 = 998244353;

int main(){
    ll H,W,N,P;
    cin >> H >> W >> N >> P;
    vector<ll> x(N);
    vector<ll> y(N);
    for (ll i = 0; i < N; i++){
        cin >> x[i] >> y[i];
        x[i]--;
        y[i]--;
    }
    vector<vector<bool>> has(H, vector<bool>(W, false));
    for (ll i = 0; i < N; i++){
        has[x[i]][y[i]] = true;
    }
    ll INF = 1000000000;
    vector<vector<ll>> dp(H, vector<ll>(W, -INF));
    dp[0][0] = 0;
    for (ll i = 0; i < H; i++){
        for (ll j = 0; j < W; j++){
            if (has[i][j]){
                if (i > 0){
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1);
                }
                if (j > 0){
                    dp[i][j] = max(dp[i][j], dp[i][j - 1] + 1);
                }
            }
            else{
                if (i > 0){
                    dp[i][j] = max(dp[i][j], dp[i - 1][j]);
                }
                if (j > 0){
                    dp[i][j] = max(dp[i][j], dp[i][j - 1]);
                }
            }
        }
    }
    ll max_omen = dp[H - 1][W - 1];
    //cout << max_omen << endl;
    
    ll ans = (my_pow(P, H + W - 3, mod2));
    ans = (ans - ((my_pow(P - 1, H + W - 3 - max_omen, mod2)) * (my_pow(P - 2, max_omen, mod2))) % mod2 + mod2) % mod2;
    ans = (ans * inv((my_pow(P, H + W - 3, mod2)), mod2)) % mod2;
    cout << ans << endl;
}
0