QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#622849#7876. Cyclic SubstringswpoemAC ✓132ms422760kbC++202.1kb2024-10-09 07:44:482024-10-09 07:44:49

Judging History

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

  • [2024-10-09 07:44:49]
  • 评测
  • 测评结果:AC
  • 用时:132ms
  • 内存:422760kb
  • [2024-10-09 07:44:48]
  • 提交

answer

#include<bits/stdc++.h>

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define debug(...) 42
#endif

using i64 = long long;

namespace ranges = std::ranges;

template <int Z, char Base>
struct Pam {
    std::vector<std::array<int, Z>> son;
    std::vector<int> link, len, dep, cnt;
    std::string s;
    int half{};

    int cur = 0, tot = 1;

    Pam(int n)
        : son(n + 2), link(n + 2), len(n + 2),
          dep(n + 2), cnt(n + 2) {
        link[0] = 1;
        len[1] = -1;
        half = n / 2;
    }

    Pam(const std::string &s) : Pam(s.size()) {
        for (int i = 0; i < s.size(); i++) {
            add(i, s[i]);
        }
    }

    void assign(int cur, int id) {
        cnt[cur]++;
    }

    int getLink(int x, int i) {
        while (i - len[x] - 1 < 0 or s[i - len[x] - 1] != s[i])
            x = link[x];
        return x;
    }

    void add(int i, char c) {
        c -= Base;
        s.push_back(c);

        int v = getLink(cur, i);

        if (!son[v][c]) {
            link[++tot] = son[getLink(link[v], i)][c];
            son[v][c] = tot;
            len[tot] = len[v] + 2;
            dep[tot] = dep[link[tot]] + 1;
        }

        cur = son[v][c];
        if (i >= half) {assign(cur, i);}
    }

    // Pam 的 linkTree 是 1 为根的
    auto getLinkTree() const {
        std::vector e(tot + 1, std::vector<int>());
        for (int i = 0; i <= tot; i++) {
            if (i != 1)
                e[link[i]].emplace_back(i);
        }
        return e;
    }
};

constexpr int P{998244353};

void solve()
{
    int n; std::cin >> n; std::string s; std::cin >> s; s = s + s;
    Pam<10, '0'> pam(s);
    auto tot{pam.tot}; auto len{pam.len}; auto cnt{pam.cnt}; auto link{pam.link};
    i64 ans{}; for (int i = tot; i >= 0; i--) {
        if (len[i] <= n) {ans += 1LL * cnt[i] * cnt[i] % P * len[i] % P; ans %= P;}
        cnt[link[i]] += cnt[i];
    }
    std::cout << ans << "\n";
}

signed main()
{
   std::cin.tie(nullptr)->sync_with_stdio(false);
   int _(1);
#ifdef tests
   std::cin >> _;
#endif
   while(_--) solve();
   return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3588kb

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3808kb

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3644kb

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

2
21

output:

2

result:

ok 1 number(s): "2"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3844kb

input:

3
233

output:

10

result:

ok 1 number(s): "10"

Test #7:

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

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

score: 0
Accepted
time: 31ms
memory: 140156kb

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

score: 0
Accepted
time: 131ms
memory: 421352kb

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

score: 0
Accepted
time: 127ms
memory: 422760kb

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

224009870

result:

ok 1 number(s): "224009870"

Test #11:

score: 0
Accepted
time: 124ms
memory: 421964kb

input:

3000000
8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...

output:

51985943

result:

ok 1 number(s): "51985943"

Test #12:

score: 0
Accepted
time: 103ms
memory: 422312kb

input:

3000000
1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...

output:

355676465

result:

ok 1 number(s): "355676465"

Test #13:

score: 0
Accepted
time: 119ms
memory: 421144kb

input:

3000000
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...

output:

788510374

result:

ok 1 number(s): "788510374"

Test #14:

score: 0
Accepted
time: 132ms
memory: 421860kb

input:

3000000
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...

output:

691884476

result:

ok 1 number(s): "691884476"

Test #15:

score: 0
Accepted
time: 92ms
memory: 421340kb

input:

3000000
0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...

output:

701050848

result:

ok 1 number(s): "701050848"

Test #16:

score: 0
Accepted
time: 78ms
memory: 422608kb

input:

3000000
2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...

output:

486861605

result:

ok 1 number(s): "486861605"

Test #17:

score: 0
Accepted
time: 108ms
memory: 421260kb

input:

3000000
4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...

output:

450625621

result:

ok 1 number(s): "450625621"

Test #18:

score: 0
Accepted
time: 112ms
memory: 421408kb

input:

3000000
1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...

output:

649551870

result:

ok 1 number(s): "649551870"

Extra Test:

score: 0
Extra Test Passed