QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#140406 | #6567. Repetitive String Invention | smosp# | TL | 1770ms | 14236kb | C++17 | 4.2kb | 2023-08-15 21:08:42 | 2023-08-15 21:08:44 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include "bits/extc++.h"
using namespace std;
// Source: ecnerwala
// Templates for Policy Based Data Structures
struct splitmix64_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
using namespace __gnu_pbds;
template <typename K, typename V, typename Hash = splitmix64_hash>
using hash_map = gp_hash_table<K, V, Hash>;
template <typename K, typename Hash = splitmix64_hash>
using hash_set = hash_map<K, null_type, Hash>;
template <class T, class Compare = less<>>
using ordered_set =
tree<T, null_type, Compare, rb_tree_tag, tree_order_statistics_node_update>;
using ll = int64_t;
ll euclid(ll a, ll b, ll& x, ll& y) {
if (!b) return x= 1, y = 0, a;
ll d = euclid(b, a%b, y, x);
return y -= a/b*x, d;
}
const ll MOD1 = 1e9+123, MOD2 = 1e9+7;
template<int MOD>
struct mod {
ll x;
mod(ll xx) : x(xx) {}
mod operator+(mod b) { return mod((x+b.x)%MOD); }
mod operator-(mod b) { return mod((x-b.x+MOD)%MOD); }
mod operator*(mod b) { return mod((x*b.x)%MOD); }
mod operator/(mod b) { return *this * invert(b); }
mod invert(mod a) {
ll x, y, g = euclid(a.x, MOD, x, y);
assert(g == 1); return mod((x+MOD)%MOD);
}
};
struct Mod {
mod<MOD1> x;
mod<MOD2> y;
Mod(ll xx):x(xx), y(xx) {}
Mod(ll xx, ll yy):x(xx), y(yy) {}
Mod(mod<MOD1> xx, mod<MOD2> yy):x(xx), y(yy) {}
Mod operator+(Mod b) { return Mod(x+b.x, y+b.y); }
Mod operator-(Mod b) { return Mod(x-b.x, y-b.y); }
Mod operator*(Mod b) { return Mod(x*b.x, y*b.y); }
Mod operator/(Mod b) { return Mod(x/b.x, y/b.y); }
Mod invert(Mod a) {
return Mod(a.x.invert(a.x), a.y.invert(a.y));
}
};
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
Mod base(rng(), rng());
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
const int MX = 1000;
vector<Mod> pows(MX, Mod(0)), ipows(MX, Mod(0));
pows[0] = ipows[0] = 1;
auto ibase = base.invert(base);
for (int i = 1; i < MX; i++) {
pows[i] = pows[i-1]*base;
ipows[i] = ipows[i-1]*ibase;
}
string s;
cin >> s;
int n = s.size();
vector<Mod> H(n+1, Mod(0));
for (int i = 0; i < n; i++) {
H[i+1] = H[i] + Mod(ll(s[i] - 'a'+1))*pows[i];
}
// [l, r)
auto get = [&](int l, int r)->Mod{
return (H[r] - H[l])*ipows[l];
};
vector<vector<Mod>> HM(n+1, vector<Mod>(n+1, Mod(0)));
for (int l = 0; l < n; l++) {
for (int r = l+1; r <= n; r++) {
HM[l][r] = get(l, r);
}
}
auto norm = [&](Mod m)->uint64_t {
return (m.x.x<<30)|m.y.x;
};
int64_t ans = 0, single = 0;
for (int l = 0; l < n;l++) {
// calculate the admissible hashes which don't touch l
vector<hash_map<ll,int>> hash_by_length(n+1);
for (int i = 0; i < n; i++) {
for (int j = i+1; j <= n; j++) {
if (l >= i && l < j) continue;
hash_by_length[j-i][norm(HM[i][j])]++;
}
}
for (int r = l+1; r <= n; r++) {
// Calculate matches
int len = r-l;
Mod full = HM[l][r];
for (int k = (len+1)/2; k <= len; k++) {
// valid check
Mod half = HM[l][l+k];
Mod extra = HM[l+k][r];
int extra_len = len - k;
Mod want = half - extra;
want = want*ipows[extra_len];
int needed = 2*k - len;
if (len == k) {
single += hash_by_length[needed][norm(want)];
}
else {
ans += hash_by_length[needed][norm(want)];
}
}
// Remove all suffixes starting at r
for (int j = r+1; j <= n; j++) {
int len = j - r;
hash_by_length[len][norm(HM[r][j])]--;
}
}
}
cout << ans + single/2 << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3520kb
input:
aaaa
output:
9
result:
ok single line: '9'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3604kb
input:
axabxbcxcdxd
output:
22
result:
ok single line: '22'
Test #3:
score: 0
Accepted
time: 209ms
memory: 5980kb
input:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
output:
536006700
result:
ok single line: '536006700'
Test #4:
score: 0
Accepted
time: 386ms
memory: 7348kb
input:
abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...
output:
136016600
result:
ok single line: '136016600'
Test #5:
score: 0
Accepted
time: 1ms
memory: 3520kb
input:
a
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3616kb
input:
ab
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3580kb
input:
aa
output:
1
result:
ok single line: '1'
Test #8:
score: 0
Accepted
time: 1537ms
memory: 13228kb
input:
bbbbbrrrrbrbrrbrbrbrbbrrbbbbrrbrrbrbrbrrrrrbbbrrbrrbbrrrbbbbbrrrrbbrbbrrrrrbbrrrbbbbbrbbrbbbrbrrbrrrrrrrrbbrbrbbrrrrrrbbbbbrbrrrrbrrbbrbrrrrrbrbrbbrbrrbrrrrbrbbrbrrbrrbbbbrrrrrbrbbrbbrrrrrbrbrbrbbbrrrrrrrbbbbbbrrbrbrrrrrbbbrbrrrrbbbbrrbrrbbbbbbbrbbbbrrrbrbbbrrrrrrbbbbbrbrrbbbbrbrbrrbbrrrbbbrbbbrbbrr...
output:
209015
result:
ok single line: '209015'
Test #9:
score: 0
Accepted
time: 1684ms
memory: 13716kb
input:
fkrryithikkpurfezufbthxxrtejxpprhrhurrfziigzgajipayrefarpxuggehkiggeppfjghuyejuagkjpiezhmfizgphkkfffifihrimuhgtgzykfupbprjyrbpbimapjmbkrhubzhgfhhpkrxgjgmkheiiugiyerggyukyfuuxgrehgayafzuxymxxabygpapbjxipkxgbxhzpekypfghaumukguaipphiyhzimzfmpmzahbmaagytygyekzhktbbheetxrbipgytppgbhkpimrybkbeirhrzytfehba...
output:
5696
result:
ok single line: '5696'
Test #10:
score: 0
Accepted
time: 1754ms
memory: 14236kb
input:
ayeaecauuueerrcsyyyuayauarecyrruueeucacrrcesercyeasuscucscruaaresaeruurreeursssuyraeysurecacueuacycacsyuyeyusssassaasyuarrcecueayacucuyyrsasrycyrrcececyuccecyreyeccuesaasceasussaraeruyarcauarcecacrucryryseysauaucyaeyurueurerrccrayyyycccsuaaesrecurucerrascyacucucryarycayeesyysusserarseyrauyrryeacauec...
output:
18249
result:
ok single line: '18249'
Test #11:
score: 0
Accepted
time: 1499ms
memory: 12972kb
input:
lclccclcclllccccclcclclcccclllllcclcclclcclllllcllccclclclccclccllcclccccccllccclclcccclclcclclccclllllccclcccccclllllccllcclclllllllccclllcllllccccccccllccclllccclcllllllllcclccclccccllccllclclcclcllcllccclcllclllclclllclllcllcclclllccclcclclllclllllllcclllcccccllcllcllcclcclcllccclclllclclcccllcll...
output:
207835
result:
ok single line: '207835'
Test #12:
score: 0
Accepted
time: 1689ms
memory: 14204kb
input:
lygxvkxeeiixzzpamosqadacrnyierrztikxheupgbkfkstyjrmyvhmjrhfhwfdeyhzuyhhbczbjgcnzouiufetegmnvxoeqxiykkwysdnnmdjtpwwzpdaqzmwmapbqbdjsdoxmpbvseyfrrqpgteehoxqsjszykfqgdmcgwikavcnhoubonianpecjtumdutpxwioqtkystzndrbtlfircdsnsxraoitswvhjirqxxjrnjjsldbmdcjngecdaotrgxvikyfyjlbbeketxrxeknuxmxehvabyyyxnrchukft...
output:
3442
result:
ok single line: '3442'
Test #13:
score: 0
Accepted
time: 1638ms
memory: 12780kb
input:
yjjxyyxjxyjjyxyyjxyyjxjxjxjyxyxxjxjjyxxxxxjjxyxxyjjxxjyyjjjyyyxjyjxjxyjjyjxjyjyyxjyjyjyyxxyyxxxjjxxxyyyjjjyxjyxjxjyjxjyyyjjxxxjxxjxjjjyjxjyjyyxjyjyjyyxxyyxxxjjxxxyyyjjjyxjyxjxjyjxjyyyjjxxxjxxjxjyjjxyyxjxyjjyxyyjxyyjxjxjxjyxyxxjxjjyxxxxxjjxyxxyjjxxjyyjjjyyyxjyjxjxyjjxyyxjxyjjyxyyjxyyjxjxjxjyxyxxjxjjy...
output:
103111
result:
ok single line: '103111'
Test #14:
score: 0
Accepted
time: 1630ms
memory: 13808kb
input:
lhllhlhlhllhlhllhhlhhhhllhhlhhhhllhhlhhhhllhllhhllhhlhhhhllhllhhhhhllhhlhhhhhlhllhlhhlhllhlhhhhllhllhhhllhhlhhhhhhlhllhlhllhhlhhhhhhlhllhlhhllhllllhllhhhlhllhlhhhllhhlhhhhhhllhhlhhhhlhllhlhllhllhhhhllhllllhllllhllllhlllhllhlhllhhlhhhhhhhllhlllhllhlhllhhlhhhhllhllllhlllhllhlhllhllhhhhhhhllhhlhhhhllhh...
output:
298186
result:
ok single line: '298186'
Test #15:
score: 0
Accepted
time: 1618ms
memory: 12908kb
input:
blbllblbblbllblbblbllblblblbblllblbbllblbllblbblbllblbblbllblblblbbllblbllblblblbbllblbllblbblbllblblblbbllblbllblblblbbllblbllblbblbllblbblbllblblblbblllblbblllblbblllblbbllblbllblblblbblllblbblllblbbllblbllblblblbblllblbbllblbllblbblbllblbblbllblblblbblllblbblllblbbllblbllblblblbblllblbblllblbblll...
output:
510790
result:
ok single line: '510790'
Test #16:
score: 0
Accepted
time: 1403ms
memory: 12628kb
input:
aqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaqaaqaqqaaqqaqaaqaqqaqaaqqaaqaqqaaqqaqaaqqaaq...
output:
584698
result:
ok single line: '584698'
Test #17:
score: 0
Accepted
time: 1770ms
memory: 13648kb
input:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
output:
8554693400
result:
ok single line: '8554693400'
Test #18:
score: -100
Time Limit Exceeded
input:
abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...