QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#122646 | #5530. No Zero-Sum Subsegment | lost_heart_hurts | WA | 47ms | 36116kb | C++14 | 2.3kb | 2023-07-10 21:09:57 | 2023-07-10 21:10:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 4e6 + 5;
char buf[1 << 23], *p1 = buf, *p2 = buf;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1 ++)
int read() {
int s = 0, w = 1; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') w = -1; ch = getchar(); }
while(isdigit(ch)) s = s * 10 + (ch ^ 48), ch = getchar();
return s * w;
}
template <typename A>
int mul(A x) { return x; }
template <typename A, typename ... B>
int mul(A x, B ... args) { return 1ll * x * mul(args ...) % mod; }
void inc(int &a, int b) { a = a >= mod - b ? a - mod + b : a + b; }
int add(int a, int b) { return a >= mod - b ? a - mod + b : a + b; }
int ksm(int a, int b) {
int res = 1;
while(b > 0) {
if(b & 1) res = mul(res, a);
a = mul(a, a), b >>= 1;
}
return res;
}
int fac[N], ifac[N];
int C(int b, int c, int d) {
if(b < 0 || c < 0 || d < 0) return 0;
return mul(fac[b + c + d], ifac[b], ifac[c], ifac[d]);
}
int h(int b, int c, int d) {
d -= 2 * b;
if(b < 0 || c < 0 || d < 0) return 0;
return mul(fac[b + c + d], ifac[b], ifac[c], ifac[d]);
}
int g(int a, int b, int c, int d) {
if(a < 0 || b < 0 || c < 0 || d < 0) return 0;
if(a) return add(h(b - 1, c, d - a - 1), h(b, c - 1, d - a));
else return add(h(b, c, d), h(b - 1, c, d - 1));
}
void solve() {
int a = read(), b = read(), c = read(), d = read();
int s = -2 * a - b + c + 2 * d;
if(!s) return printf("0\n"), void();
if(s < 0) swap(a, d), swap(b, c);
auto f = [&](int i) {
if(i == 1) {
int x = C(b, c - 2, d - a - 2 * b);
int y = C(b - 2, c, d - a - 2 * b + 2);
int z = C(b - 1, c - 1, d - a - 2 * b + 1);
return add(add(x, y), add(z, z));
}
if(!i) return add(g(a, b - 1, c, d - 1), g(a, b, c, d));
else return add(g(a - i, b, c - 1, d - i), g(a - i, b - 1, c, d - i - 1));
};
int lim = min(a, d), ans = f(0);
if(lim) inc(ans, mul(f(1), lim - 1)), inc(ans, f(lim));
printf("%d\n", ans);
}
signed main() {
for(int i = fac[0] = 1; i < N; ++ i) fac[i] = mul(fac[i - 1], i);
ifac[N - 1] = ksm(fac[N - 1], mod - 2);
for(int i = N - 2; i >= 0; -- i) ifac[i] = mul(ifac[i + 1], i + 1);
int T = read();
while(T --) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 36ms
memory: 36116kb
input:
5 69 0 0 0 1 1 1 1 0 0 3 3 6 1 0 6 10000 10000 1000000 1000000
output:
1 0 20 2 480402900
result:
ok 5 number(s): "1 0 20 2 480402900"
Test #2:
score: -100
Wrong Answer
time: 47ms
memory: 35856kb
input:
83520 13 1 6 8 13 9 3 14 1 16 13 0 8 12 7 16 10 5 9 15 16 16 15 1 5 11 0 4 12 4 11 15 16 7 1 10 2 6 3 3 15 14 12 0 0 8 12 12 3 0 6 7 16 2 16 15 16 16 16 13 8 13 2 14 7 9 7 0 9 8 4 6 13 16 4 11 11 1 12 4 7 9 4 16 12 13 0 4 5 0 9 1 11 11 0 3 1 11 14 3 3 3 7 3 8 5 4 4 6 7 12 14 2 11 5 6 15 4 9 14 15 11...
output:
0 0 0 0 0 0 52 0 26784 0 0 0 392 0 0 0 0 0 0 0 0 478686 0 136136 0 0 0 0 0 0 0 1680 0 821 0 24024 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8994258 0 0 0 0 0 293930 0 0 156637 166 0 0 0 0 0 0 0 0 0 46 0 0 1248 126 0 0 0 344 0 9867 10880 544 0 0 0 0 0 216580 0 0 3948 0 0 0 0 0 57915 0 0 0 6 0 66690456 0 10854738...
result:
wrong answer 72nd numbers differ - expected: '54', found: '46'