답안 #585772

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
585772 2022-06-29T10:25:31 Z Drew_ Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
778 ms 72884 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ll long long
#define ii pair<int, int>
#define f1 first
#define s2 second

const int prime = 177013;
const int mod1 = 1e9 + 9;
const int mod2 = 420691273;
const int MAX = 2569;
const ll inf = 1e18 + 69;

template<int KEY1 = mod1, int KEY2 = mod2>
struct Hash
{
    template<int MOD>
    struct Mint
    {
        int val;

        Mint() : val(0) {}
        Mint(int64_t _val) : val((int)(_val % MOD)) { if (val < 0) val += MOD; }

        Mint& operator+= (const Mint &rhs) { val += rhs.val; if (val >= MOD) val -= MOD; return *this; }
        Mint& operator-= (const Mint &rhs) { val -= rhs.val; if (val < 0) val += MOD; return *this; }
        Mint& operator*= (const Mint &rhs) { val = (int)(1ll * val * rhs.val % MOD); return *this; }

        friend Mint fpow(Mint x, int64_t y)
        {
            Mint res = 1;
            for (; y > 0; y >>= 1, x *= x)
            {
                if (y & 1)
                    res *= x;
            }
            return res;
        }
        friend Mint inverse(Mint x) { return fpow(x, MOD-2); }
        Mint& operator/= (const Mint &rhs) { return *this *= inverse(rhs); }

        friend Mint operator+ (Mint a, const Mint &b) { return a += b; }
        friend Mint operator- (Mint a, const Mint &b) { return a -= b; }
        friend Mint operator- (Mint a) { return 0 - a; }
        friend Mint operator* (Mint a, const Mint &b) { return a *= b; }
        friend Mint operator/ (Mint a, const Mint &b) { return a /= b; }
        friend ostream& operator<< (ostream &os, const Mint &a) { return os << a.val; }
        friend bool operator== (const Mint &a, const Mint &b) { return a.val == b.val; }
        friend bool operator!= (const Mint &a, const Mint &b) { return a.val != b.val; }
    };

    Mint<KEY1> val1;
    Mint<KEY2> val2;

    Hash() : val1(0), val2(0) {}
    Hash(int64_t _val) : val1(_val), val2(_val) {}

    Hash& operator+= (const Hash &rhs) { val1 += rhs.val1; val2 += rhs.val2; return *this; }
    Hash& operator-= (const Hash &rhs) { val1 -= rhs.val1; val2 -= rhs.val2; return *this; }
    Hash& operator*= (const Hash &rhs) { val1 *= rhs.val1; val2 *= rhs.val2; return *this; }
    Hash& operator/= (const Hash &rhs) { val1 /= rhs.val1; val2 /= rhs.val2; return *this; }

    friend Hash operator+ (Hash a, const Hash &b) { return a += b; }
    friend Hash operator- (Hash a, const Hash &b) { return a -= b; }
    friend Hash operator- (Hash a) { return 0 - a; }
    friend Hash operator* (Hash a, const Hash &b) { return a *= b; }
    friend Hash operator/ (Hash a, const Hash &b) { return a /= b; }
    friend ostream& operator<< (ostream &os, const Hash &a) { return os << a.val1 << " " << a.val2; }
    friend bool operator== (const Hash &a, const Hash &b) { return a.val1 == b.val1 && a.val2 == b.val2; }
    friend bool operator!= (const Hash &a, const Hash &b) { return a.val1 != b.val1 || a.val2 != b.val2; }
};


ll A, B, C;
int N;
char s[MAX];

Hash<> pwr[MAX], inv[MAX], pfx[MAX];

int nxt[MAX][MAX]; // nxt[l][r]: smallest `x` such that x >= r and s.substr(l, r-l) = s.substr(x, r-l)
ll dp[MAX][MAX]; // dp[l][r]: lowest cost to make x == "" and y == s.substr(l, r-l) 

