Submission #1364382

#TimeUsernameProblemLanguageResultExecution timeMemory
1364382saywooDubai Chewy Cookie (KAISTRUN26SPRING_C)C++20
100 / 100
15 ms552 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long int;
const ll mod = 998244353;

ll n, m, q;
ll p[2020], rp[2020];
bool x[2020], y[2020];
ll dp1[2020], dp2[2020], dp3[2020], ndp[2020];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n >> m >> q;

    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        rp[i] = mod + 1 - p[i];
        if (rp[i] >= mod) rp[i] -= mod;
    }
    for (int i = 0; i < m; i++) {
        ll a, b; cin >> a >> b;
        
        if (a == 1) x[b] = true;
        if (b == 1) x[a] = true;
        if (a == 2) y[b] = true;
        if (b == 2) y[a] = true;
    }

    x[1] = true;
    y[2] = true;

    dp1[0] = dp2[0] = dp3[0] = 1;
    ll c = 0;

    for (int i = 1; i <= n; i++) {
        if (x[i] && y[i]) {
            c++;
            for (int j = 0; j <= n; j++) {
                if (!dp1[j]) continue;
                ndp[j+1] = (ndp[j+1] + (dp1[j] * p[i]) % mod) % mod;
                ndp[j] = (ndp[j] + (dp1[j] * rp[i]) % mod) % mod;
            }
            for (int j = 0; j <= n; j++) {
                dp1[j] = ndp[j];
                ndp[j] = 0;
            }
        }
        else if (x[i]) {
            for (int j = 0; j <= n; j++) {
                if (!dp2[j]) continue;
                ndp[j+1] = (ndp[j+1] + (dp2[j] * p[i]) % mod) % mod;
                ndp[j] = (ndp[j] + (dp2[j] * rp[i]) % mod) % mod;
            }
            for (int j = 0; j <= n; j++) {
                dp2[j] = ndp[j];
                ndp[j] = 0;
            }
        }
        else if (y[i]) {
            for (int j = 0; j <= n; j++) {
                if (!dp3[j]) continue;
                ndp[j+1] = (ndp[j+1] + (dp3[j] * p[i]) % mod) % mod;
                ndp[j] = (ndp[j] + (dp3[j] * rp[i]) % mod) % mod;
            }
            for (int j = 0; j <= n; j++) {
                dp3[j] = ndp[j];
                ndp[j] = 0;
            }
        }
    }

    for (int i = 0; i < q; i++) {
        ll a, b; cin >> a >> b;

        ll mn = min(a, b);
        ll ans = 0;
        for (int j = 0; j <= min(mn, c); j++) {
            ll tmp = dp1[j];
            tmp = ((tmp * dp2[a-j]) % mod * dp3[b-j]) % mod;
            ans = (ans + tmp) % mod;
        }
        cout << ans << '\n';
    }

    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...