QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#303383 | #7876. Cyclic Substrings | mendicillin2 | WA | 166ms | 408288kb | C++17 | 2.5kb | 2024-01-12 13:11:11 | 2024-01-12 13:11:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
using ll = int64_t;
template <class F> struct ycr { /// start-hash
F f;
template <class T> explicit ycr(T&& f_) : f(forward<T>(f_)) {}
template <class... A> decltype(auto) operator()(A&&... as) {
return f(ref(*this), forward<A>(as)...);
}
};
template <class F> decltype(auto) yc(F&& f) {
return ycr<decay_t<F>>(forward<F>(f));
} /// end-hash
// 0, ..., K-1
template <int K> struct Eertree {
struct Node {
array<int, K> ch;
int fail;
int l, r; // location of the first ocurrence
Node(int f_, int l_, int r_) : ch{}, fail(f_), l(l_), r(r_) {}
int len() const { return r-l; }
};
V<Node> x;
V<int> buf;
int cur;
Eertree(int alloc = 0) {
if (alloc) {
x.reserve(alloc+2);
buf.reserve(alloc);
}
x.emplace_back(-1, 1, 0);
x.emplace_back(0, 0, 0);
reset();
}
void reset() {
cur = 1;
buf.clear();
}
int append(int a) {
int i = int(buf.size());
buf.push_back(a);
auto works = [&](int v) -> bool {
int l = i - x[v].len();
return l > 0 && buf[l-1] == a;
};
for (; !works(cur); cur = x[cur].fail) {}
if (!x[cur].ch[a]) {
int par = x[cur].fail;
if (par != -1) {
for (; !works(par); par = x[par].fail) {}
}
int npar = (par == -1 ? 1 : x[par].ch[a]);
x[cur].ch[a] = int(x.size());
x.emplace_back(npar, i - x[cur].len() - 1, i + 1);
}
assert(x[cur].ch[a]);
cur = x[cur].ch[a];
return cur;
}
int size() const {
return int(x.size());
}
const Node& operator [](int i) const {
return x[i];
}
};
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int N;
cin >> N;
string S;
cin >> S;
S += S;
Eertree<10> et(2 * N);
V<int> locs; locs.reserve(2 * N);
for (char c : S) {
int v = int(c - '0');
assert(0 <= v && v < 10);
locs.push_back(et.append(v));
}
int nnodes = et.size();
V<int> f_before(nnodes);
V<int> f_all(nnodes);
for (int i = 0; i < N; i++) {
int v = locs[i];
f_before[v]++;
f_all[v]++;
}
for (int i = N; i < 2*N; i++) {
int v = locs[i];
f_all[v]++;
}
for (int v = nnodes-1; v >= 1; v--) {
int p = et[v].fail;
f_before[p] += f_before[v];
f_all[p] += f_all[v];
}
const ll MOD = 998244353;
ll ans = 0;
for (int v : locs) {
int len = et[v].len();
if (len <= N) {
ll f_cyc = f_all[v] - f_before[v];
f_cyc %= MOD;
ll val = len * ll(f_cyc) % MOD * f_cyc % MOD;
ans += val;
}
}
ans %= MOD;
cout << ans << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
input:
5 01010
output:
39
result:
ok 1 number(s): "39"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
8 66776677
output:
192
result:
ok 1 number(s): "192"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3756kb
input:
1 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3476kb
input:
2 22
output:
12
result:
ok 1 number(s): "12"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3808kb
input:
2 21
output:
2
result:
ok 1 number(s): "2"
Test #6:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
3 233
output:
10
result:
ok 1 number(s): "10"
Test #7:
score: 0
Accepted
time: 0ms
memory: 3532kb
input:
3 666
output:
54
result:
ok 1 number(s): "54"
Test #8:
score: 0
Accepted
time: 43ms
memory: 137852kb
input:
1000000 3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...
output:
496166704
result:
ok 1 number(s): "496166704"
Test #9:
score: 0
Accepted
time: 166ms
memory: 408288kb
input:
3000000 2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...
output:
890701718
result:
ok 1 number(s): "890701718"
Test #10:
score: -100
Wrong Answer
time: 132ms
memory: 216692kb
input:
3000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...
output:
672695901
result:
wrong answer 1st numbers differ - expected: '224009870', found: '672695901'