1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5,M = 2.5e3 + 5,M2 = 3e2 + 5,P = 998244353; inline void Plus(int &x,const int &y) { x += y;if(x >= P) x -= P;} int n,m,k,lim,Sum; int C0,C1,D0,D1; int bel[N],s[N],siz[N]; bool spc[N],spcity[N]; int dislike[N]; int f[M],g[M],sf[M],sg[M]; vector<int> sch[N]; inline void Prework() { cin >> n >> m; cin >> C0 >> C1 >> D0 >> D1; lim = max({C0,C1,D0,D1}); for(int i = 1;i <= n;i++) siz[i] = spc[i] = spcity[i] = dislike[i] = 0,sch[i].clear(); for(int i = 0;i <= lim;i++) f[i] = g[i] = 0; Sum = 0; for(int i = 1;i <= n;i++) cin >> bel[i] >> s[i],siz[bel[i]] += s[i],Sum += s[i]; cin >> k; for(int i = 1,x,y;i <= k;i++) cin >> x >> y,spc[x] = spcity[bel[x]] = true,dislike[x] = y; for(int i = 1;i <= n;i++) if(spc[i]) sch[bel[i]].push_back(i); f[0] = 1; for(int i = 1;i <= n;i++) if(!spc[i]) for(int j = lim;j >= s[i];j--) Plus(f[j],f[j - s[i]]); g[0] = 1; for(int i = 1;i <= m;i++) if(siz[i] && !spcity[i]) for(int j = lim;j >= siz[i];j--) Plus(g[j],g[j - siz[i]]); sf[0] = f[0];sg[0] = g[0]; for(int i = 1;i <= lim;i++) sf[i] = (sf[i - 1] + f[i]) % P,sg[i] = (sg[i - 1] + g[i]) % P; } inline void Solve0() { int l = max(0,Sum - D1),r = D0; int ans = 1; if(l > r) ans = 0; else ans = 1ll * ans * (sf[r] - (l > 0 ? sf[l - 1] : 0) + P) % P; l = max(0,Sum - C1),r = C0; if(l > r) ans = 0; else ans = 1ll * ans * (sg[r] - (l > 0 ? sg[l - 1] : 0) + P) % P; cout << ans << endl; } int dp[2][M][M2][2]; inline void Solve1() { int lim2 = 0; for(int i = 1;i <= n;i++) if(spc[i]) lim2 += s[i]; for(int i = 0;i <= lim;i++) for(int j = 0;j < M2;j++) dp[0][i][j][0] = dp[0][i][j][1] = dp[1][i][j][0] = dp[1][i][j][1] = 0; dp[0][0][0][0] = 1; int op = 0; auto DpNext = [&]() { for(int j = 0;j <= lim;j++) for(int k = 0;k < M2;k++) dp[op][j][k][0] = dp[op][j][k][1] = 0; op ^= 1; }; for(int i = 1;i <= m;i++) { if(!sch[i].size()) continue; for(int j = 0;j <= lim;j++) for(int k = 0;k <= lim2;k++) { int now = sch[i][0]; int id = dislike[sch[i][0]],val = (dp[op][j][k][0] + dp[op][j][k][1]) % P; if(!val) continue; if(id != 0 && j + siz[i] <= lim && k + s[now] <= lim2) Plus(dp[op ^ 1][j + siz[i]][k + s[now]][0],val); if(id != 1 && j + siz[i] <= lim) Plus(dp[op ^ 1][j + siz[i]][k][0],val); if(id != 2 && k + s[now] <= lim2) Plus(dp[op ^ 1][j][k + s[now]][1],val); if(id != 3) Plus(dp[op ^ 1][j][k][1],val); } DpNext(); for(int t = 1;t < sch[i].size();t++) { int now = sch[i][t]; for(int j = 0;j <= lim;j++) for(int k = 0;k <= lim2;k++) { int id = dislike[now]; if(id != 0 && k + s[now] <= lim2) Plus(dp[op ^ 1][j][k + s[now]][0],dp[op][j][k][0]); if(id != 1) Plus(dp[op ^ 1][j][k][0],dp[op][j][k][0]); if(id != 2 && k + s[now] <= lim2) Plus(dp[op ^ 1][j][k + s[now]][1],dp[op][j][k][1]); if(id != 3) Plus(dp[op ^ 1][j][k][1],dp[op][j][k][1]); } DpNext(); } } int ans = 0; for(int j = 0;j <= lim;j++) for(int k = 0;k <= lim2;k++) { int coef = (dp[op][j][k][0] + dp[op][j][k][1]) % P; int l = max(0,Sum - D1 - k),r = D0 - k; if(l > r) coef = 0; else coef = 1ll * coef * (sf[r] - (l > 0 ? sf[l - 1] : 0) + P) % P; l = max(0,Sum - C1 - j);r = C0 - j; if(l > r) coef = 0; else coef = 1ll * coef * (sg[r] - (l > 0 ? sg[l - 1] : 0) + P) % P; Plus(ans,coef); } cout << ans << endl; } int main() { int T; cin >> T; while(T--) { Prework(); Solve1(); } return 0; }
|