QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#112193 | #6567. Repetitive String Invention | maomao90# | TL | 1550ms | 4280kb | C++17 | 3.2kb | 2023-06-10 15:54:59 | 2023-06-10 15:55:01 |
Judging History
answer
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 805;
const ii X = {31, 31};
const ii INVX = {128805723, 129032259};
const int MOD1 = 998244353, MOD2 = 1000000007;
int n;
string s;
ii pw[MAXN], ipw[MAXN];
unordered_map<ll, int> mp;
ll ans;
inline ll iitoll(ii x) {
return (ll) x.FI * MOD2 + x.SE;
}
ii operator+ (const ii &l, const ii &r) {
ii tmp = {l.FI + r.FI, l.SE + r.SE};
if (tmp.FI >= MOD1) {
tmp.FI -= MOD1;
}
if (tmp.SE >= MOD2) {
tmp.SE -= MOD2;
}
return tmp;
}
ii operator- (const ii &l, const ii &r) {
ii tmp = {l.FI - r.FI, l.SE - r.SE};
if (tmp.FI < 0) {
tmp.FI += MOD1;
}
if (tmp.SE < 0) {
tmp.SE += MOD2;
}
return tmp;
}
ii operator* (const ii &l, const ii &r) {
return {(ll) l.FI * r.FI % MOD1, (ll) l.SE * r.SE % MOD2};
}
ii operator* (const ii &l, const int &r) {
return {(ll) l.FI * r % MOD1, (ll) l.SE * r % MOD2};
}
ll solve(bool z) {
ll res = 0;
mp.clear();
RREP (i, n - 2, 0) {
ii chsh = {0, 0};
REP (j, i + 1, n) {
chsh = chsh + pw[j - i - 1] * (s[j] - 'a' + 1);
mp[iitoll(chsh)]++;
}
chsh = {0, 0};
RREP (j, i, 0) {
chsh = chsh * X + ii{s[j] - 'a' + 1, s[j] - 'a' + 1};
ii lhsh = chsh, rhsh = {0, 0};
RREP (k, i, (j - i) / 2) {
// 2 * (k - j + 1) > i - j + 1
// 2 * k - 2 * j + 2 > i - j + 1
// 2 * k > j - i - 1
// 2 * k >= j - i
if (k != i || z) {
ii tmp = (lhsh - rhsh) * ipw[i - k];
if (i == 2 && j == 0 && k == 1) {
cerr << tmp.FI << ' ' << tmp.SE << '\n';
}
res += mp[iitoll(tmp)];
}
lhsh = lhsh - pw[k - j] * (s[k] - 'a' + 1);
rhsh = rhsh * X + ii{s[k] - 'a' + 1, s[k] - 'a' + 1};
}
}
}
cerr << res << '\n';
return res;
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> s;
n = SZ(s);
pw[0] = ipw[0] = {1, 1};
REP (i, 1, n) {
pw[i] = pw[i - 1] * X;
ipw[i] = ipw[i - 1] * INVX;
}
ans += solve(1);
reverse(ALL(s));
ans += solve(0);
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3384kb
input:
aaaa
output:
9
result:
ok single line: '9'
Test #2:
score: 0
Accepted
time: 2ms
memory: 3420kb
input:
axabxbcxcdxd
output:
22
result:
ok single line: '22'
Test #3:
score: 0
Accepted
time: 1550ms
memory: 4280kb
input:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
output:
536006700
result:
ok single line: '536006700'
Test #4:
score: -100
Time Limit Exceeded
input:
abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...