#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const ll base = 1e9 + 9;
const ll MOD = 1000000000000037ll;
const ll INF = 1e18;
const int SIZE = 2505;
int n;
string s;
int A, B, C;
ll dp[SIZE][SIZE];
int nxt[SIZE];
ll pro[SIZE], inv[SIZE], pre[SIZE];
ll power(ll d, ll up) {
ll re = 1;
while (up) {
if (up & 1) re = (__int128) re * d % MOD;
d = (__int128) d * d % MOD;
up >>= 1;
}
return re;
}
ll cal(int l, int r) {
ll x = pre[r] - pre[l - 1];
if (x < 0) x += MOD;
return (__int128) x * inv[l] % MOD;
}
void solve() {
cin >> n >> s;
cin >> A >> B >> C;
s = " " + s;
pro[0] = inv[0] = 1;
inv[1] = power(base, MOD - 2);
FOR (i, 1, n) {
pro[i] = (__int128) pro[i - 1] * base % MOD;
inv[i] = (__int128) inv[i - 1] * inv[1] % MOD;
pre[i] = (pre[i - 1] + (__int128) pro[i] * s[i]) % MOD;
}
FOR (i, 1, n) FOR (j, i, n) dp[i][j] = INF;
FOR (len, 1, n) {
unordered_map<int, int> pos;
unordered_map<int, vector<int>> mp;
for (int l = 1, r = len; r <= n; l++, r++) {
dp[l][r] = min(dp[l][r], min(dp[l][r - 1], dp[l + 1][r]) + A);
int val = cal(l, r);
pos[val] = 0;
mp[val].pb(l);
}
for (int l = 1, r = len; r <= n; l++, r++) {
int val = cal(l, r);
while (pos[val] < mp[val].size() && mp[val][pos[val]] <= r) pos[val]++;
if (pos[val] == mp[val].size()) nxt[l] = n + 1;
else nxt[l] = mp[val][pos[val]];
}
for (int l = 1, r = len; r <= n; l++, r++) {
int pl = l, pr = r, cnt = 1;
while (pr <= n) {
dp[l][pr] = min(dp[l][pr], dp[l][r] + B + 1ll * C * cnt + 1ll * A * (pr - l + 1 - len * cnt));
pl = nxt[pl];
pr = pl + len - 1;
cnt++;
}
}
}
cout << dp[1][n] << '\n';
}
int main() {
Waimai;
solve();
}
Compilation message
copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:72:29: warning: comparison of integer expressions of different signedness: 'std::unordered_map<int, int>::mapped_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | while (pos[val] < mp[val].size() && mp[val][pos[val]] <= r) pos[val]++;
copypaste3.cpp:73:26: warning: comparison of integer expressions of different signedness: 'std::unordered_map<int, int>::mapped_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | if (pos[val] == mp[val].size()) nxt[l] = n + 1;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
478 ms |
20800 KB |
Output is correct |
4 |
Correct |
578 ms |
23620 KB |
Output is correct |
5 |
Correct |
668 ms |
26804 KB |
Output is correct |
6 |
Correct |
774 ms |
30196 KB |
Output is correct |
7 |
Correct |
892 ms |
33892 KB |
Output is correct |
8 |
Correct |
911 ms |
33888 KB |
Output is correct |
9 |
Correct |
905 ms |
33892 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
3 ms |
596 KB |
Output is correct |
41 |
Correct |
6 ms |
1236 KB |
Output is correct |
42 |
Correct |
9 ms |
1328 KB |
Output is correct |
43 |
Correct |
9 ms |
1320 KB |
Output is correct |
44 |
Correct |
9 ms |
1324 KB |
Output is correct |
45 |
Correct |
8 ms |
1320 KB |
Output is correct |
46 |
Correct |
8 ms |
1236 KB |
Output is correct |
47 |
Correct |
8 ms |
1236 KB |
Output is correct |
48 |
Correct |
8 ms |
1236 KB |
Output is correct |
49 |
Correct |
8 ms |
1236 KB |
Output is correct |
50 |
Correct |
9 ms |
1236 KB |
Output is correct |
51 |
Correct |
10 ms |
1304 KB |
Output is correct |
52 |
Correct |
9 ms |
1296 KB |
Output is correct |
53 |
Correct |
9 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
3 ms |
596 KB |
Output is correct |
41 |
Correct |
6 ms |
1236 KB |
Output is correct |
42 |
Correct |
9 ms |
1328 KB |
Output is correct |
43 |
Correct |
9 ms |
1320 KB |
Output is correct |
44 |
Correct |
9 ms |
1324 KB |
Output is correct |
45 |
Correct |
8 ms |
1320 KB |
Output is correct |
46 |
Correct |
8 ms |
1236 KB |
Output is correct |
47 |
Correct |
8 ms |
1236 KB |
Output is correct |
48 |
Correct |
8 ms |
1236 KB |
Output is correct |
49 |
Correct |
8 ms |
1236 KB |
Output is correct |
50 |
Correct |
9 ms |
1236 KB |
Output is correct |
51 |
Correct |
10 ms |
1304 KB |
Output is correct |
52 |
Correct |
9 ms |
1296 KB |
Output is correct |
53 |
Correct |
9 ms |
1236 KB |
Output is correct |
54 |
Correct |
47 ms |
2984 KB |
Output is correct |
55 |
Correct |
127 ms |
8296 KB |
Output is correct |
56 |
Correct |
216 ms |
8444 KB |
Output is correct |
57 |
Correct |
245 ms |
8468 KB |
Output is correct |
58 |
Correct |
218 ms |
8404 KB |
Output is correct |
59 |
Correct |
214 ms |
8440 KB |
Output is correct |
60 |
Correct |
212 ms |
8404 KB |
Output is correct |
61 |
Correct |
179 ms |
8320 KB |
Output is correct |
62 |
Correct |
135 ms |
8300 KB |
Output is correct |
63 |
Correct |
207 ms |
8424 KB |
Output is correct |
64 |
Correct |
199 ms |
8332 KB |
Output is correct |
65 |
Correct |
188 ms |
8456 KB |
Output is correct |
66 |
Correct |
192 ms |
8328 KB |
Output is correct |
67 |
Correct |
213 ms |
8436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
478 ms |
20800 KB |
Output is correct |
14 |
Correct |
578 ms |
23620 KB |
Output is correct |
15 |
Correct |
668 ms |
26804 KB |
Output is correct |
16 |
Correct |
774 ms |
30196 KB |
Output is correct |
17 |
Correct |
892 ms |
33892 KB |
Output is correct |
18 |
Correct |
911 ms |
33888 KB |
Output is correct |
19 |
Correct |
905 ms |
33892 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
0 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
3 ms |
596 KB |
Output is correct |
58 |
Correct |
6 ms |
1236 KB |
Output is correct |
59 |
Correct |
9 ms |
1328 KB |
Output is correct |
60 |
Correct |
9 ms |
1320 KB |
Output is correct |
61 |
Correct |
9 ms |
1324 KB |
Output is correct |
62 |
Correct |
8 ms |
1320 KB |
Output is correct |
63 |
Correct |
8 ms |
1236 KB |
Output is correct |
64 |
Correct |
8 ms |
1236 KB |
Output is correct |
65 |
Correct |
8 ms |
1236 KB |
Output is correct |
66 |
Correct |
8 ms |
1236 KB |
Output is correct |
67 |
Correct |
9 ms |
1236 KB |
Output is correct |
68 |
Correct |
10 ms |
1304 KB |
Output is correct |
69 |
Correct |
9 ms |
1296 KB |
Output is correct |
70 |
Correct |
9 ms |
1236 KB |
Output is correct |
71 |
Correct |
47 ms |
2984 KB |
Output is correct |
72 |
Correct |
127 ms |
8296 KB |
Output is correct |
73 |
Correct |
216 ms |
8444 KB |
Output is correct |
74 |
Correct |
245 ms |
8468 KB |
Output is correct |
75 |
Correct |
218 ms |
8404 KB |
Output is correct |
76 |
Correct |
214 ms |
8440 KB |
Output is correct |
77 |
Correct |
212 ms |
8404 KB |
Output is correct |
78 |
Correct |
179 ms |
8320 KB |
Output is correct |
79 |
Correct |
135 ms |
8300 KB |
Output is correct |
80 |
Correct |
207 ms |
8424 KB |
Output is correct |
81 |
Correct |
199 ms |
8332 KB |
Output is correct |
82 |
Correct |
188 ms |
8456 KB |
Output is correct |
83 |
Correct |
192 ms |
8328 KB |
Output is correct |
84 |
Correct |
213 ms |
8436 KB |
Output is correct |
85 |
Correct |
576 ms |
16820 KB |
Output is correct |
86 |
Correct |
1386 ms |
34276 KB |
Output is correct |
87 |
Correct |
1390 ms |
34264 KB |
Output is correct |
88 |
Correct |
1395 ms |
34268 KB |
Output is correct |
89 |
Correct |
1373 ms |
34260 KB |
Output is correct |
90 |
Correct |
1346 ms |
34332 KB |
Output is correct |
91 |
Correct |
899 ms |
33916 KB |
Output is correct |
92 |
Correct |
1094 ms |
33996 KB |
Output is correct |
93 |
Correct |
1295 ms |
34212 KB |
Output is correct |
94 |
Correct |
1322 ms |
34348 KB |
Output is correct |
95 |
Correct |
1290 ms |
33948 KB |
Output is correct |
96 |
Correct |
1270 ms |
34064 KB |
Output is correct |
97 |
Correct |
1361 ms |
34284 KB |
Output is correct |