Submission #673636

# Submission time Handle Problem Language Result Execution time Memory
673636 2022-12-21T14:16:16 Z peijar Copy and Paste 3 (JOI22_copypaste3) C++17
100 / 100
932 ms 49712 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

template <const int32_t MOD> struct ModInt {
  int32_t x;
  ModInt() : x(0) {}
  ModInt(long long u) : x(u % MOD) {
    if (x < 0)
      x += MOD;
  }
  friend bool operator==(const ModInt &a, const ModInt &b) {
    return a.x == b.x;
  }
  friend bool operator!=(const ModInt &a, const ModInt &b) {
    return a.x != b.x;
  }
  friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; }
  friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; }
  friend bool operator<=(const ModInt &a, const ModInt &b) {
    return a.x <= b.x;
  }
  friend bool operator>=(const ModInt &a, const ModInt &b) {
    return a.x >= b.x;
  }
  static ModInt sign(long long k) {
    return ((k & 1) ? ModInt(MOD - 1) : ModInt(1));
  }

  ModInt &operator+=(const ModInt &m) {
    x += m.x;
    if (x >= MOD)
      x -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &m) {
    x -= m.x;
    if (x < 0LL)
      x += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &m) {
    x = (1LL * x * m.x) % MOD;
    return *this;
  }

  friend ModInt operator-(const ModInt &a) {
    ModInt res(a);
    if (res.x)
      res.x = MOD - res.x;
    return res;
  }
  friend ModInt operator+(const ModInt &a, const ModInt &b) {
    return ModInt(a) += ModInt(b);
  }
  friend ModInt operator-(const ModInt &a, const ModInt &b) {
    return ModInt(a) -= ModInt(b);
  }
  friend ModInt operator*(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b);
  }

  static long long fp(long long u, long long k) {
    long long res = 1LL;
    while (k > 0LL) {
      if (k & 1LL)
        res = (res * u) % MOD;
      u = (u * u) % MOD;
      k /= 2LL;
    }
    return res;
  }

  static constexpr int mod() { return MOD; }

  ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
  ModInt inv() {
    assert(x);
    return ModInt(fp(x, MOD - 2));
  }
  ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); }
  friend ModInt operator/(const ModInt &a, const ModInt &b) {
    return ModInt(a) *= ModInt(b).inv();
  }

  friend ostream &operator<<(ostream &out, const ModInt &a) {
    return out << a.x;
  }
  friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; }
};

const int MOD1 = 1e9 + 9, MOD2 = 1e9 + 7;

template <typename T1, typename T2> struct ModPair {
  T1 x;
  T2 y;

  ModPair() : x(0), y(0) {}

  ModPair(int u) : x(u), y(u) {}

  ModPair(T1 _x, T2 _y) : x(_x), y(_y) {}

  ModPair &operator+=(const ModPair &other) {
    x += other.x;
    y += other.y;
    return *this;
  }

  ModPair &operator*=(const ModPair &other) {
    x *= other.x;
    y *= other.y;
    return *this;
  }

  ModPair &operator-=(const ModPair &other) {
    x -= other.x;
    y -= other.y;
    return *this;
  }

  friend ModPair operator+(const ModPair &a, const ModPair &b) {
    return ModPair(a) += ModPair(b);
  }
  friend ModPair operator-(const ModPair &a, const ModPair &b) {
    return ModPair(a) -= ModPair(b);
  }
  friend ModPair operator*(const ModPair &a, const ModPair &b) {
    return ModPair(a) *= ModPair(b);
  }

  bool operator<(const ModPair &o) const { return pair(x, y) < pair(o.x, o.y); }
  bool operator>(const ModPair &o) const { return pair(x, y) > pair(o.x, o.y); }
  bool operator<=(const ModPair &o) const {
    return pair(x, y) <= pair(o.x, o.y);
  }
  bool operator>=(const ModPair &o) const {
    return pair(x, y) >= pair(o.x, o.y);
  }
  bool operator==(const ModPair &o) const {
    return pair(x, y) == pair(o.x, o.y);
  }

  ModPair inv() { return ModPair(x.inv(), y.inv()); };
};

using Mint = ModPair<ModInt<MOD1>, ModInt<MOD2>>;

const int MAXN = 2501;
const int INF = 1e18;

Mint powBase[MAXN], invPow[MAXN];
int N, costAdd, costCut, costPaste;
string s;
vector<Mint> prefPow;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int randint(int lb, int ub) {
  return uniform_int_distribution<int>(lb, ub)(rng);
}

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int BASE = randint(300, 400);

  powBase[0] = 1;
  for (int i = 1; i < MAXN; ++i)
    powBase[i] = powBase[i - 1] * BASE;
  invPow[MAXN - 1] = powBase[MAXN - 1].inv();
  for (int i = MAXN - 1; i > 0; --i)
    invPow[i - 1] = invPow[i] * BASE;

  cin >> N >> s >> costAdd >> costCut >> costPaste;
  prefPow.resize(N + 1);

  for (int i = 0; i < N; ++i)
    prefPow[i + 1] = prefPow[i] + powBase[i] * s[i];

  vector<vector<int>> dp(N + 1, vector<int>(N + 1, INF));

  for (int len = 0; len <= N; ++len) {
    map<Mint, vector<int>> occs;
    for (int deb = 0; deb + len <= N; ++deb) {
      dp[deb][deb + len] = min(dp[deb][deb + len], len * costAdd);
      if (len)
        dp[deb][deb + len] =
            min(dp[deb][deb + len],
                min(dp[deb + 1][deb + len], dp[deb][deb + len - 1]) + costAdd);
      Mint x = (prefPow[deb + len] - prefPow[deb]) * invPow[deb];
      occs[x].push_back(deb);
    }
    if (!len)
      continue;

    for (auto &[x, vec] : occs) {
      for (int i = 0; i < (int)vec.size(); ++i) {
        int nbOccs = 0;
        int curPos = vec[i];
        while (true) {
          nbOccs++;
          dp[vec[i]][curPos + len] =
              min(dp[vec[i]][curPos + len],
                  dp[vec[i]][vec[i] + len] +
                      costAdd * (curPos + len - vec[i] - nbOccs * len) +
                      costCut + costPaste * nbOccs);
          int j =
              lower_bound(vec.begin(), vec.end(), curPos + len) - vec.begin();
          if (j == (int)vec.size())
            break;
          curPos = vec[j];
        }
      }
    }
  }
  cout << dp[0][N] << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 428 ms 26652 KB Output is correct
