Submission #565337

# Submission time Handle Problem Language Result Execution time Memory
565337 2022-05-20T17:26:43 Z Zanite Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
832 ms 99692 KB
#include <bits/stdc++.h>
using namespace std;

using ll	= long long;
using i8	= __int128;
using pll	= pair<ll, ll>;
using pi8	= pair<i8, i8>;

#define fi	first
#define se	second

const i8 mod	= 1850294015875828717;

const ll maxN	= 2515;
const ll maxNr	= 2510;
const ll INF	= 1e18;

void maddto(i8 &x, i8 y) {x += y; x %= mod;}
i8 msub(i8 x, i8 y) {x -= y; while (x < 0) x += mod; x %= mod; return x;}
i8 mmul(i8 x, i8 y) {x *= y; x %= mod; return x;}

void chmin(ll &x, ll y) {x = min(x, y);}

i8 modexp(i8 x, i8 y) {
	if (!x) return 0; if (!y) return 1;
	i8 t = modexp(x, y >> 1);
	return mmul(t, mmul(t, (y & 1) ? x : 1));
}

ll A, B, C, N;
char S[maxN];

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<ll> uid(1e5, 2e5);
i8 key = uid(rng), invkey = modexp(key, mod-2);
i8 kpow[maxN], invkpow[maxN], hsh[maxN];

ll dp[maxN][maxN], nxt[maxN][maxN];

void precomp() {
	key = rng();
	invkey = modexp(key, mod-2);
	kpow[0] = invkpow[0] = 1;
	for (ll i = 1; i <= maxNr; i++) {
		kpow[i] = mmul(kpow[i-1], key);
		invkpow[i] = mmul(invkpow[i-1], invkey);
	}
}

void hashS() {
	for (ll i = 1; i <= N; i++) {
		hsh[i] = mmul(S[i], kpow[i]);
		maddto(hsh[i], hsh[i-1]);
	}
}

i8 getHash(ll l, ll r) {
	i8 ret = msub(hsh[r], hsh[l-1]);
	return mmul(ret, invkpow[l-1]);
}

int main() {
	precomp();

	scanf("%lld", &N);
	S[0] = '#';
	scanf("%s", S + 1);
	scanf("%lld %lld %lld", &A, &B, &C);

	hashS();

	// compute nxt[l][r] for each possible substring length
	memset(nxt, -1, sizeof(nxt));
	for (ll len = 1; len < N; len++) {
		vector<pi8> hashes = {{-1, -1}};
		for (ll st = 1; st <= N-len+1; st++) {
			hashes.push_back({getHash(st, st+len-1), st});
		}
		sort(hashes.begin(), hashes.end());
		/*
		for (auto H : hashes) {
			printf("{%lld, %lld} ", H.fi, H.se);
		}
		cout << '\n';
		*/

		deque<ll> pending;
		for (ll i = 1; i < hashes.size(); i++) {
			if (hashes[i].fi != hashes[i-1].fi) {
				pending.clear();
			}
			while (!pending.empty()) {
				if (pending.front() <= hashes[i].se - len) {
					nxt[pending.front()][pending.front()+len-1] = hashes[i].se;
					pending.pop_front();
				} else break;
			}
			pending.push_back(hashes[i].se);
		}
	}

	/*
	for (ll l = 1; l <= N; l++) {
		for (ll r = l; r <= N; r++) {
			printf("nxt[%lld][%lld] = %lld\n", l, r, nxt[l][r]);
		}
	}
	*/

	// Calculate DP
	for (auto &i : dp) {
		for (auto &j : i) {
			j = INF;
		}
	}
	// Base case
	for (ll i = 1; i <= N; i++) {
		dp[i][i] = A + B;
	}
	// Transition
	for (ll len = 1; len < N; len++) {
		for (ll l = 1; l <= N-len+1; l++) {
			ll r = l+len-1;

			// Type S[l-1]
			if (l > 1) {
				chmin(dp[l-1][r], dp[l][r] + A);
			}

			// Type S[r+1]
			if (r < N) {
				chmin(dp[l][r+1], dp[l][r] + A);
			}

			// Paste next occurences of S[l..r]
			ll cur = l, pastes = 1;
			while (nxt[cur][cur+len-1] != -1) {
				cur = nxt[cur][cur+len-1];
				pastes++;
				chmin(
					dp[l][cur+len-1],
					dp[l][r] + 1ll * pastes * C + 1ll * (cur+len-l - 1ll * len * pastes) * A + B
				);
			}
		}
	}

	printf("%lld\n", dp[1][N] - B);
}