int main()
{
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	pwr[0] = 1;
	for (int i = 1; i < MAX; ++i)
		pwr[i] = prime * pwr[i-1];
	inv[MAX-1] = 1 / (pwr[MAX-1]);
	for (int i = MAX-2; i >= 0; --i)
		inv[i] = prime * inv[i+1];

	for (auto &x : dp)
		for (auto &y : x)
			y = inf;

	cin >> N >> s >> A >> B >> C;

	pfx[0] = s[0];
	for (int i = 1; i < N; ++i)
		pfx[i] = pfx[i-1] + s[i] * pwr[i];

	const auto getSum = [&](int l, int r) -> ii
	{
		Hash<> tmp = (pfx[r] - (l ? pfx[l-1] : 0)) * inv[l];
		return { tmp.val1.val, tmp.val2.val }; 
	};

	for (int len = 1; len <= N; ++len)
	{
		map<ii, vector<int>> memo;
		for (int l = 0; l+len <= N; ++l)
			memo[getSum(l, l+len-1)].pb(l);

		for (const auto &[x, v] : memo)
		{
			// cerr << "HAHA\n";
			for (int i = 0, j = 0, sz = (int)v.size(); i < sz; ++i)
			{
				while (j < sz && v[i]+len > v[j]) ++j;
				nxt[v[i]][v[i] + len] = (j == sz ? N : v[j]);
			}
		}
	}

	for (int i = 0; i < N; ++i)
		dp[i][i+1] = A + B;

	for (int len = 1; len < N; ++len)
	{
		for (int l = 0; l+len <= N; ++l)
		{
			int r = l + len;
			if (l) dp[l-1][r] = min(dp[l-1][r], dp[l][r] + A);
			if (r+1 <= N) dp[l][r+1] = min(dp[l][r+1], dp[l][r] + A);

			for(int cur = l, ctr = 1; nxt[cur][cur + len] < N;)
			{
				cur = nxt[cur][cur + len];
				ctr++;
				dp[l][cur + len] = min(dp[l][cur + len], dp[l][r] + C*ctr + A*(cur+len - l - ctr*len) + B);
			}
		}
	}


	cout << dp[0][N] - B << '\n';
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 52052 KB Output is correct
2 Correct 25 ms 52100 KB Output is correct
3 Correct 23 ms 51956 KB Output is correct
4 Correct 22 ms 52052 KB Output is correct
5 Correct 21 ms 51952 KB Output is correct
6 Correct 21 ms 52052 KB Output is correct
7 Correct 21 ms 51916 KB Output is correct
8 Correct 22 ms 52056 KB Output is correct
9 Correct 22 ms 51964 KB Output is correct
10 Correct 22 ms 52004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 52040 KB Output is correct
2 Correct 23 ms 52088 KB Output is correct
3 Correct 345 ms 65668 KB Output is correct
4 Correct 377 ms 67156 KB Output is correct
5 Correct 445 ms 68900 KB Output is correct
6 Correct 522 ms 70728 KB Output is correct
7 Correct 616 ms 72544 KB Output is correct
8 Correct 627 ms 72548 KB Output is correct
9 Correct 612 ms 72656 KB Output is correct
10 Correct 23 ms 51944 KB Output is correct
11 Correct 22 ms 52032 KB Output is correct
12 Correct 21 ms 52012 KB Output is correct
13 Correct 21 ms 52052 KB Output is correct
14 Correct 22 ms 52060 KB Output is correct
15 Correct 23 ms 52092 KB Output is correct
16 Correct 22 ms 52052 KB Output is correct
17 Correct 24 ms 52084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 52052 KB Output is correct
2 Correct 25 ms 52100 KB Output is correct
3 Correct 23 ms 51956 KB Output is correct
4 Correct 22 ms 52052 KB Output is correct
5 Correct 21 ms 51952 KB Output is correct
6 Correct 21 ms 52052 KB Output is correct
7 Correct 21 ms 51916 KB Output is correct
8 Correct 22 ms 52056 KB Output is correct
9 Correct 22 ms 51964 KB Output is correct
10 Correct 22 ms 52004 KB Output is correct
11 Correct 24 ms 52080 KB Output is correct
12 Correct 28 ms 51984 KB Output is correct
13 Correct 21 ms 52052 KB Output is correct
14 Correct 22 ms 52056 KB Output is correct
15 Correct 21 ms 52052 KB Output is correct
16 Correct 24 ms 52072 KB Output is correct
17 Correct 22 ms 52052 KB Output is correct
18 Correct 23 ms 51924 KB Output is correct
19 Correct 27 ms 52032 KB Output is correct
20 Correct 27 ms 52052 KB Output is correct
21 Correct 26 ms 52132 KB Output is correct
22 Correct 22 ms 52044 KB Output is correct
23 Correct 22 ms 52056 KB Output is correct
24 Correct 26 ms 52084 KB Output is correct
25 Correct 23 ms 52172 KB Output is correct
26 Correct 23 ms 52088 KB Output is correct
27 Correct 23 ms 52052 KB Output is correct
28 Correct 26 ms 52032 KB Output is correct
29 Correct 23 ms 52048 KB Output is correct
30 Correct 22 ms 52052 KB Output is correct
31 Correct 22 ms 52056 KB Output is correct
32 Correct 22 ms 52056 KB Output is correct
33 Correct 22 ms 52052 KB Output is correct
34 Correct 22 ms 52052 KB Output is correct
35 Correct 22 ms 52028 KB Output is correct
36 Correct 22 ms 51952 KB Output is correct
37 Correct 24 ms 52004 KB Output is correct
38 Correct 22 ms 52000 KB Output is correct
39 Correct 23 ms 52060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 52052 KB Output is correct
2 Correct 25 ms 52100 KB Output is correct
3 Correct 23 ms 51956 KB Output is correct
4 Correct 22 ms 52052 KB Output is correct
5 Correct 21 ms 51952 KB Output is correct
6 Correct 21 ms 52052 KB Output is correct
7 Correct 21 ms 51916 KB Output is correct
8 Correct 22 ms 52056 KB Output is correct
9 Correct 22 ms 51964 KB Output is correct
10 Correct 22 ms 52004 KB Output is correct
11 Correct 24 ms 52080 KB Output is correct
12 Correct 28 ms 51984 KB Output is correct
13 Correct 21 ms 52052 KB Output is correct
14 Correct 22 ms 52056 KB Output is correct
15 Correct 21 ms 52052 KB Output is correct
16 Correct 24 ms 52072 KB Output is correct
17 Correct 22 ms 52052 KB Output is correct
18 Correct 23 ms 51924 KB Output is correct
19 Correct 27 ms 52032 KB Output is correct
20 Correct 27 ms 52052 KB Output is correct
21 Correct 26 ms 52132 KB Output is correct
22 Correct 22 ms 52044 KB Output is correct
23 Correct 22 ms 52056 KB Output is correct
24 Correct 26 ms 52084 KB Output is correct
25 Correct 23 ms 52172 KB Output is correct
26 Correct 23 ms 52088 KB Output is correct
27 Correct 23 ms 52052 KB Output is correct
28 Correct 26 ms 52032 KB Output is correct
29 Correct 23 ms 52048 KB Output is correct
30 Correct 22 ms 52052 KB Output is correct
31 Correct 22 ms 52056 KB Output is correct
32 Correct 22 ms 52056 KB Output is correct
33 Correct 22 ms 52052 KB Output is correct
34 Correct 22 ms 52052 KB Output is correct
35 Correct 22 ms 52028 KB Output is correct
36 Correct 22 ms 51952 KB Output is correct
37 Correct 24 ms 52004 KB Output is correct
38 Correct 22 ms 52000 KB Output is correct
39 Correct 23 ms 52060 KB Output is correct
40 Correct 21 ms 52388 KB Output is correct
41 Correct 23 ms 52908 KB Output is correct
42 Correct 26 ms 52932 KB Output is correct
43 Correct 29 ms 52948 KB Output is correct
44 Correct 26 ms 52948 KB Output is correct
45 Correct 26 ms 52928 KB Output is correct
46 Correct 26 ms 52852 KB Output is correct
47 Correct 31 ms 52848 KB Output is correct
48 Correct 25 ms 52832 KB Output is correct
49 Correct 25 ms 52820 KB Output is correct
50 Correct 25 ms 52884 KB Output is correct
51 Correct 25 ms 52820 KB Output is correct
52 Correct 25 ms 52820 KB Output is correct
53 Correct 26 ms 52928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 52052 KB Output is correct
2 Correct 25 ms 52100 KB Output is correct
3 Correct 23 ms 51956 KB Output is correct
4 Correct 22 ms 52052 KB Output is correct
5 Correct 21 ms 51952 KB Output is correct
6 Correct 21 ms 52052 KB Output is correct
7 Correct 21 ms 51916 KB Output is correct
8 Correct 22 ms 52056 KB Output is correct
9 Correct 22 ms 51964 KB Output is correct
10 Correct 22 ms 52004 KB Output is correct
11 Correct 24 ms 52080 KB Output is correct
12 Correct 28 ms 51984 KB Output is correct
13 Correct 21 ms 52052 KB Output is correct
14 Correct 22 ms 52056 KB Output is correct
15 Correct 21 ms 52052 KB Output is correct
16 Correct 24 ms 52072 KB Output is correct
17 Correct 22 ms 52052 KB Output is correct
18 Correct 23 ms 51924 KB Output is correct
19 Correct 27 ms 52032 KB Output is correct
20 Correct 27 ms 52052 KB Output is correct
21 Correct 26 ms 52132 KB Output is correct
22 Correct 22 ms 52044 KB Output is correct
23 Correct 22 ms 52056 KB Output is correct
24 Correct 26 ms 52084 KB Output is correct
25 Correct 23 ms 52172 KB Output is correct
26 Correct 23 ms 52088 KB Output is correct
27 Correct 23 ms 52052 KB Output is correct
28 Correct 26 ms 52032 KB Output is correct
29 Correct 23 ms 52048 KB Output is correct
30 Correct 22 ms 52052 KB Output is correct
31 Correct 22 ms 52056 KB Output is correct
32 Correct 22 ms 52056 KB Output is correct
33 Correct 22 ms 52052 KB Output is correct
34 Correct 22 ms 52052 KB Output is correct
35 Correct 22 ms 52028 KB Output is correct
36 Correct 22 ms 51952 KB Output is correct
37 Correct 24 ms 52004 KB Output is correct
38 Correct 22 ms 52000 KB Output is correct
39 Correct 23 ms 52060 KB Output is correct
40 Correct 21 ms 52388 KB Output is correct
41 Correct 23 ms 52908 KB Output is correct
42 Correct 26 ms 52932 KB Output is correct
43 Correct 29 ms 52948 KB Output is correct
44 Correct 26 ms 52948 KB Output is correct
45 Correct 26 ms 52928 KB Output is correct
46 Correct 26 ms 52852 KB Output is correct
47 Correct 31 ms 52848 KB Output is correct
48 Correct 25 ms 52832 KB Output is correct
49 Correct 25 ms 52820 KB Output is correct
50 Correct 25 ms 52884 KB Output is correct
51 Correct 25 ms 52820 KB Output is correct
52 Correct 25 ms 52820 KB Output is correct
53 Correct 26 ms 52928 KB Output is correct
54 Correct 48 ms 54212 KB Output is correct
55 Correct 91 ms 57928 KB Output is correct
56 Correct 125 ms 58092 KB Output is correct
57 Correct 122 ms 58020 KB Output is correct
58 Correct 124 ms 58080 KB Output is correct
59 Correct 125 ms 58116 KB Output is correct
60 Correct 121 ms 58028 KB Output is correct
61 Correct 97 ms 58036 KB Output is correct
62 Correct 79 ms 58012 KB Output is correct
63 Correct 124 ms 57984 KB Output is correct
64 Correct 115 ms 58064 KB Output is correct
65 Correct 111 ms 58020 KB Output is correct
66 Correct 112 ms 58068 KB Output is correct
67 Correct 125 ms 58092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 52052 KB Output is correct
2 Correct 25 ms 52100 KB Output is correct
3 Correct 23 ms 51956 KB Output is correct
4 Correct 22 ms 52052 KB Output is correct
5 Correct 21 ms 51952 KB Output is correct
6 Correct 21 ms 52052 KB Output is correct
7 Correct 21 ms 51916 KB Output is correct
8 Correct 22 ms 52056 KB Output is correct
9 Correct 22 ms 51964 KB Output is correct
10 Correct 22 ms 52004 KB Output is correct
11 Correct 26 ms 52040 KB Output is correct
12 Correct 23 ms 52088 KB Output is correct
13 Correct 345 ms 65668 KB Output is correct
14 Correct 377 ms 67156 KB Output is correct
15 Correct 445 ms 68900 KB Output is correct
16 Correct 522 ms 70728 KB Output is correct
17 Correct 616 ms 72544 KB Output is correct
18 Correct 627 ms 72548 KB Output is correct
19 Correct 612 ms 72656 KB Output is correct
20 Correct 23 ms 51944 KB Output is correct
21 Correct 22 ms 52032 KB Output is correct
22 Correct 21 ms 52012 KB Output is correct
23 Correct 21 ms 52052 KB Output is correct
24 Correct 22 ms 52060 KB Output is correct
25 Correct 23 ms 52092 KB Output is correct
26 Correct 22 ms 52052 KB Output is correct
27 Correct 24 ms 52084 KB Output is correct
28 Correct 24 ms 52080 KB Output is correct
29 Correct 28 ms 51984 KB Output is correct
30 Correct 21 ms 52052 KB Output is correct
31 Correct 22 ms 52056 KB Output is correct
32 Correct 21 ms 52052 KB Output is correct
33 Correct 24 ms 52072 KB Output is correct
34 Correct 22 ms 52052 KB Output is correct
35 Correct 23 ms 51924 KB Output is correct
36 Correct 27 ms 52032 KB Output is correct
37 Correct 27 ms 52052 KB Output is correct
38 Correct 26 ms 52132 KB Output is correct
39 Correct 22 ms 52044 KB Output is correct
40 Correct 22 ms 52056 KB Output is correct
41 Correct 26 ms 52084 KB Output is correct
42 Correct 23 ms 52172 KB Output is correct
43 Correct 23 ms 52088 KB Output is correct
44 Correct 23 ms 52052 KB Output is correct
45 Correct 26 ms 52032 KB Output is correct
46 Correct 23 ms 52048 KB Output is correct
47 Correct 22 ms 52052 KB Output is correct
48 Correct 22 ms 52056 KB Output is correct
49 Correct 22 ms 52056 KB Output is correct
50 Correct 22 ms 52052 KB Output is correct
51 Correct 22 ms 52052 KB Output is correct
52 Correct 22 ms 52028 KB Output is correct
53 Correct 22 ms 51952 KB Output is correct
54 Correct 24 ms 52004 KB Output is correct
55 Correct 22 ms 52000 KB Output is correct
56 Correct 23 ms 52060 KB Output is correct
57 Correct 21 ms 52388 KB Output is correct
58 Correct 23 ms 52908 KB Output is correct
59 Correct 26 ms 52932 KB Output is correct
60 Correct 29 ms 52948 KB Output is correct
61 Correct 26 ms 52948 KB Output is correct
62 Correct 26 ms 52928 KB Output is correct
63 Correct 26 ms 52852 KB Output is correct
64 Correct 31 ms 52848 KB Output is correct
65 Correct 25 ms 52832 KB Output is correct
66 Correct 25 ms 52820 KB Output is correct
67 Correct 25 ms 52884 KB Output is correct
68 Correct 25 ms 52820 KB Output is correct
69 Correct 25 ms 52820 KB Output is correct
70 Correct 26 ms 52928 KB Output is correct
71 Correct 48 ms 54212 KB Output is correct
72 Correct 91 ms 57928 KB Output is correct
73 Correct 125 ms 58092 KB Output is correct
74 Correct 122 ms 58020 KB Output is correct
75 Correct 124 ms 58080 KB Output is correct
76 Correct 125 ms 58116 KB Output is correct
77 Correct 121 ms 58028 KB Output is correct
78 Correct 97 ms 58036 KB Output is correct
79 Correct 79 ms 58012 KB Output is correct
80 Correct 124 ms 57984 KB Output is correct
81 Correct 115 ms 58064 KB Output is correct
82 Correct 111 ms 58020 KB Output is correct
83 Correct 112 ms 58068 KB Output is correct
84 Correct 125 ms 58092 KB Output is correct
85 Correct 323 ms 63464 KB Output is correct
86 Correct 778 ms 72884 KB Output is correct
87 Correct 759 ms 72780 KB Output is correct
88 Correct 741 ms 72844 KB Output is correct
89 Correct 727 ms 72724 KB Output is correct
90 Correct 738 ms 72740 KB Output is correct
91 Correct 443 ms 72568 KB Output is correct
92 Correct 480 ms 72552 KB Output is correct
93 Correct 658 ms 72668 KB Output is correct
94 Correct 689 ms 72644 KB Output is correct
95 Correct 734 ms 72648 KB Output is correct
96 Correct 729 ms 72756 KB Output is correct
97 Correct 737 ms 72708 KB Output is correct