QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#363584 | #8512. Harmonic Operations | Days_of_Future_Past# | WA | 0ms | 5832kb | C++23 | 3.4kb | 2024-03-24 00:33:13 | 2024-03-24 00:33:14 |
Judging History
answer
#pragma GCC optimize("O3,unroll-loops")
#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
typedef long long LL;
typedef unsigned long long ULL;
#define fi first
#define se second
#define all(x) ((x).begin()), ((x).end())
#define pb push_back
typedef double D;
const int N = 200011;
int tp[N], d[N], nxt[N];
pair<string, int> find(string s) {
int i, j, k, l, n = s.length();
s += s;
for(i = 0, j = 1; j < n; ) {
for(k = 0; k < n && s[i + k] == s[j + k]; k++);
if(k >= n) break;
if(s[i + k] < s[j + k]) {
j += k + 1;
}
else l = i + k, i = j, j = max(l, j) + 1;
}
return make_pair(s.substr(i, n), i);
}
struct Op {
int coe, delta;
void print() const {
printf("c = %d, d = %d\n", coe, delta);
}
};
int per;
Op operator + (const Op & a, const Op & b) {
return Op{a.coe * b.coe, (a.delta * b.coe % per + b.delta + per) % per};
}
bool operator < (const Op & a, const Op & b) {
return a.coe < b.coe || a.coe == b.coe && a.delta < b.delta;
}
int main() {
string s;
cin >> s;
int n = s.size();
int k;
scanf("%d", &k);
for(int i = 0; i < k; i++) {
static char st[11];
scanf("%s", st);
tp[i] = st[0];
if(tp[i] != 'I') {
scanf("%d", &d[i]);
if(tp[i] == 'R') {
d[i] = d[i] % n;
}else {
d[i] = (-d[i] + n) % n;
}
}
}
int revdelta = -1;
string t = s;
reverse(all(t));
pair<string, int> ms = find(s), mt = find(t);
if(ms.first == mt.first) {
revdelta = ms.se - mt.se;
}
s = '$' + s;
nxt[0] = -1;
for(int i = 1; i <= n; i++) {
int p = nxt[i - 1];
while(p != -1 && s[p + 1] != s[i]) {
//printf("p = %d\n", p);
p = nxt[p];
}
nxt[i] = p + 1;
}
per = n - nxt[n];
LL ans = 0;
//printf("per = %d, revdelta = %d\n", per, revdelta);
if(revdelta == -1) {
Op cur{1, 0};
map<Op, int> cnt;
cnt[cur] = 1;
for(int i = 0; i < k; i++) {
Op opi;
if(tp[i] == 'I') {
opi = Op{-1, 0};
} else {
opi = Op{1, d[i] % per};
}
cur = cur + opi;
//cur.print();
ans += cnt[cur];
cnt[cur]++;
}
/*for(int i = 0, j; i < k; i = j) {
if(tp[i] == 'I') {
j = i + 1;
continue;
}
for(j = i; tp[j] != 'I'; j++);
map<int, int> cnt;
cnt[0] = 1;
int cur = 0;
for(int k = i; k < j; k++) {
cur = (cur + d[k]) % per;
ans += cnt[cur];
cnt[cur]++;
}
}*/
} else {
/*if(per == 1) {
ans = (k * (k + 1ll) / 2);
} else */{
map<int, int> cnt;
cnt[0] = 1;
int cur = 0;
for(int i = 0; i < k; i++) {
if(tp[i] == 'I') {
cur = (per - cur + revdelta) % per;
} else {
cur = (cur + d[i]) % per;
}
ans += cnt[cur];
cnt[cur]++;
}
}
}
printf("%lld\n", ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3844kb
input:
pda 2 R 2 L 2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 4084kb
input:
aaa 4 R 1 I I R 1
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
caso 6 L 1 I I R 1 I I
output:
4
result:
ok 1 number(s): "4"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3804kb
input:
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 100 L 12 I R 47 L 54 I I R 80 L 86 L 19 R 5 L 53 L 40 R 20 L 11 R 40 I I R 66 R 6 L 76 L 93 R 39 I I L 24 R 59 R 99 L 52 I I R 77 L 11 R 60 L 16 I L 40 I R 35 L 64 R 11 L 34 I R 35 I L 87 I I L 42 L ...
output:
5050
result:
ok 1 number(s): "5050"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3792kb
input:
wewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewewe 100 R 83 R 34 I I R 87 R 74 L 98 I L 77 L 8 R 23 L 94 I I L 79 L 87 L 47 L 85 L 49 L 7 I I R 97 R 15 I R 66 L 8 R 62 R 68 I I R 32 R 24 R 36 L 60 R 75 R 77 I L 42 I L 61 I I R 78 R 51 L 98 I L 77 I I...
output:
2556
result:
ok 1 number(s): "2556"
Test #6:
score: -100
Wrong Answer
time: 0ms
memory: 5832kb
input:
rtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtrtr 100 R 27 R 68 I L 29 L 51 L 19 L 12 L 10 L 52 L 38 L 17 R 30 L 29 L 51 L 17 R 29 I R 96 R 50 R 56 I I I L 73 L 15 I R 1 R 81 L 94 R 27 R 52 R 57 R 44 I I L 53 I R 87 L 39 L 25 I I R 25 I I I L 88 L ...
output:
2500
result:
wrong answer 1st numbers differ - expected: '116', found: '2500'