Submission #965535

# Submission time Handle Problem Language Result Execution time Memory
965535 2024-04-18T19:42:36 Z Alkaser_ID Copy and Paste 3 (JOI22_copypaste3) C++17
0 / 100
3000 ms 604 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;
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) {
	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);
		}
	}
	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:48: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]
   48 |  for (ll ln = 1; ln <= k.size() / 2; ++ln) {
      |                  ~~~^~~~~~~~~~~~~~~
copypaste3.cpp:50: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]
   50 |   for (ll i = 0; i + ln <= k.size(); ++i) {
      |                  ~~~~~~~^~~~~~~~~~~
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:85: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]
   85 |  for (ll i = 2; i <= s.size() + 3; ++i) {
      |                 ~~^~~~~~~~~~~~~~~
copypaste3.cpp:90: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]
   90 |  for (ll i = 1; 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 = 0; i < s.size(); ++i) {
      |                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 604 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 Execution timed out 3076 ms 556 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 604 KB Output isn't correct
2 Halted 0 ms 0 KB -