Compilation message

copypaste3.cpp: In function 'i8 modexp(i8, i8)':
copypaste3.cpp:25:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   25 |  if (!x) return 0; if (!y) return 1;
      |  ^~
copypaste3.cpp:25:20: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   25 |  if (!x) return 0; if (!y) return 1;
      |                    ^~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:88:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<__int128, __int128> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |   for (ll i = 1; i < hashes.size(); i++) {
      |                  ~~^~~~~~~~~~~~~~~
copypaste3.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   65 |  scanf("%lld", &N);
      |  ~~~~~^~~~~~~~~~~~
copypaste3.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |  scanf("%s", S + 1);
      |  ~~~~~^~~~~~~~~~~~~
copypaste3.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |  scanf("%lld %lld %lld", &A, &B, &C);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 40 ms 99352 KB Output is correct
2 Correct 40 ms 99336 KB Output is correct
3 Correct 41 ms 99412 KB Output is correct
4 Correct 41 ms 99288 KB Output is correct
5 Correct 41 ms 99328 KB Output is correct
6 Correct 43 ms 99384 KB Output is correct
7 Correct 48 ms 99300 KB Output is correct
8 Correct 38 ms 99288 KB Output is correct
9 Correct 43 ms 99328 KB Output is correct
10 Correct 43 ms 99352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 99296 KB Output is correct
2 Correct 42 ms 99276 KB Output is correct
3 Correct 375 ms 99480 KB Output is correct
4 Correct 475 ms 99460 KB Output is correct
5 Correct 569 ms 99592 KB Output is correct
6 Correct 669 ms 99592 KB Output is correct
7 Correct 798 ms 99488 KB Output is correct
8 Correct 829 ms 99608 KB Output is correct
9 Correct 825 ms 99616 KB Output is correct
10 Correct 43 ms 99276 KB Output is correct
11 Correct 42 ms 99404 KB Output is correct
12 Correct 44 ms 99324 KB Output is correct
13 Correct 39 ms 99336 KB Output is correct
14 Correct 41 ms 99404 KB Output is correct
15 Correct 40 ms 99340 KB Output is correct
16 Correct 40 ms 99328 KB Output is correct
17 Correct 42 ms 99408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 99352 KB Output is correct
2 Correct 40 ms 99336 KB Output is correct
3 Correct 41 ms 99412 KB Output is correct
4 Correct 41 ms 99288 KB Output is correct
5 Correct 41 ms 99328 KB Output is correct
6 Correct 43 ms 99384 KB Output is correct
7 Correct 48 ms 99300 KB Output is correct
8 Correct 38 ms 99288 KB Output is correct
9 Correct 43 ms 99328 KB Output is correct
10 Correct 43 ms 99352 KB Output is correct
11 Correct 42 ms 99348 KB Output is correct
12 Correct 46 ms 99380 KB Output is correct
13 Correct 44 ms 99496 KB Output is correct
14 Correct 42 ms 99404 KB Output is correct
15 Correct 42 ms 99376 KB Output is correct
16 Correct 41 ms 99308 KB Output is correct
17 Correct 49 ms 99396 KB Output is correct
18 Correct 47 ms 99316 KB Output is correct
19 Correct 54 ms 99332 KB Output is correct
20 Correct 41 ms 99396 KB Output is correct
21 Correct 59 ms 99376 KB Output is correct
22 Correct 41 ms 99404 KB Output is correct
23 Correct 45 ms 99372 KB Output is correct
24 Correct 45 ms 99344 KB Output is correct
25 Correct 43 ms 99344 KB Output is correct
26 Correct 46 ms 99296 KB Output is correct
27 Correct 40 ms 99376 KB Output is correct
28 Correct 41 ms 99304 KB Output is correct
29 Correct 41 ms 99336 KB Output is correct
30 Correct 43 ms 99296 KB Output is correct
31 Correct 48 ms 99368 KB Output is correct
32 Correct 52 ms 99404 KB Output is correct
33 Correct 41 ms 99352 KB Output is correct
34 Correct 42 ms 99276 KB Output is correct
35 Correct 44 ms 99292 KB Output is correct
36 Correct 41 ms 99296 KB Output is correct
37 Correct 42 ms 99328 KB Output is correct
38 Correct 42 ms 99328 KB Output is correct
39 Correct 40 ms 99404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 99352 KB Output is correct
2 Correct 40 ms 99336 KB Output is correct
3 Correct 41 ms 99412 KB Output is correct
4 Correct 41 ms 99288 KB Output is correct
5 Correct 41 ms 99328 KB Output is correct
6 Correct 43 ms 99384 KB Output is correct
7 Correct 48 ms 99300 KB Output is correct
8 Correct 38 ms 99288 KB Output is correct
9 Correct 43 ms 99328 KB Output is correct
10 Correct 43 ms 99352 KB Output is correct
11 Correct 42 ms 99348 KB Output is correct
12 Correct 46 ms 99380 KB Output is correct
13 Correct 44 ms 99496 KB Output is correct
14 Correct 42 ms 99404 KB Output is correct
15 Correct 42 ms 99376 KB Output is correct
16 Correct 41 ms 99308 KB Output is correct
17 Correct 49 ms 99396 KB Output is correct
18 Correct 47 ms 99316 KB Output is correct
19 Correct 54 ms 99332 KB Output is correct
20 Correct 41 ms 99396 KB Output is correct
21 Correct 59 ms 99376 KB Output is correct
22 Correct 41 ms 99404 KB Output is correct
23 Correct 45 ms 99372 KB Output is correct
24 Correct 45 ms 99344 KB Output is correct
25 Correct 43 ms 99344 KB Output is correct
26 Correct 46 ms 99296 KB Output is correct
27 Correct 40 ms 99376 KB Output is correct
28 Correct 41 ms 99304 KB Output is correct
29 Correct 41 ms 99336 KB Output is correct
30 Correct 43 ms 99296 KB Output is correct
31 Correct 48 ms 99368 KB Output is correct
32 Correct 52 ms 99404 KB Output is correct
33 Correct 41 ms 99352 KB Output is correct
34 Correct 42 ms 99276 KB Output is correct
35 Correct 44 ms 99292 KB Output is correct
36 Correct 41 ms 99296 KB Output is correct
37 Correct 42 ms 99328 KB Output is correct
38 Correct 42 ms 99328 KB Output is correct
39 Correct 40 ms 99404 KB Output is correct
40 Correct 44 ms 99292 KB Output is correct
41 Correct 52 ms 99380 KB Output is correct
42 Correct 52 ms 99344 KB Output is correct
43 Correct 48 ms 99424 KB Output is correct
44 Correct 47 ms 99404 KB Output is correct
45 Correct 44 ms 99312 KB Output is correct
46 Correct 43 ms 99296 KB Output is correct
47 Correct 45 ms 99404 KB Output is correct
48 Correct 43 ms 99516 KB Output is correct
49 Correct 43 ms 99392 KB Output is correct
50 Correct 49 ms 99404 KB Output is correct
51 Correct 48 ms 99384 KB Output is correct
52 Correct 48 ms 99320 KB Output is correct
53 Correct 43 ms 99328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 99352 KB Output is correct
2 Correct 40 ms 99336 KB Output is correct
3 Correct 41 ms 99412 KB Output is correct
4 Correct 41 ms 99288 KB Output is correct
5 Correct 41 ms 99328 KB Output is correct
6 Correct 43 ms 99384 KB Output is correct
7 Correct 48 ms 99300 KB Output is correct
8 Correct 38 ms 99288 KB Output is correct
9 Correct 43 ms 99328 KB Output is correct
10 Correct 43 ms 99352 KB Output is correct
11 Correct 42 ms 99348 KB Output is correct
12 Correct 46 ms 99380 KB Output is correct
13 Correct 44 ms 99496 KB Output is correct
14 Correct 42 ms 99404 KB Output is correct
15 Correct 42 ms 99376 KB Output is correct
16 Correct 41 ms 99308 KB Output is correct
17 Correct 49 ms 99396 KB Output is correct
18 Correct 47 ms 99316 KB Output is correct
19 Correct 54 ms 99332 KB Output is correct
20 Correct 41 ms 99396 KB Output is correct
21 Correct 59 ms 99376 KB Output is correct
22 Correct 41 ms 99404 KB Output is correct
23 Correct 45 ms 99372 KB Output is correct
24 Correct 45 ms 99344 KB Output is correct
25 Correct 43 ms 99344 KB Output is correct
26 Correct 46 ms 99296 KB Output is correct
27 Correct 40 ms 99376 KB Output is correct
28 Correct 41 ms 99304 KB Output is correct
29 Correct 41 ms 99336 KB Output is correct
30 Correct 43 ms 99296 KB Output is correct
31 Correct 48 ms 99368 KB Output is correct
32 Correct 52 ms 99404 KB Output is correct
33 Correct 41 ms 99352 KB Output is correct
34 Correct 42 ms 99276 KB Output is correct
35 Correct 44 ms 99292 KB Output is correct
36 Correct 41 ms 99296 KB Output is correct
37 Correct 42 ms 99328 KB Output is correct
38 Correct 42 ms 99328 KB Output is correct
39 Correct 40 ms 99404 KB Output is correct
40 Correct 44 ms 99292 KB Output is correct
41 Correct 52 ms 99380 KB Output is correct
42 Correct 52 ms 99344 KB Output is correct
43 Correct 48 ms 99424 KB Output is correct
44 Correct 47 ms 99404 KB Output is correct
45 Correct 44 ms 99312 KB Output is correct
46 Correct 43 ms 99296 KB Output is correct
47 Correct 45 ms 99404 KB Output is correct
48 Correct 43 ms 99516 KB Output is correct
49 Correct 43 ms 99392 KB Output is correct
50 Correct 49 ms 99404 KB Output is correct
51 Correct 48 ms 99384 KB Output is correct
52 Correct 48 ms 99320 KB Output is correct
53 Correct 43 ms 99328 KB Output is correct
54 Correct 48 ms 99340 KB Output is correct
55 Correct 108 ms 99396 KB Output is correct
56 Correct 93 ms 99504 KB Output is correct
57 Correct 90 ms 99384 KB Output is correct
58 Correct 94 ms 99396 KB Output is correct
59 Correct 91 ms 99380 KB Output is correct
60 Correct 106 ms 99384 KB Output is correct
61 Correct 108 ms 99532 KB Output is correct
62 Correct 115 ms 99504 KB Output is correct
63 Correct 96 ms 99516 KB Output is correct
64 Correct 117 ms 99404 KB Output is correct
65 Correct 126 ms 99476 KB Output is correct
66 Correct 109 ms 99380 KB Output is correct
67 Correct 92 ms 99392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 99352 KB Output is correct
2 Correct 40 ms 99336 KB Output is correct
3 Correct 41 ms 99412 KB Output is correct
4 Correct 41 ms 99288 KB Output is correct
5 Correct 41 ms 99328 KB Output is correct
6 Correct 43 ms 99384 KB Output is correct
7 Correct 48 ms 99300 KB Output is correct
8 Correct 38 ms 99288 KB Output is correct
9 Correct 43 ms 99328 KB Output is correct
10 Correct 43 ms 99352 KB Output is correct
11 Correct 42 ms 99296 KB Output is correct
12 Correct 42 ms 99276 KB Output is correct
13 Correct 375 ms 99480 KB Output is correct
14 Correct 475 ms 99460 KB Output is correct
15 Correct 569 ms 99592 KB Output is correct
16 Correct 669 ms 99592 KB Output is correct
17 Correct 798 ms 99488 KB Output is correct
18 Correct 829 ms 99608 KB Output is correct
19 Correct 825 ms 99616 KB Output is correct
20 Correct 43 ms 99276 KB Output is correct
21 Correct 42 ms 99404 KB Output is correct
22 Correct 44 ms 99324 KB Output is correct
23 Correct 39 ms 99336 KB Output is correct
24 Correct 41 ms 99404 KB Output is correct
25 Correct 40 ms 99340 KB Output is correct
26 Correct 40 ms 99328 KB Output is correct
27 Correct 42 ms 99408 KB Output is correct
28 Correct 42 ms 99348 KB Output is correct
29 Correct 46 ms 99380 KB Output is correct
30 Correct 44 ms 99496 KB Output is correct
31 Correct 42 ms 99404 KB Output is correct
32 Correct 42 ms 99376 KB Output is correct
33 Correct 41 ms 99308 KB Output is correct
34 Correct 49 ms 99396 KB Output is correct
35 Correct 47 ms 99316 KB Output is correct
36 Correct 54 ms 99332 KB Output is correct
37 Correct 41 ms 99396 KB Output is correct
38 Correct 59 ms 99376 KB Output is correct
39 Correct 41 ms 99404 KB Output is correct
40 Correct 45 ms 99372 KB Output is correct
41 Correct 45 ms 99344 KB Output is correct
42 Correct 43 ms 99344 KB Output is correct
43 Correct 46 ms 99296 KB Output is correct
44 Correct 40 ms 99376 KB Output is correct
45 Correct 41 ms 99304 KB Output is correct
46 Correct 41 ms 99336 KB Output is correct
47 Correct 43 ms 99296 KB Output is correct
48 Correct 48 ms 99368 KB Output is correct
49 Correct 52 ms 99404 KB Output is correct
50 Correct 41 ms 99352 KB Output is correct
51 Correct 42 ms 99276 KB Output is correct
52 Correct 44 ms 99292 KB Output is correct
53 Correct 41 ms 99296 KB Output is correct
54 Correct 42 ms 99328 KB Output is correct
55 Correct 42 ms 99328 KB Output is correct
56 Correct 40 ms 99404 KB Output is correct
57 Correct 44 ms 99292 KB Output is correct
58 Correct 52 ms 99380 KB Output is correct
59 Correct 52 ms 99344 KB Output is correct
60 Correct 48 ms 99424 KB Output is correct
61 Correct 47 ms 99404 KB Output is correct
62 Correct 44 ms 99312 KB Output is correct
63 Correct 43 ms 99296 KB Output is correct
64 Correct 45 ms 99404 KB Output is correct
65 Correct 43 ms 99516 KB Output is correct
66 Correct 43 ms 99392 KB Output is correct
67 Correct 49 ms 99404 KB Output is correct
68 Correct 48 ms 99384 KB Output is correct
69 Correct 48 ms 99320 KB Output is correct
70 Correct 43 ms 99328 KB Output is correct
71 Correct 48 ms 99340 KB Output is correct
72 Correct 108 ms 99396 KB Output is correct
73 Correct 93 ms 99504 KB Output is correct
74 Correct 90 ms 99384 KB Output is correct
75 Correct 94 ms 99396 KB Output is correct
76 Correct 91 ms 99380 KB Output is correct
77 Correct 106 ms 99384 KB Output is correct
78 Correct 108 ms 99532 KB Output is correct
79 Correct 115 ms 99504 KB Output is correct
80 Correct 96 ms 99516 KB Output is correct
81 Correct 117 ms 99404 KB Output is correct
82 Correct 126 ms 99476 KB Output is correct
83 Correct 109 ms 99380 KB Output is correct
84 Correct 92 ms 99392 KB Output is correct
85 Correct 196 ms 99444 KB Output is correct
86 Correct 410 ms 99584 KB Output is correct
87 Correct 436 ms 99604 KB Output is correct
88 Correct 375 ms 99536 KB Output is correct
89 Correct 401 ms 99692 KB Output is correct
90 Correct 368 ms 99532 KB Output is correct
91 Correct 693 ms 99560 KB Output is correct
92 Correct 832 ms 99568 KB Output is correct
93 Correct 472 ms 99512 KB Output is correct
94 Correct 466 ms 99568 KB Output is correct
95 Correct 686 ms 99464 KB Output is correct
96 Correct 707 ms 99604 KB Output is correct
97 Correct 381 ms 99564 KB Output is correct