Submission #965536

# Submission time Handle Problem Language Result Execution time Memory
965536 2024-04-18T19:46:58 Z Alkaser_ID Copy and Paste 3 (JOI22_copypaste3) C++17
0 / 100
1696 ms 904 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7, mod2 = 998422353;
#define ml(x,y) ((x*y)%mod)
ll b1 = 53, b2 = 47, p1[3003], p2[3003];
ll inv[3003];

ll a, b, c;
map<ll, ll> dp;
inline ll fsp(ll x, ll b) {
	if (b == 0) return 1;
	if (b == 1) return x;
	ll g = ml(x, x);
	g = fsp(g, b / 2);
	if (b % 2 == 0) return g;
	return (g * x) % mod;
}
inline ll small(vector<ll> k) {
	if (k.size() == 1) return a;
	if (k.size() == 2) {
		ll fr = k[0], sc = (k[1] - k[0] + mod) % mod;
		sc = (sc * inv[1]) % mod;
		if (fr == sc) return min(2 * a, a + b + 2 * c);
		return 2 * a;
	}
	ll fr = k[0], sc = (k[1] - k[0] + mod) % mod, th = (k[2] - k[1] + mod) % mod;
	sc = (sc * inv[1]) % mod;
	th = (th * inv[2]) % mod;
	if (sc != fr && fr != th && sc != th) return 3 * a;
	return min(3 * a, 2 * a + b + 2 * c);
}
inline ll getmin(vector<ll> k) {
	ll th = k[k.size() - 1];
	if (dp.find(th) != dp.end()) return dp[th];
	if (k.size() <= 3) 
		return small(k);
	/*
	ll h = 0;
	for (ll i = 0; i < k.size(); ++i) {
		hash[i] = (p1[i] * (k[i] - 95)) % mod;
		hash[i] = (hash[i] + h) % mod;
		h = hash[i];
	}
	*/
	ll dc = 0, tot = (ll)k.size() * a;
	for (ll ln = 1; ln <= k.size() / 2; ++ln) {
		map<ll, ll> m, d; map<ll, pair<ll, ll>> st;
		for (ll i = 0; i + ln <= k.size(); ++i) {
			ll j = i + ln - 1;
			ll hs = (k[j] - (i > 0 ? k[i - 1] : 0) + mod) % mod;
			hs = (hs * inv[i]) % mod;
			if (m.find(hs) != m.end()) {
				if (i < d[hs]) continue;
			}
			else st[hs] = { i,j };
			d[hs] = i + ln;
			++m[hs];
		}
		for (map<ll, ll>::iterator it = m.begin(); it != m.end(); ++it) {
			vector<ll> nh; pair<ll, ll> se = st[it->first];
			ll bg = se.first;
			for (ll j = se.first; j <= se.second; ++j) {
				ll hvl = (k[j] - (bg == 0 ? 0 : k[bg - 1]) + mod) % mod;
 				hvl = (hvl * inv[bg]) % mod;
				nh.push_back(hvl);
			}
			ll prp = getmin(nh);
			ll tcost = it->second * ln * a - prp - it->second * c - b;
			if (tcost <= 0) continue;
			dc = max(dc, tcost);
		}
	}
	dp[th] = tot - dc;
	return tot - dc;
}
int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll n; cin >> n;
	string s; cin >> s;
	cin >> a >> b >> c;
	p1[0] = p2[0] = 1;
	p1[1] = b1; p2[1] = b2;
	for (ll i = 2; i <= s.size() + 3; ++i) {
		p1[i] = (p1[i - 1] * b1) % mod;
		p2[i] = (p2[i - 1] * b2) % mod2;
	}
	inv[0] = 1;
	for (ll i = 1; i <= s.size() + 3; ++i) {
		inv[i] = fsp(p1[i], mod - 2);
	}
	vector<ll> vhash; ll lst = 0;
	for (ll i = 0; i < s.size(); ++i) {
		ll hv = ((s[i] - 95) * p1[i]) % mod;
		hv = (hv + lst) % mod;
		vhash.push_back(hv);
		lst = hv;
	}
	cout << getmin(vhash);
}

/*
18
aababbbababbbaabbb
1000000000
100000
10000000

*/

Compilation message

copypaste3.cpp: In function 'll getmin(std::vector<long long int>)':
copypaste3.cpp:51:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |  for (ll ln = 1; ln <= k.size() / 2; ++ln) {
      |                  ~~~^~~~~~~~~~~~~~~
copypaste3.cpp:53:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |   for (ll i = 0; i + ln <= k.size(); ++i) {
      |                  ~~~~~~~^~~~~~~~~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:89:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |  for (ll i = 2; i <= s.size() + 3; ++i) {
      |                 ~~^~~~~~~~~~~~~~~
copypaste3.cpp:94:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |  for (ll i = 1; i <= s.size() + 3; ++i) {
      |                 ~~^~~~~~~~~~~~~~~
copypaste3.cpp:98:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |  for (ll i = 0; i < s.size(); ++i) {
      |                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 660 ms 864 KB Output is correct
4 Correct 855 ms 592 KB Output is correct
5 Correct 1067 ms 852 KB Output is correct
6 Correct 1323 ms 876 KB Output is correct
7 Correct 1660 ms 712 KB Output is correct
8 Correct 1682 ms 904 KB Output is correct
9 Correct 1696 ms 660 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 412 KB Output is correct
12 Incorrect 0 ms 348 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -