Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using i64 = long long;
- vector<int> getPi(const string &s) {
- int n = (int)s.size();
- vector<int> pi(n, 0);
- for (int i = 1, j = 0; i < n; ++i) {
- while (j > 0 and s[i] != s[j]) j = pi[j - 1];
- if (s[i] == s[j]) ++j;
- pi[i] = j;
- }
- return pi;
- }
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- int n; cin >> n;
- string s; cin >> s;
- auto pi = getPi(s);
- i64 ans = 0;
- for (int i = n; i >= 2; --i) {
- int d = i - pi[i-1];
- if (i % d == 0) {
- ans += (i - d);
- } else {
- ans += (d * (i / d));
- }
- }
- cout << ans << '\n';
- }
- /*
- b -> 0
- ba -> 0
- bab -> ba 2
- baba -> ba 2
- babab -> baba 4
- bababa -> baba 4
- bababab -> bababa 6
- babababa -> bababa 6
- total = 24
- */
Advertisement
Add Comment
Please, Sign In to add comment