QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#608496 | #7814. 消消乐 | tiankonguse | 100 ✓ | 236ms | 15492kb | C++20 | 2.6kb | 2024-10-03 22:14:12 | 2024-10-03 22:14:14 |
Judging History
answer
/*
ID: tiankonguse
TASK: game
LANG: C++
CONTEST: CSP-S 2023
OJ: https://qoj.ac/problem/7814
*/
#define TASK "game"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int mod1e7 = 1000000007, mod1e9 = 1000000009;
const ll BASE = 29;
const int N = 2000010;
const int M = 26;
char str[N];
void InitIO() {
// #ifndef USACO_LOCAL_JUDGE
// freopen(TASK ".in", "r", stdin);
// freopen(TASK ".out", "w", stdout);
// #endif
}
mt19937 rnd(time(0));
struct Matrix {
ll P;
int sz;
vector<vector<ll>> a;
Matrix(int sz = 2) : sz(sz) { //
Init(sz);
}
void Init(int n, ll p = 998244353) {
P = p;
sz = n;
a.clear();
a.resize(n, vector<ll>(n, 0));
}
void Union() {
int l = sz;
while (l--) {
a[l][l] = 1;
}
}
bool IsUnion() {
for (int i = 0; i < sz; i++) {
for (int j = 0; j < sz; j++) {
if (i == j && a[i][j] != 1) return false;
if (i != j && a[i][j] != 0) return false;
}
}
return true;
}
Matrix operator*(Matrix& B) {
Matrix ret(sz);
for (int i = 0; i < sz; i++)
for (int j = 0; j < sz; j++)
for (int k = 0; k < sz; k++)
ret.a[i][j] = (ret.a[i][j] + a[i][k] * B.a[k][j]) % P;
return ret;
}
Matrix Inv() { return Pow(P - 2); }
bool IsInv() { return Pow(P - 1).IsUnion(); }
Matrix Pow(ll k) {
Matrix ret(sz);
Matrix A = *this;
ret.Union();
while (k) {
if (k & 1) ret = ret * A;
A = A * A;
k >>= 1;
}
return ret;
}
void Rand() {
for (int i = 0; i < sz; i++)
for (int j = 0; j < sz; j++) {
a[i][j] = rnd() % P;
}
}
ull Hash() {
ull res = 0;
for (int i = 0; i < sz; i++) {
ull tmp = 1;
for (int j = 0; j < sz; j++) {
tmp *= a[i][j];
}
res ^= tmp;
}
return res;
}
};
/*
*/
void Solver() {
ll n;
scanf("%lld%s", &n, str);
vector<Matrix> mat(M), rmat(M);
for (int i = 0; i < 26; i++) {
Matrix& t = mat[i];
t.Rand();
while (!t.IsInv()) t.Rand();
rmat[i] = t.Inv();
}
ll ans = 0;
Matrix pre(2);
pre.Union();
unordered_map<ll, ll> h;
h[pre.Hash()]++;
for (int i = 0; i < n; i++) {
const char c = str[i];
const ll v = c - 'a';
pre = pre * (i & 1 ? mat[v] : rmat[v]);
ull tmp = pre.Hash();
ans += h[tmp];
h[tmp]++;
}
printf("%lld\n", ans);
}
int main() {
InitIO();
Solver();
return 0;
}
/*
8
accabccb
*/
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 5
Accepted
time: 1ms
memory: 4156kb
input:
10 aaaaaaaaaa
output:
25
result:
ok single line: '25'
Test #2:
score: 5
Accepted
time: 1ms
memory: 3872kb
input:
10 ooooppuuhh
output:
16
result:
ok single line: '16'
Test #3:
score: 5
Accepted
time: 1ms
memory: 3880kb
input:
10 nnppkkppjj
output:
16
result:
ok single line: '16'
Test #4:
score: 5
Accepted
time: 1ms
memory: 3880kb
input:
10 vvcckkhhqq
output:
15
result:
ok single line: '15'
Test #5:
score: 5
Accepted
time: 1ms
memory: 4148kb
input:
10 llmmxxoopp
output:
15
result:
ok single line: '15'
Test #6:
score: 5
Accepted
time: 1ms
memory: 3868kb
input:
800 jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjddzziiyyyyjjooppiivvppmmooppttnnbbmmaaqqvvkkllyyvvmmjjsswwxxccwweeeekkhhaakkvvffaaiieessmmiibbff...
output:
38306
result:
ok single line: '38306'
Test #7:
score: 5
Accepted
time: 1ms
memory: 3832kb
input:
800 pffpgppguqqugllgwhhwxjjxpkkpxnnxlkklunnuxiixallayqqyccccjnnjnyynqccqqjjqtuutboobwddwfwwfgvvgwuuwpqqppttpyggysrrsgwwgeddexbbxbssbvbbvbeebweewwkkwwddwnoontddtcddcajjapffptcctkeekboobfjjfvaavpjjpriirlfflnazdsevmozgezlngnhktrllcgorjoollwtqylmmzxfeibnifcebsnnugzluzlizievzxfnlokzodymigextlmyltzbmptyjf...
output:
27479
result:
ok single line: '27479'
Test #8:
score: 5
Accepted
time: 0ms
memory: 3940kb
input:
8000 vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv...
output:
3820477
result:
ok single line: '3820477'
Test #9:
score: 5
Accepted
time: 2ms
memory: 3976kb
input:
8000 neenbnnbvjjvddddommolrrlzttzqwwqlfflijjiaqqayaayihhiqmmqduudlzzlyzzyannatyytfiifbqqbroorcffcgjjgwhhwoqqopuupdjjdeqqewnnwvwwvpiiprqqrukkuhbbhsiisrqqrxvvxfmmfzkkzjlljimmiazzariirdvvdcuucpaapbaabbaabccccxqqxivvipttpqffqmggmzhhzdaadyxxyhccheddeaiianggnxjjxqssqfaafqllqskksfuufviivozzocwwcznnzfxxfnss...
output:
2713693
result:
ok single line: '2713693'
Test #10:
score: 5
Accepted
time: 2ms
memory: 4204kb
input:
8000 ivviurrukrrkaccafoofaxxaleelcyycsiisimmifvvfoeeodppdneenqjjqsccsguugonnoyiiymttmcjjcmkkmjwwjieeikeekdbbdolloyvvyolloxiixmaamfnnfzrrzqbbqweewvttvboobbeebcmmcsqqsiyyifddfyvvyvttvvddvejjeeeeeleellzzlzbbzcrrcobbowsswquuqozzoyqqyulluwvvwyxxyxllxwrrwgmmgeppecvvcsiisyvvyukkuyppyryyrhjjhthhtvssvvnnvnee...
output:
2721590
result:
ok single line: '2721590'
Test #11:
score: 5
Accepted
time: 42ms
memory: 13020kb
input:
200000 tdaatgfvypbdkxdlvttiuksoooxszoqnqvmtznizlqfstlenqrnmdpkmvhmzftporwzvlbpyplrvrkgrpvqxyjcwwpcinilarbaudodadcglszflkzvmmdahgqphqoueltddqlymwpvweslymviccsmhuezesmlwsswgznaofullpxciuezrbkxfnodiglhpyrfcjajyuqzfoxsysgeqrqmvtqysypgaivfetwhmgxngzdvavbkipanoopqftdmntwpxgaljiqrkppywfuspzfgljheckapftjqar...
output:
8356
result:
ok single line: '8356'
Test #12:
score: 5
Accepted
time: 49ms
memory: 13092kb
input:
200000 nleqrvpkpopfbstnolidsxrfhivbwbbqbwxmvymzmnwozrbklbuwliccquzhpfjsbxlnkzewvjpjarqmwcpsvovxeynjrgdjxrauincbnjvokawliinjqqulwxxeztpbrotihuiyjjqsxxosragocxbpjmhquczgwfakfaojxslteazikjqkebpcrrggswjiyetkpyvjubhtoounhveftykvetuwqgpddicnarbsigpjytmhcymvapbmkforkaeccsuhmwiuljmvajlztzjghnohlmxnmdehfxnra...
output:
8340
result:
ok single line: '8340'
Test #13:
score: 5
Accepted
time: 20ms
memory: 4036kb
input:
200000 abbabbbbbbaababaaabaaaababaaaaaabbababababbabbaaababbbbaabaabbbbbbababaabababbababaabbaabaaaabbbbbaabbbbaababbbabaaababbbbbbbbbbabbbabaabaabaababbbbbbabaabbbbbbbbbbaaabbbbabaabbaababaaabbabbaaabbbbbaaabababaababbaaabbbbababbbbbbaaaabaaaaaaabbbaabbaaaabbabbbbbaabbbbbbbabbaaaabaaabaabbaaaababbb...
output:
52315097
result:
ok single line: '52315097'
Test #14:
score: 5
Accepted
time: 20ms
memory: 4064kb
input:
200000 aaaababaaaaabbabababbbbabbbbababbabbaabaaabbabbaabaaaaaabaaaabbbbbbbabaabbabaabababbbaabbbbabbbbbaabaaaabaaaababaaaabbaaaaabbbabbbabbbbbbbaabbbabbaaabbbabaabaaaaaaaaababbaaabbbbaabaabbaaababbabbaabbbbaaababbbbbbbbbbbaabaaabbaaabaabbaaabbabaabbabbaabbbababbaaabaaaaaabbbbbbbabaabbabbaabaabaabaa...
output:
72221142
result:
ok single line: '72221142'
Test #15:
score: 5
Accepted
time: 22ms
memory: 4844kb
input:
200000 yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy...
output:
2395581278
result:
ok single line: '2395581278'
Test #16:
score: 5
Accepted
time: 22ms
memory: 4892kb
input:
200000 sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss...
output:
2396621459
result:
ok single line: '2396621459'
Test #17:
score: 5
Accepted
time: 22ms
memory: 4848kb
input:
200000 ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo...
output:
2395864064
result:
ok single line: '2395864064'
Test #18:
score: 5
Accepted
time: 236ms
memory: 15280kb
input:
2000000 iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii...
output:
239586659381
result:
ok single line: '239586659381'
Test #19:
score: 5
Accepted
time: 234ms
memory: 15492kb
input:
2000000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
239687303822
result:
ok single line: '239687303822'
Test #20:
score: 5
Accepted
time: 226ms
memory: 15280kb
input:
2000000 vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv...
output:
239608981171
result:
ok single line: '239608981171'