QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#303386#7876. Cyclic Substringsmendicillin2AC ✓172ms407696kbC++172.5kb2024-01-12 13:14:162024-01-12 13:14:16

Judging History

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

  • [2024-01-12 13:14:16]
  • 评测
  • 测评结果:AC
  • 用时:172ms
  • 内存:407696kb
  • [2024-01-12 13:14:16]
  • 提交

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 = 0; v < nnodes; v++) {
		int len = et[v].len();
		if (1 <= len && 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;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

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

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

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

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

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

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

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

input:

2
21

output:

2

result:

ok 1 number(s): "2"

Test #6:

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

input:

3
233

output:

10

result:

ok 1 number(s): "10"

Test #7:

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

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

score: 0
Accepted
time: 39ms
memory: 138044kb

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

score: 0
Accepted
time: 136ms
memory: 407696kb

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

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

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

224009870

result:

ok 1 number(s): "224009870"

Test #11:

score: 0
Accepted
time: 121ms
memory: 407652kb

input:

3000000
8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...

output:

51985943

result:

ok 1 number(s): "51985943"

Test #12:

score: 0
Accepted
time: 113ms
memory: 407536kb

input:

3000000
1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...

output:

355676465

result:

ok 1 number(s): "355676465"

Test #13:

score: 0
Accepted
time: 146ms
memory: 364260kb

input:

3000000
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...

output:

788510374

result:

ok 1 number(s): "788510374"

Test #14:

score: 0
Accepted
time: 172ms
memory: 376772kb

input:

3000000
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...

output:

691884476

result:

ok 1 number(s): "691884476"

Test #15:

score: 0
Accepted
time: 87ms
memory: 231752kb

input:

3000000
0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...

output:

701050848

result:

ok 1 number(s): "701050848"

Test #16:

score: 0
Accepted
time: 46ms
memory: 108256kb

input:

3000000
2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...

output:

486861605

result:

ok 1 number(s): "486861605"

Test #17:

score: 0
Accepted
time: 117ms
memory: 352640kb

input:

3000000
4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...

output:

450625621

result:

ok 1 number(s): "450625621"

Test #18:

score: 0
Accepted
time: 135ms
memory: 344980kb

input:

3000000
1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...

output:

649551870

result:

ok 1 number(s): "649551870"

Extra Test:

score: 0
Extra Test Passed