4 Correct 529 ms 31108 KB Output is correct
5 Correct 638 ms 36308 KB Output is correct
6 Correct 774 ms 42352 KB Output is correct
7 Correct 929 ms 49484 KB Output is correct
8 Correct 932 ms 49464 KB Output is correct
9 Correct 926 ms 49472 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 3 ms 596 KB Output is correct
42 Correct 5 ms 596 KB Output is correct
43 Correct 4 ms 596 KB Output is correct
44 Correct 4 ms 596 KB Output is correct
45 Correct 4 ms 596 KB Output is correct
46 Correct 5 ms 596 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 4 ms 596 KB Output is correct
49 Correct 4 ms 596 KB Output is correct
50 Correct 5 ms 596 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
52 Correct 3 ms 596 KB Output is correct
53 Correct 4 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 3 ms 596 KB Output is correct
42 Correct 5 ms 596 KB Output is correct
43 Correct 4 ms 596 KB Output is correct
44 Correct 4 ms 596 KB Output is correct
45 Correct 4 ms 596 KB Output is correct
46 Correct 5 ms 596 KB Output is correct
47 Correct 3 ms 596 KB Output is correct
48 Correct 4 ms 596 KB Output is correct
49 Correct 4 ms 596 KB Output is correct
50 Correct 5 ms 596 KB Output is correct
51 Correct 3 ms 596 KB Output is correct
52 Correct 3 ms 596 KB Output is correct
53 Correct 4 ms 596 KB Output is correct
54 Correct 21 ms 2004 KB Output is correct
55 Correct 82 ms 8148 KB Output is correct
56 Correct 113 ms 8276 KB Output is correct
57 Correct 110 ms 8276 KB Output is correct
58 Correct 115 ms 8352 KB Output is correct
59 Correct 111 ms 8276 KB Output is correct
60 Correct 109 ms 8276 KB Output is correct
61 Correct 80 ms 8280 KB Output is correct
62 Correct 54 ms 8252 KB Output is correct
63 Correct 99 ms 8276 KB Output is correct
64 Correct 99 ms 8304 KB Output is correct
65 Correct 92 ms 8276 KB Output is correct
66 Correct 89 ms 8296 KB Output is correct
67 Correct 113 ms 8348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 428 ms 26652 KB Output is correct
14 Correct 529 ms 31108 KB Output is correct
15 Correct 638 ms 36308 KB Output is correct
16 Correct 774 ms 42352 KB Output is correct
17 Correct 929 ms 49484 KB Output is correct
18 Correct 932 ms 49464 KB Output is correct
19 Correct 926 ms 49472 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 0 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 0 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 0 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 3 ms 596 KB Output is correct
59 Correct 5 ms 596 KB Output is correct
60 Correct 4 ms 596 KB Output is correct
61 Correct 4 ms 596 KB Output is correct
62 Correct 4 ms 596 KB Output is correct
63 Correct 5 ms 596 KB Output is correct
64 Correct 3 ms 596 KB Output is correct
65 Correct 4 ms 596 KB Output is correct
66 Correct 4 ms 596 KB Output is correct
67 Correct 5 ms 596 KB Output is correct
68 Correct 3 ms 596 KB Output is correct
69 Correct 3 ms 596 KB Output is correct
70 Correct 4 ms 596 KB Output is correct
71 Correct 21 ms 2004 KB Output is correct
72 Correct 82 ms 8148 KB Output is correct
73 Correct 113 ms 8276 KB Output is correct
74 Correct 110 ms 8276 KB Output is correct
75 Correct 115 ms 8352 KB Output is correct
76 Correct 111 ms 8276 KB Output is correct
77 Correct 109 ms 8276 KB Output is correct
78 Correct 80 ms 8280 KB Output is correct
79 Correct 54 ms 8252 KB Output is correct
80 Correct 99 ms 8276 KB Output is correct
81 Correct 99 ms 8304 KB Output is correct
82 Correct 92 ms 8276 KB Output is correct
83 Correct 89 ms 8296 KB Output is correct
84 Correct 113 ms 8348 KB Output is correct
85 Correct 316 ms 20212 KB Output is correct
86 Correct 813 ms 49708 KB Output is correct
87 Correct 796 ms 49704 KB Output is correct
88 Correct 791 ms 49712 KB Output is correct
89 Correct 767 ms 49704 KB Output is correct
90 Correct 781 ms 49708 KB Output is correct
91 Correct 384 ms 49492 KB Output is correct
92 Correct 381 ms 49468 KB Output is correct
93 Correct 672 ms 49600 KB Output is correct
94 Correct 714 ms 49616 KB Output is correct
95 Correct 709 ms 49632 KB Output is correct
96 Correct 701 ms 49612 KB Output is correct
97 Correct 772 ms 49704 KB Output is correct