QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#164477#5474. Incredibly Cute Penguin ChicksckisekiTL 1ms3676kbC++205.0kb2023-09-05 00:19:042023-09-05 00:19:05

Judging History

你现在查看的是最新测评结果

  • [2023-09-05 00:19:05]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3676kb
  • [2023-09-05 00:19:04]
  • 提交

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);
    
    [&] {
        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);

        array<function<int(int)>, 3> get;
        array<function<bool(int, int)>, 3> cmp;
        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] = [&get, i](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';
    }();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3580kb

input:

ICPC

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3576kb

input:

CCCIIIPPP

output:

69

result:

ok single line: '69'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3676kb

input:

PICCICCIPPI

output:

24

result:

ok single line: '24'

Test #4:

score: -100
Time Limit Exceeded

input:

PCPPCCICPICPCPCPCCPPIPPPIICCCICPICCPPPIPCCCCICCIICPCCICCCPCICIPPIPIPIPPCCPCIPPCCPPCCIPPPIPPCIICIIIPPCIPPICPPICCPIICCCCCCCICPCCPPIPIPICPCIICPCCIPICCCPPCPIIICPCPPPIIIIICCCPCICCPPCPPCICIIIPIPICPCPPPPICPCPIPIIICCICPCIPPICCICPIIPPCCCIICPCPCCICCPICPPPPPPICCCIPCCICICIPICIIICCICPCIPIICPIPIIICCPCCIIPCCCCPIPI...

output:


result: