QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#274652#7876. Cyclic Substringshos_lyricAC ✓162ms363548kbC++1411.5kb2023-12-03 19:42:012023-12-03 19:42:01

Judging History

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

  • [2023-12-03 19:42:01]
  • 评测
  • 测评结果:AC
  • 用时:162ms
  • 内存:363548kb
  • [2023-12-03 19:42:01]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


// alphabet is [OFFSET, OFFSET + SIZE), with sentinel (OFFSET - 1)
template <class T, int SIZE, T OFFSET> struct Pam {
  struct Node {
    // ts[pos, pos + len]: suffix when created
    // fail: longest proper palindromic suffix
    int len, pos, fail;
    int nxt[SIZE];
  };

  // nodes[0]: length 0  (also means null in nxt)
  // nodes[1]: length -1
  // nodes[2, nodesLen): non-empty palindromic substring
  // suf: node for longest palindromic suffix
  int nodesLen, suf;
  vector<Node> nodes;
  // current whole string: ts[0, r)  (0 <= r <= nR)
  int nR, r;
  vector<T> tsBuffer;
  T *ts;

  Pam(int nR_) : nR(nR_) {
    nodesLen = 2;
    suf = 0;
    nodes.resize(2 + nR);
    nodes[0].len =  0; nodes[0].pos = 0; nodes[0].fail = 1;
    nodes[1].len = -1; nodes[1].pos = 0; nodes[1].fail = 1;
    memset(nodes[0].nxt, 0, sizeof(nodes[0].nxt));
    memset(nodes[1].nxt, 0, sizeof(nodes[1].nxt));
    r = 0;
    tsBuffer.assign(1 + nR, OFFSET - 1);
    ts = tsBuffer.data() + 1;
  }
  const Node &operator[](int u) const {
    return nodes[u];
  }

  void pushBack(T t) {
    assert(r < nR);
    const int a = t - OFFSET;
    ts[r++] = t;
    for (; ts[r - 2 - nodes[suf].len] != t; suf = nodes[suf].fail) {}
    Node &f = nodes[suf];
    if (!f.nxt[a]) {
      Node &g = nodes[nodesLen];
      g.len = f.len + 2; g.pos = r - g.len;
      int u = f.fail;
      for (; ts[r - 2 - nodes[u].len] != t; u = nodes[u].fail) {}
      g.fail = nodes[u].nxt[a];
      memset(g.nxt, 0, sizeof(g.nxt));
      f.nxt[a] = nodesLen++;  // this needs to be after setting g.fail
    }
    suf = f.nxt[a];
  }

  void dfsPrint(ostream &os, int u, const string &branch, int type) const {
    const Node &f = nodes[u];
    os << branch << ((type == 0) ? "" : (type == 1) ? "|-- " : "`-- ");
    if (f.len <= 0) {
      os << "(" << f.len << ")";
    } else {
      for (int i = f.pos; i < f.pos + f.len; ++i) os << ts[i];
    }
    os << " " << u << " " << f.fail;
    // debug here
    os << "\n";
    int a0 = -1;
    for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) a0 = a;
    for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) {
      dfsPrint(os, f.nxt[a],
               branch + ((type == 0) ? "" : (type == 1) ? "|   " : "    "),
               (a == a0) ? 2 : 1);
    }
  }
  friend ostream &operator<<(ostream &os, const Pam &pam) {
    pam.dfsPrint(os, 0, "  ", 0);
    pam.dfsPrint(os, 1, "", 0);
    return os;
  }
};

// alphabet is [OFFSET, OFFSET + SIZE), with sentinel (OFFSET - 1)
template <class T, int SIZE, T OFFSET> struct Depam {
  struct Node {
    // ts[pos, pos + len]: prefix/suffix when created
    // fail: longest proper palindromic prefix/suffix
    // quick[a]: longest proper palindromic prefix/suffix followed/preceded by a
    int len, pos, fail;
    int nxt[SIZE], quick[SIZE];
  };

  // nodes[0]: length 0  (also means null in nxt)
  // nodes[1]: length -1
  // nodes[2, nodesLen): non-empty palindromic substring
  // pre/suf: node for longest palindromic prefix/suffix
  int nodesLen, pre, suf;
  vector<Node> nodes;
  // current whole string: ts[l, r)  (-nL <= l <= 0 <= r <= nR)
  int nL, nR, l, r;
  vector<T> tsBuffer;
  T *ts;
  // ((~pre)/suf before pushFront/pushBack, parent of created node or -1)
  int historyLen;
  vector<pair<int, int>> history;

  Depam(int nL_, int nR_) : nL(nL_), nR(nR_) {
    nodesLen = 2;
    pre = suf = 0;
    nodes.resize(2 + nL + nR);
    nodes[0].len =  0; nodes[0].pos = 0; nodes[0].fail = 1;
    nodes[1].len = -1; nodes[1].pos = 0; nodes[1].fail = 1;
    memset(nodes[0].nxt, 0, sizeof(nodes[0].nxt));
    memset(nodes[1].nxt, 0, sizeof(nodes[1].nxt));
    for (int a = 0; a < SIZE; ++a) nodes[0].quick[a] = 1;
    for (int a = 0; a < SIZE; ++a) nodes[1].quick[a] = 1;
    l = r = 0;
    tsBuffer.assign(1 + nL + nR + 1, OFFSET - 1);
    ts = tsBuffer.data() + (1 + nL);
    historyLen = 0;
    history.resize(nL + nR);
  }
  const Node &operator[](int u) const {
    return nodes[u];
  }

  void pushFront(T t) {
    assert(-nL < l);
    const int a = t - OFFSET;
    history[historyLen++] = make_pair(~pre, -1);
    ts[--l] = t;
    if (ts[l + 1 + nodes[pre].len] != t) pre = nodes[pre].quick[a];
    Node &f = nodes[pre];
    if (!f.nxt[a]) {
      history[historyLen - 1].second = pre;
      Node &g = nodes[nodesLen];
      g.len = f.len + 2; g.pos = l; g.fail = nodes[f.quick[a]].nxt[a];
      memset(g.nxt, 0, sizeof(g.nxt));
      memcpy(g.quick, nodes[g.fail].quick, sizeof(g.quick));
      g.quick[ts[l + nodes[g.fail].len] - OFFSET] = g.fail;
      f.nxt[a] = nodesLen++;  // this needs to be after setting g.fail
    }
    if (nodes[pre = f.nxt[a]].len == r - l) suf = pre;
  }
  void pushBack(T t) {
    assert(r < nR);
    const int a = t - OFFSET;
    history[historyLen++] = make_pair(suf, -1);
    ts[r++] = t;
    if (ts[r - 2 - nodes[suf].len] != t) suf = nodes[suf].quick[a];
    Node &f = nodes[suf];
    if (!f.nxt[a]) {
      history[historyLen - 1].second = suf;
      Node &g = nodes[nodesLen];
      g.len = f.len + 2; g.pos = r - g.len; g.fail = nodes[f.quick[a]].nxt[a];
      memset(g.nxt, 0, sizeof(g.nxt));
      memcpy(g.quick, nodes[g.fail].quick, sizeof(g.quick));
      g.quick[ts[r - 1 - nodes[g.fail].len] - OFFSET] = g.fail;
      f.nxt[a] = nodesLen++;  // this needs to be after setting g.fail
    }
    if (nodes[suf = f.nxt[a]].len == r - l) pre = suf;
  }
  void undo() {
    const pair<int, int> h = history[--historyLen];
    if (h.first < 0) {
      // pushFront
      if (nodes[pre].len == r - l) suf = nodes[suf].fail;
      pre = ~h.first;
      if (~h.second) {
        --nodesLen;
        nodes[h.second].nxt[ts[l] - OFFSET] = 0;
      }
      ts[l++] = OFFSET - 1;
    } else {
      // pushBack
      if (nodes[suf].len == r - l) pre = nodes[pre].fail;
      suf = h.first;
      if (~h.second) {
        --nodesLen;
        nodes[h.second].nxt[ts[r - 1] - OFFSET] = 0;
      }
      ts[--r] = OFFSET - 1;
    }
  }

  void dfsPrint(ostream &os, int u, const string &branch, int type) const {
    const Node &f = nodes[u];
    os << branch << ((type == 0) ? "" : (type == 1) ? "|-- " : "`-- ");
    if (f.len <= 0) {
      os << "(" << f.len << ")";
    } else {
      for (int i = f.pos; i < f.pos + f.len; ++i) os << ts[i];
    }
    os << " " << u << " " << f.fail;
    // debug here
    os << "\n";
    int a0 = -1;
    for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) a0 = a;
    for (int a = 0; a < SIZE; ++a) if (f.nxt[a]) {
      dfsPrint(os, f.nxt[a],
               branch + ((type == 0) ? "" : (type == 1) ? "|   " : "    "),
               (a == a0) ? 2 : 1);
    }
  }
  friend ostream &operator<<(ostream &os, const Depam &depam) {
    depam.dfsPrint(os, 0, "  ", 0);
    depam.dfsPrint(os, 1, "", 0);
    return os;
  }
};

////////////////////////////////////////////////////////////////////////////////


int N;
char S[3'000'010];

int main() {
  for (; ~scanf("%d", &N); ) {
    scanf("%s", S);
    
    Pam<char, 10, '0'> pam(2 * N);
    vector<int> us(2 * N + 1);
    us[0] = pam.suf;
    for (int i = 0; i < N; ++i) {
      pam.pushBack(S[i]);
      us[i + 1] = pam.suf;
    }
    for (int i = 0; i < N; ++i) {
      pam.pushBack(S[i]);
      us[N + i + 1] = pam.suf;
    }
// cerr<<pam<<endl;
// cerr<<us<<endl;
    
    vector<Mint> dp(pam.nodesLen, 0);
    for (int i = N + 1; i <= 2 * N; ++i) {
      dp[us[i]] += 1;
    }
    for (int u = pam.nodesLen; --u >= 2; ) {
      dp[pam[u].fail] += dp[u];
    }
// cerr<<"dp = "<<dp<<endl;
    Mint ans = 0;
    for (int u = 2; u < pam.nodesLen; ++u) if (pam[u].len <= N) {
      ans += dp[u] * dp[u] * pam[u].len;
    }
    printf("%u\n", ans.x);
  }
  return 0;
}

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

详细

Test #1:

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

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

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

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

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

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

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

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

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

input:

2
21

output:

2

result:

ok 1 number(s): "2"

Test #6:

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

input:

3
233

output:

10

result:

ok 1 number(s): "10"

Test #7:

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

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

score: 0
Accepted
time: 40ms
memory: 125360kb

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

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

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

score: 0
Accepted
time: 106ms
memory: 350916kb

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

224009870

result:

ok 1 number(s): "224009870"

Test #11:

score: 0
Accepted
time: 96ms
memory: 363532kb

input:

3000000
8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...

output:

51985943

result:

ok 1 number(s): "51985943"

Test #12:

score: 0
Accepted
time: 114ms
memory: 363548kb

input:

3000000
1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...

output:

355676465

result:

ok 1 number(s): "355676465"

Test #13:

score: 0
Accepted
time: 139ms
memory: 360744kb

input:

3000000
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...

output:

788510374

result:

ok 1 number(s): "788510374"

Test #14:

score: 0
Accepted
time: 162ms
memory: 361460kb

input:

3000000
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...

output:

691884476

result:

ok 1 number(s): "691884476"

Test #15:

score: 0
Accepted
time: 88ms
memory: 352112kb

input:

3000000
0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...

output:

701050848

result:

ok 1 number(s): "701050848"

Test #16:

score: 0
Accepted
time: 71ms
memory: 343556kb

input:

3000000
2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...

output:

486861605

result:

ok 1 number(s): "486861605"

Test #17:

score: 0
Accepted
time: 116ms
memory: 360044kb

input:

3000000
4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...

output:

450625621

result:

ok 1 number(s): "450625621"

Test #18:

score: 0
Accepted
time: 100ms
memory: 359420kb

input:

3000000
1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...

output:

649551870

result:

ok 1 number(s): "649551870"

Extra Test:

score: 0
Extra Test Passed