Submission #1059789

# Submission time Handle Problem Language Result Execution time Memory
1059789 2024-08-15T08:11:48 Z vjudge1 Copy and Paste 3 (JOI22_copypaste3) C++17
25 / 100
3000 ms 18516 KB
#include <bits/stdc++.h>

using namespace std;
    
typedef long long ll;
const int  N = 2500 + 2;
struct H{
    int p,mod;
    vector<ll> h,inv;
    ll binpow(ll a,ll b) {
        ll ret = 1,k = a;
        while(b) {
            if(b & 1) {
                ret *= k;
                ret %= mod;
            }
            k *= k;
            k%=mod;
            b /= 2;
        }
        return ret;
    }
    void init(string s) {
        int n = (int)s.size();
        h.resize((int)s.size());
        inv.resize((int)s.size());
        ll x = 1;
        for(int i = 0;i < n;i++) {
            h[i] = x * 1ll * (s[i] - 'a' + 1)%mod;
            if(i) h[i] += h[i - 1];
            if(h[i] >= mod) h[i] -= mod;
            inv[i] = binpow(x,mod-2);
            x = x * 1ll * p%mod;
        }
    }
    int get(int l,int r) {
        ll val = h[r];
        if(l) {
            val -= h[l - 1];
        }
        if(val < 0) val += mod;
        val = val * inv[l]%mod;
        return val;
    }

}xx,yy;
string s;
ll a,b,c;
int n;
ll dp[N][N];
int nx[N][N];
map<pair<int,int>,vector<pair<int,int>>> f;
void test() {
    cin >> n >> s >> a >> b >> c;
    yy.p = 37;yy.mod = 998244353;yy.init(s);
    xx.p = 167;xx.mod = (int)1e9 + 7;xx.init(s);
    bool sub = true;
    for(int i = 0;i < n;i++) {
        if(s[i] != 'a') {
            sub = false;
        }
    }
    ll res = n * 1ll * a;
    if(sub) {
        for(int i = 1;i < n;i++) {
            res = min(res,i * 1ll * a + b + a * 1ll * (n % i) + (n / i) *1ll* c);
        }
        cout << res;
        return;
    }
    for(int i = 0;i < n;i++) {
        for(int j = i;j < n;j++) {
            f[{xx.get(i,j),yy.get(i,j)}].push_back({i,j});
        }
    }
    for(auto [t,k]:f) {
        int it = (int)k.size() - 1;
        for(int j = (int)k.size() - 1;j >= 0;j--) {
            while(it >= 0 && k[it].first > k[j].second) {
                it--;
            }
            if(it == (int)k.size() - 1) {
                nx[k[j].first][k[j].second] = -1; 
            } else {
                nx[k[j].first][k[j].second] = k[it + 1].first;
            }
        }
    }
    for(int l = n - 1;l >= 0;l--) {
        dp[l][l] = a;
        for(int r = l + 1;r < n;r++) {
            dp[l][r] = min(dp[l + 1][r],dp[l][r - 1]) + a;
            int len = (r - l + 1) / 2;
            int mx = -1;
            for(;len >= 1;len--) {
                if(xx.get(l,l + len - 1) != xx.get(r - len + 1,r)) continue;
                if(yy.get(l,l + len - 1) != yy.get(r - len + 1,r)) continue;
                int occ = 0;
                int L = l,R = l + len - 1;
                while(true) {
                    occ++;
                    L = nx[L][R];
                    R = L + len - 1;
                    if(L == -1 || R > r) {
                        break;
                    }
                }
                mx = occ;
                int sz = (r - l + 1);
                dp[l][r] = min(dp[l][r],dp[l][l + len - 1] + (sz - len * 1ll * occ) * 1ll * a + (occ - 1) * c + b + c);

            }
            // cout << l << ' ' << r << ' ' << dp[l][r] << '\n';
        }
    }
    cout << dp[0][n-1] << '\n';
}
signed main() {
    ios_base::sync_with_stdio(false);cin.tie(0);
    int t = 1; 
    // cin >> t;
    while(t--) {
        test();
    }
}  

Compilation message

copypaste3.cpp: In function 'void test()':
copypaste3.cpp:94:17: warning: variable 'mx' set but not used [-Wunused-but-set-variable]
   94 |             int mx = -1;
      |                 ^~
# 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 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Incorrect 1 ms 600 KB Output isn't correct
5 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 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 856 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 0 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 432 KB Output is correct
# 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 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 856 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 0 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 432 KB Output is correct
40 Correct 2 ms 1372 KB Output is correct
41 Correct 25 ms 2544 KB Output is correct
42 Correct 12 ms 4188 KB Output is correct
43 Correct 11 ms 4252 KB Output is correct
44 Correct 11 ms 4248 KB Output is correct
45 Correct 11 ms 4444 KB Output is correct
46 Correct 11 ms 4324 KB Output is correct
47 Correct 7 ms 2908 KB Output is correct
48 Correct 9 ms 3540 KB Output is correct
49 Correct 10 ms 3932 KB Output is correct
50 Correct 10 ms 3932 KB Output is correct
51 Correct 9 ms 3420 KB Output is correct
52 Correct 10 ms 3420 KB Output is correct
53 Correct 11 ms 4444 KB Output is correct
# 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 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 604 KB Output is correct
22 Correct 0 ms 604 KB Output is correct
23 Correct 0 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 0 ms 604 KB Output is correct
27 Correct 0 ms 604 KB Output is correct
28 Correct 1 ms 856 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 0 ms 604 KB Output is correct
31 Correct 0 ms 604 KB Output is correct
32 Correct 0 ms 604 KB Output is correct
33 Correct 0 ms 604 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 1 ms 432 KB Output is correct
40 Correct 2 ms 1372 KB Output is correct
41 Correct 25 ms 2544 KB Output is correct
42 Correct 12 ms 4188 KB Output is correct
43 Correct 11 ms 4252 KB Output is correct
44 Correct 11 ms 4248 KB Output is correct
45 Correct 11 ms 4444 KB Output is correct
46 Correct 11 ms 4324 KB Output is correct
47 Correct 7 ms 2908 KB Output is correct
48 Correct 9 ms 3540 KB Output is correct
49 Correct 10 ms 3932 KB Output is correct
50 Correct 10 ms 3932 KB Output is correct
51 Correct 9 ms 3420 KB Output is correct
52 Correct 10 ms 3420 KB Output is correct
53 Correct 11 ms 4444 KB Output is correct
54 Correct 105 ms 15812 KB Output is correct
55 Execution timed out 3069 ms 18516 KB Time limit exceeded
56 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 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Incorrect 1 ms 600 KB Output isn't correct
15 Halted 0 ms 0 KB -