QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#164329 | #5474. Incredibly Cute Penguin Chicks | rgnerdplayer | TL | 1ms | 3444kb | C++20 | 5.0kb | 2023-09-04 21:41:17 | 2023-09-04 21:41:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
template <int P>
struct Modint {
private:
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : m - 1LL * minv(m, a) * m / a;
}
public:
constexpr Modint() : v(0) {}
constexpr Modint(i64 v_) : v(v_ % P) {
if (v < 0) {
v += P;
}
}
constexpr explicit operator int() const { return v; }
constexpr friend ostream& operator<<(ostream &out, const Modint &n) {
return out << int(n);
}
constexpr friend istream& operator>>(istream &in, Modint &n) {
i64 v;
in >> v;
n = Modint(v);
return in;
}
constexpr friend bool operator==(const Modint &a, const Modint &b) {
return a.v == b.v;
}
constexpr friend bool operator!=(const Modint &a, const Modint &b) {
return a.v != b.v;
}
constexpr Modint inv() const {
Modint res;
res.v = minv(v, P);
return res;
}
constexpr Modint operator-() const {
Modint res;
res.v = v ? P - v : 0;
return res;
}
constexpr Modint& operator++() {
v++;
if (v == P) v = 0;
return *this;
}
constexpr Modint& operator--() {
if (v == 0) v = P;
v--;
return *this;
}
constexpr Modint& operator+=(const Modint &o) {
v -= P - o.v;
v = (v < 0) ? v + P : v;
return *this;
}
constexpr Modint& operator-=(const Modint &o) {
v -= o.v;
v = (v < 0) ? v + P : v;
return *this;
}
constexpr Modint& operator*=(const Modint &o) {
v = 1LL * v * o.v % P;
return *this;
}
constexpr Modint& operator/=(const Modint &o) { return *this *= o.inv(); }
constexpr friend Modint operator++(Modint &a, int) {
Modint r = a;
++a;
return r;
}
constexpr friend Modint operator--(Modint &a, int) {
Modint r = a;
--a;
return r;
}
constexpr friend Modint operator+(const Modint &a, const Modint &b) {
return Modint(a) += b;
}
constexpr friend Modint operator-(const Modint &a, const Modint &b) {
return Modint(a) -= b;
}
constexpr friend Modint operator*(const Modint &a, const Modint &b) {
return Modint(a) *= b;
}
constexpr friend Modint operator/(const Modint &a, const Modint &b) {
return Modint(a) /= b;
}
constexpr Modint qpow(i64 p) {
Modint res = 1, x = v;
while (p > 0) {
if (p & 1) { res *= x; }
x *= x;
p >>= 1;
}
return res;
}
};
constexpr int P = 998244353;
using Mint = Modint<P>;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
auto solve = [&]() {
string s;
cin >> s;
int n = s.size();
vector<int> a(n);
for (int i = 0; i < n; i++) {
a[i] = string("ICP").find(s[i]);
}
vector pref(n + 1, vector<int>(3));
for (int i = 0; i < n; i++) {
pref[i + 1] = pref[i];
pref[i + 1][a[i]]++;
}
vector<int> o(n + 1);
vector<function<int(int)>> get(3);
vector<function<bool(int, int)>> cmp(3);
for (int i = 0; i < 3; i++) {
get[i] = [&pref, i](int x) { return pref[x][i] - pref[x][(i + 1) % 3]; };
}
for (int i = 0; i < 3; i++) {
cmp[i] = [i, &get](int x, int y) {
return get[i](x) < get[i](y);
};
}
vector<Mint> cnt(2 * n + 1), dp(n + 1);
dp[0] = 1;
auto dc = [&](auto dc, int l, int r) -> void {
if (r - l == 1) {
return;
}
int m = l + r >> 1;
dc(dc, l, m);
for (int c = 0; c < 3; c++) {
iota(o.begin() + l, o.begin() + r, l);
sort(o.begin() + l, o.begin() + m, cmp[c]);
sort(o.begin() + m, o.begin() + r, cmp[c]);
int i = l;
int c1 = (c + 1) % 3;
for (int j = m; j < r; j++) {
while (i < m && cmp[c](o[i], o[j])) {
int x = get[c1](o[i]) + n;
assert(x >= 0 && x <= 2 * n);
cnt[x] += dp[o[i]];
i++;
}
int x = get[c1](o[j]) + n;
assert(x >= 0 && x <= 2 * n);
dp[o[j]] += cnt[x];
}
while (i > l) {
i--;
int x = get[c1](o[i]) + n;
assert(x >= 0 && x <= 2 * n);
cnt[x] -= dp[o[i]];
}
}
dc(dc, m, r);
};
dc(dc, 0, n + 1);
cout << dp[n] << '\n';
};
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3444kb
input:
ICPC
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3412kb
input:
CCCIIIPPP
output:
69
result:
ok single line: '69'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3412kb
input:
PICCICCIPPI
output:
24
result:
ok single line: '24'
Test #4:
score: -100
Time Limit Exceeded
input:
PCPPCCICPICPCPCPCCPPIPPPIICCCICPICCPPPIPCCCCICCIICPCCICCCPCICIPPIPIPIPPCCPCIPPCCPPCCIPPPIPPCIICIIIPPCIPPICPPICCPIICCCCCCCICPCCPPIPIPICPCIICPCCIPICCCPPCPIIICPCPPPIIIIICCCPCICCPPCPPCICIIIPIPICPCPPPPICPCPIPIIICCICPCIPPICCICPIIPPCCCIICPCPCCICCPICPPPPPPICCCIPCCICICIPICIIICCICPCIPIICPIPIIICCPCCIIPCCCCPIPI...