QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#81899 | #2834. Nonsense | AHSFNU_team_0# | WA | 2ms | 5612kb | C++14 | 3.2kb | 2023-02-26 17:31:11 | 2023-02-26 17:31:14 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std; const int maxn = 5e3 + 5, maxm = 2e5 + 5, mod = 998244353; typedef long long ll;
inline int qpow(int x, int k) { int res = 1; while (k) { if (k & 1) res = (ll)res * x % mod; x = (ll)x * x % mod, k >>= 1; } return res; }
int n, A, B, q, a[maxm], b[maxm], ma, mb;
int C[maxn << 1], F1[maxn << 1], F2[maxn << 1], C_[maxn << 1];
// inline int Calc(int a, int b, int i)
// {
// return (ll)F2[i] * qpow(F2[a], )
// }
int f[maxn][maxn];
int main()
{
for (int i = F2[0] = 1; i < maxn << 1; i++) F2[i] = (ll)F2[i - 1] * i % mod;
while (~scanf("%d%d%d%d", &n, &A, &B, &q))
{
ma = mb = 0;
for (int i = 1; i <= q; i++) scanf("%d%d", a + i, b + i), ma = max(ma, a[i]), mb = max(mb, b[i]);
for (int i = C[0] = 1; i <= ma + mb; i++) C[i] = (ll)C[i - 1] * (n - i + 1) % mod * qpow(i, mod - 2) % mod;
for (int i = C_[0] = 1; i <= ma + mb; i++) C_[i] = (ll)C_[i - 1] * (n - i + 2) % mod * qpow(i, mod - 2) % mod;
// for (int i = 0; i <= ma + mb; i++) cerr << C[i] << ' '; cerr << endl;
if (A == 0 && B != 0)
{
F1[0] = n;
for (int i = 1; i <= ma + mb; i++) F1[i] = (ll)F1[i - 1] * (n - i) % mod;
for (int i = 1; i <= q; i++)
{
int Ans = (ll)F1[a[i] + b[i] - 1] * qpow(F1[a[i] - 1], mod - 2) % mod * qpow(F2[b[i]], mod - 2) % mod * qpow(B, n - a[i] - a[i]) % mod;
printf("%d\n", (Ans + mod) % mod);
}
continue;
}
if (A != 0 && B == 0)
{
F1[0] = n;
for (int i = 1; i <= ma + mb; i++) F1[i] = (ll)F1[i - 1] * (n - i) % mod;
for (int i = 1; i <= q; i++)
{
int Ans = (ll)F1[a[i] + b[i] - 1] * qpow(F1[b[i] - 1], mod - 2) % mod * qpow(F2[a[i]], mod - 2) % mod * qpow(A, n - a[i] - b[i]) % mod;
printf("%d\n", (Ans + mod) % mod);
}
continue;
}
if (A == 0 && B == 0)
{
for (int i = 1; i <= q; i++) printf("%d\n", int(a[i] + b[i] == n));
continue;
}
if (A == B)
{
for (int i = 1; i <= q; i++)
{
int Ans = (ll)qpow(A, n - a[i] - b[i]) * C_[a[i] + b[i] + 1] % mod;
printf("%d\n", (Ans + mod) % mod);
}
continue;
}
for (int i = 0; i <= ma; i++) for (int j = 0; j <= mb; j++) f[i][j] = 0;
for (int j = 0, K = qpow(B, n + 1); j <= mb; j++) f[0][j] = (ll)C_[j] * K % mod;
for (int i = 0, K = qpow(A, n + 1); i <= ma; i++) f[i][0] = (f[i][0] - (ll)C_[i] * K) % mod;
int K = qpow(B - A, mod - 2);
for (int i = 0; i <= ma; i++) for (int j = 0; j <= mb; j++)
{
f[i][j] = (f[i][j] - (ll)B * (j ? f[i][j - 1] : 0) + (ll)A * (i ? f[i - 1][j] : 0)) % mod * K % mod;
}
for (int i = 1; i <= q; i++)
{
// int Ans = f[a[i]][b[i]];
int Ans = (ll)f[a[i]][b[i]] * qpow((ll)qpow(A, a[i]) * qpow(B, b[i]) % mod, mod - 2) % mod;
printf("%d\n", (Ans + mod) % mod);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5612kb
input:
3 1 2 2 1 1 1 2 100 2 3 1 1 1
output:
6 1 866021789
result:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 5544kb
input:
1000000000 0 1 1 1000 1000 2 0 0 1 1 1 2 998244352 998244352 1 1 1
output:
381781645 1 956089115
result:
wrong answer 3rd lines differ - expected: '1', found: '956089115'