alexjercan/bugnet huggingface dataset:
Problem p01939 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = long long;

i64 MOD = 1000000007;

i64 N, M;

int main() {
  cin >> N >> M;
  if (N == 1) {
    cout << (M + 1) % MOD << endl;
    return 0;
  }

  i64 k = (M / (N - 1)) % MOD;

  i64 a = (2 * k + 1) % MOD * ((M + 1) % MOD) % MOD;
  i64 b = (N - 1) % MOD * k % MOD * (k + 1) % MOD;
  cout << (a - b + MOD) % MOD << endl;
  // d = 0 -> M - 0 + 1
  // d = 1 -> M - (N-1) + 1
  // d = 2 -> M - 2*(N-1) + 1
  // ...
  // d = k -> M - k*(N-1) + 1
  // where k <= M/(N-1)
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = long long;

i64 MOD = 1000000007;

i64 modpow(i64 a, i64 b) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a % MOD;
  if (b % 2 == 1) {
    return b * modpow(a, b - 1) % MOD;
  }
  i64 h = pow(a, b / 2);
  return h * h % MOD;
}

i64 modinv(i64 a) { return modpow(a, MOD - 2); }

i64 N, M;

int main() {
  cin >> N >> M;
  if (N == 1) {
    cout << M + 1 << endl;
  }
  i64 k = (M / (N - 1)) % MOD;

  i64 a = (2 * k + 1) % MOD * (M + 1) % MOD;
  i64 b = (N - 1) % MOD * k % MOD * (k + 1) % MOD;
  cout << (a - b + MOD) % MOD << endl;
  // d == 0 -> M - 0 + 1
  // d == 1 -> M - (N-1) + 1
  // d == 2 -> M - 2*(N-1) + 1
  // ...
  // d == k -> M - k*(N-1) + 1
  // k <= M/(N-1)
  return 0;
}
Problem p01939 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main(void) {
  ll N, M;
  cin >> N >> M;

  if (N != 1) {
    ll d = (N - 1) % MOD;
    ll n = ((M + 1) / (N - 1) + 1) % MOD;
    ll x1 = ((M + 1) % (N - 1)) % MOD;
    ll xn = (M + 1) % MOD;

    // cout << d << " " << n << " " << x1 << " " << xn << endl;

    ll sum = (((((x1 + xn) % MOD) * n) % MOD) * ((MOD + 1) / 2)) % MOD;

    // cout << sum << endl;

    cout << (xn + (((sum - xn + MOD) % MOD) * 2) % MOD) % MOD << endl;
    ;
  } else {
    cout << (M + 1) % MOD << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main(void) {
  ll N, M;
  cin >> N >> M;

  ll d = (N - 1) % MOD;
  ll n = ((M + 1) / (N - 1) + 1) % MOD;
  ll x1 = ((M + 1) % (N - 1)) % MOD;
  ll xn = (M + 1) % MOD;

  // cout << d << " " << n << " " << x1 << " " << xn << endl;

  ll sum = ((x1 + xn) * n * ((MOD + 1) / 2)) % MOD;

  // cout << sum << endl;

  cout << (xn + ((sum - xn + MOD) % MOD) * 2) % MOD << endl;
  ;
}
Problem p01939 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FOR(i, a, b) for (ut i = (a); i < (int)(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define ALL(c) c.begin(), c.end()
#define PB push_back
#define cat // cout << __LINE__ << endl;
using namespace std;
typedef long long LL;
typedef long double ld;
typedef int ut;
typedef vector<ut> VI;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef priority_queue<pr, Vpr, greater<pr>> PQ;
const int SIZE = 2 * 1e5;
const int INF = 1LL << 30;
const LL p = 7 + 1e9;
LL n, m;
LL solve() {
  cin >> n >> m;
  if (n == 1)
    return m + 1;
  n--;
  m++;
  LL c = m / n;
  LL mods = m % n;
  c %= p;
  mods %= p;
  n %= p;
  return (mods * (2 * c % p + 1) % p + n * c % p * c % p) % p;
  // return (mods%p*(c+1)%p*(c+1)%p+(n-mods)%p*(c)%p*(c)%p)%p;
}
int main() {
  cout << solve() % p << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (ut i = (a); i < (int)(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define ALL(c) c.begin(), c.end()
#define PB push_back
#define cat // cout << __LINE__ << endl;
using namespace std;
typedef long long LL;
typedef long double ld;
typedef int ut;
typedef vector<ut> VI;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef priority_queue<pr, Vpr, greater<pr>> PQ;
const int SIZE = 2 * 1e5;
const int INF = 1LL << 30;
const LL p = 7 + 1e9;
LL n, m;
LL solve() {
  cin >> n >> m;
  n--;
  m++;
  LL c = m / n;
  c %= p;
  LL mods = m % n;
  mods %= p;
  n %= p;
  return (mods * (2 * c % p + 1) % p + n * c % p * c % p) % p;
  // return (mods%p*(c+1)%p*(c+1)%p+(n-mods)%p*(c)%p*(c)%p)%p;
}
int main() {
  cout << solve() << endl;
  return 0;
}
Problem p01939 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll extgcd(ll a, ll b, ll &x, ll &y) {
  ll d = a;
  if (b != 0LL) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

ll mod_inverse(ll a, ll m) {
  ll x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

ll n, m;

int main(void) {
  scanf("%lld%lld", &n, &m);
  if (n == 1LL) {
    printf("%lld\\n", (m + 1LL) % MOD);
    return 0;
  }
  ll d_max = m / (n - 1LL);
  ll val2 = (((m + 1LL) % MOD) * (d_max % MOD)) % MOD;
  val2 = val2 * 2LL % MOD;
  ll val3 = d_max % MOD;
  ll val4 = (n - 1LL) % MOD;
  ll val5 = (n - 1LL) % MOD * (d_max % MOD) % MOD;
  ll val6 = (val4 + val5) % MOD;
  ll res = (val2 - val3 * val6 % MOD + MOD) % MOD;
  res += (m + 1LL) % MOD;
  res %= MOD;
  printf("%lld\\n", res);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll n, m;

int main(void) {
  scanf("%lld%lld", &n, &m);
  ll b = 0;
  ll as = m + 1LL;
  ll diff = n - 1;
  ll kosu = (as + diff - 1LL) / diff;
  ll res =
      ((as % MOD + as % MOD - (diff) * (kosu - 1LL) % MOD) % MOD) * kosu % MOD;
  res -= m + 1;
  res %= MOD;
  res += MOD;
  res %= MOD;
  printf("%lld\\n", res);
  return 0;
}
Problem p01939 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back

const ll MOD = 1e9 + 7;
signed main() {
  int N, M;
  cin >> N >> M;

  if (M < N || N == 1) {
    cout << (M + 1) % MOD << endl;
    return 0;
  }

  int n = (M / (N - 1)) % MOD;
  int a = (M + 1 - N + 1) % MOD;
  int d = 1 + MOD - (N % MOD);

  cout << (M + 1 +
           2 * ((n * a) % MOD +
                ((n * (n - 1) / 2) % MOD * (1 + MOD - N % MOD)) % MOD)) %
              MOD
       << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back

const ll MOD = 1e9 + 7;
signed main() {
  int N, M;
  cin >> N >> M;

  int n = (M / (N - 1)) + 1;
  n %= MOD;

  int l = (n * (M % MOD + 1)) % MOD -
          ((n * (n - 1) / 2) % MOD * (N + MOD - 1) % MOD) % MOD + 100 * MOD;
  assert(l >= 0);
  l %= MOD;

  cout << (l * 2 - (M + 1) % MOD + 10 * MOD) % MOD << endl;

  assert((l * 2 - (M + 1) % MOD + 10 * MOD) % MOD >= 0);
}
Problem p01939 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll mod = 1000000007;
  ll n;
  ll m;
  cin >> n >> m;
  // ll ks=m/(n-1)
  if (n == 1) {
    cout << (m + 1) % mod << endl;
    return 0;
  }

  ll sy = m + 1;
  ll ma = sy % (n - 1);
  ll ks = sy / (n - 1);
  //  if(ma)ks++;
  ks++;
  cout << ((((sy + ma) % mod) * (ks % mod)) % mod - sy % mod + mod) % mod
       << endl;
  //  cout << (m+1)%mod*((m+1)/(n-1))%mod<< endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll mod = 1000000007;
  ll n;
  ll m;
  cin >> n >> m;
  // ll ks=m/(n-1)
  if (n == 1) {
    assert(0);
    cout << m + 1 << endl;
    return 0;
  }

  ll sy = m + 1;
  ll ma = sy % (n - 1);
  ll ks = sy / (n - 1);
  //  if(ma)ks++;
  ks++;
  cout << ((((sy + ma) % mod) * (ks % mod)) % mod - sy % mod + mod) % mod
       << endl;
  //  cout << (m+1)%mod*((m+1)/(n-1))%mod<< endl;
  return 0;
}
Problem p01939 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define int long long
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define ll long long

static const int MOD = 1000000007;

int n, m;
int ns, f, d;
int ans;

signed main() {

  cin >> n >> m;

  if (n == 1) {
    ns = 0;
  } else
    ns = ((m + 1) / (n - 1)) % MOD;
  f = (m + 1 - (n - 1)) % MOD;
  d = -(n - 1) % MOD;

  ans = (ns * ((2 * f + ((ns - 1) * d) % MOD) % MOD) % MOD + m + 1) % MOD;

  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define int long long
#define REP(i, n) for (int i = 0; (i) < (n); (i)++)
#define ll long long

static const int MOD = 1000000007;

int n, m;
int ns, f, d;
int ans;

signed main() {

  cin >> n >> m;

  ns = ((m + 1) / (n - 1)) % MOD;
  f = (m + 1 - (n - 1)) % MOD;
  d = -(n - 1) % MOD;

  ans = (ns * ((2 * f + ((ns - 1) * d) % MOD) % MOD) % MOD + m + 1) % MOD;

  cout << ans << endl;
  return 0;
}
Problem p01940 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
using namespace std;

string T, P;
vector<int> vec;

main() {
  cin >> T >> P;
  vec = vector<int>(P.size(), -1);
  int ip = 0;
  for (int i = 0; i < T.size() && ip < P.size(); i++) {
    if (T[i] == P[ip]) {
      vec[ip++] = i;
    }
  }
  if (vec[P.size() - 1] < 0) {
    cout << "no" << endl;
    return 0;
  }
  ip = P.size() - 1;
  for (int i = T.size() - 1; i >= 0; i--) {
    if (T[i] == P[ip]) {
      if (i != vec[ip--]) {
        cout << "no" << endl;
        return 0;
      }
    }
  }
  cout << "yes" << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

string T, P;

int solve(int it, int ip) {
  if (ip == P.size()) {
    return 0;
  }
  bool f = false;
  int mx = T.size();
  int back = 0;
  for (int i = it; i < mx; i++) {
    if (T[i] == P[ip]) {
      if (f) {
        return -2;
      } else {
        int res = solve(i + 1, ip + 1);
        if (res >= 0) {
          f = true;
          mx = res;
          back = i;
        } else {
          return -1;
        }
      }
    }
  }
  if (f) {
    return back;
  } else {
    return -1;
  }
}

int main() {
  cin >> T >> P;
  int ans = solve(0, 0);
  cout << (ans >= 0 ? "yes" : "no") << endl;
  return 0;
}
Problem p01940 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  string S;
  string P;
  cin >> S;
  cin >> P;

  int search = 0;
  int count = 0;
  bool finish = false;
  int preserch = 0;
  bool many_same_char = false;
  vector<tuple<int, int>> st;

  for (int ptr = 0; !finish; ptr++) {
    if (S[ptr] == P[search]) {
      preserch = search;
      if (search >= P.length() - 1) {
        int oldcount = count;
        for (int subptr = ptr; subptr < S.length(); subptr++) {
          if (S[subptr] == P[search]) {
            count++;
          }
        }
        if (count - oldcount == 1) {
          if (many_same_char) {
            count = 2000;
          } else {
            count = 1;
          }
          finish = true;
        }

        if (!st.empty()) {
          tuple<int, int> t = st.back();
          st.pop_back();
          ptr = get<0>(t);
          search = get<1>(t);
        } else {
          finish = true;
        }

        if (count >= 2) {
          finish = true;
        }
      } else {
        st.push_back(make_tuple(ptr, search));
        search++;
      }
    } else if (ptr >= S.length() - 1) {
      finish = true;
    } else if (S[ptr] == P[preserch]) {
      many_same_char = true;
    }
  }

  if (count == 1) {
    cout << "yes" << endl;
  } else {
    cout << "no" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  string S;
  string P;
  cin >> S;
  cin >> P;

  int search = 0;
  int count = 0;
  bool finish = false;
  int preserch = 0;
  bool many_same_char = false;
  vector<tuple<int, int>> st;

  for (int ptr = 0; !finish; ptr++) {
    if (S[ptr] == P[search]) {
      preserch = search;
      if (search >= P.length() - 1) {
        int oldcount = count;
        for (int subptr = ptr; subptr < S.length(); subptr++) {
          if (S[subptr] == P[search]) {
            count++;
          }
        }
        if (count - oldcount == 1) {
          if (many_same_char) {
            count = 2000;
          } else {
            count = 1;
          }
          finish = true;
        }

        if (!st.empty()) {
          tuple<int, int> t = st.back();
          st.pop_back();
          ptr = get<0>(t);
          search = get<1>(t);
        } else {
          finish = true;
        }

        if (count >= 2) {
          finish = true;
        }
      } else {
        st.push_back(make_tuple(ptr, search));
        search++;
      }
    } else if (ptr >= S.length() - 1) {
      if (!st.empty()) {
        tuple<int, int> t = st.back();
        st.pop_back();
        ptr = get<0>(t);
        search = get<1>(t);
      } else {
        finish = true;
      }
    } else if (S[ptr] == P[preserch]) {
      many_same_char = true;
    }
  }

  if (count == 1) {
    cout << "yes" << endl;
  } else {
    cout << "no" << endl;
  }

  return 0;
}
Problem p01940 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

string T, P;
vector<int> v1, v2;
int main(void) {
  cin >> T >> P;
  bool ans = false;
  int ind = 0;
  for (int i = 0; i < T.size() && ind < P.size(); i++) {
    if (T[i] == P[ind]) {
      v1.push_back(i);
      ind++;
    }
  }
  if (v1.size() == P.size()) {
    ind--;
    for (int i = T.size() - 1; i >= 0 && ind >= 0; i--) {
      if (T[i] == P[ind]) {
        v2.push_back(i);
        ind--;
      }
    }
    reverse(v2.begin(), v2.end());
    ans = v1 == v2;
  }
  cout << (ans ? "yes" : "no") << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
using ll = long long;

string T, P;
map<pair<int, int>, int> m;
vector<int> loc[26];
int dfs(int ti, int pi) {
  if (pi == P.size())
    return 1;
  if (ti == T.size())
    return 0;
  if (m.count({ti, pi}))
    return m[{ti, pi}];
  ll ret = 0;
  auto v = loc[P[pi] - 'a'];
  for (auto it = lower_bound(v.begin(), v.end(), ti); it != v.end(); it++) {
    ret += dfs((*it) + 1, pi + 1);
    if (ret >= 2)
      break;
  }
  return m[{ti, pi}] = ret;
}
int main(void) {
  cin >> T >> P;
  for (int i = 0; i < 26; i++)
    loc[i] = vector<int>();
  for (int i = 0; i < T.size(); i++) {
    int ind = T[i] - 'a';
    loc[ind].push_back(i);
  }
  cout << (dfs(0, 0) == 1 ? "yes" : "no") << endl;
}
Problem p01940 (C++) — buggy submission, verdict: Runtime Error

#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
  string t, p;
  cin >> t >> p;

  int n = t.size();
  int m = p.size();

  vector<int> ks;
  int k = 0;
  int i = 0;
  while (k < n) {
    while (k < n && t[k] != p[i])
      k++;
    if (k >= n)
      break;
    ks.push_back(k);
    k++;
    i++;
    if (i == m)
      break;
  }
  if (i < m) { // no match
    cout << "no" << endl;
    return 0;
  }
  // now, found a match. let's try next!
  // try moving the last one to the next position
  // if possible, then the second match found.
  // if not, try moving the 2nd last one to the next position before the last,
  // and so on.
  ks.push_back(n);
  while (i > 0) {
    i--;
    for (k = ks[i] + 1; k < ks[i + 1]; k++) {
      if (t[k] == p[i]) { // the second match!
        cout << "no" << endl;
        return 0;
      }
    }
  }
  cout << "yes" << endl;
}

Error: 0

Fixed submission:

#include <functional>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(int argc, char *argv[]) {
  string t, p;
  cin >> t >> p;

  int n = t.size();
  int m = p.size();

  vector<int> ks;
  int k = 0;
  int i = 0;
  while (k < n) {
    while (k < n && t[k] != p[i])
      k++;
    if (k >= n)
      break;
    ks.push_back(k);
    i++;
    if (i == m)
      break;
  }
  if (i < m) { // no match
    throw 1;
    cout << "no" << endl;
    return 0;
  }
  // now, found a match. let's try next!
  // try moving the last one to the next position
  // if possible, then the second match found.
  // if not, try moving the 2nd last one to the next position before the last,
  // and so on.
  ks.push_back(n);
  while (i > 0) {
    i--;
    for (k = ks[i] + 1; k < ks[i + 1]; k++) {
      if (t[k] == p[i]) { // the second match!
        cout << "no" << endl;
        return 0;
      }
    }
  }
  cout << "yes" << endl;
}
Problem p01940 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string t, p;
  cin >> t >> p;
  int i, j;
  vector<int> a;
  for (i = j = 0; i < t.size() && j < p.size(); i++) {
    if (t[i] == p[j]) {
      j++;
      a.push_back(i);
    }
  }
  if (j != p.size()) {
    cout << "no" << endl;
    return 0;
  }
  string s = t, q = p;
  reverse(s.begin(), s.end());
  reverse(q.begin(), q.end());
  int k, l;
  vector<int> b;
  for (int k = l = 0; k < s.size() && l < q.size(); k++) {
    if (s[k] == q[l]) {
      l++;
      b.push_back(s.size() - k - 1);
    }
  }
  reverse(b.begin(), b.end());
  for (int m = 0; m < a.size(); m++) {
    if (a[m] != b[m]) {
      cout << "no" << endl;
      return 0;
    }
  }
  cout << "yes" << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string t, p;
  cin >> t >> p;
  int i, j;
  vector<int> a;
  for (i = j = 0; i < t.size() || j < p.size(); i++) {
    if (t[i] == p[j]) {
      j++;
      a.push_back(i);
    }
  }
  if (j != p.size()) {
    cout << "no" << endl;
    return 0;
  }
  string s = t, q = p;
  reverse(s.begin(), s.end());
  reverse(q.begin(), q.end());
  int k, l;
  vector<int> b;
  for (int k = l = 0; k < s.size() || l < q.size(); k++) {
    if (s[k] == q[l]) {
      l++;
      b.push_back(s.size() - k - 1);
    }
  }
  reverse(b.begin(), b.end());
  for (int m = 0; m < a.size(); m++) {
    if (a[m] != b[m]) {
      cout << "no" << endl;
      return 0;
    }
  }
  cout << "yes" << endl;
}
Problem p01941 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> P;
const ll mod = 1e9 + 7;
const ll inf = 1 << 29;

const int M = 10000;

int n, c;

int main() {
  cin >> n >> c;
  vvi dp(c + 1, vi(M + 1, -inf));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    int A, B, C;
    cin >> A >> B >> C;
    vvi DP = dp;
    for (int j = 0; j <= c - C; j++)
      for (int k = 0; k <= M; k++) {
        int tmp = dp[j][k];
        if (max(tmp + A, k + B) <= M)
          DP[j + C][max(tmp + A, k + B)] =
              max(DP[j + C][max(tmp + A, k + B)], min(tmp + A, k + B));
        if (max(tmp + B, k + A) <= M)
          DP[j + C][max(tmp + B, k + A)] =
              max(DP[j + C][max(tmp + B, k + A)], min(tmp + B, k + A));
      }
    dp = DP;
  }
  int res = 0;
  for (int i = 0; i <= c; i++)
    for (int j = 0; j < M; j++)
      res = max(res, dp[i][j]);
  cout << res << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> P;
const ll mod = 1e9 + 7;

int n, c;

int main() {
  cin >> n >> c;
  map<P, int> m;
  m[{0, 0}] = 0;
  for (int i = 0; i < n; i++) {
    int A, B, C;
    cin >> A >> B >> C;
    map<P, int> M;
    for (auto j = m.begin(); j != m.end(); j++) {
      P p = j->first;
      int co = p.first, I = p.second, J = j->second, tmp = M[p];
      M[p] = max(tmp, J);
      if (co + C <= c) {
        tmp = M[{co + C, max(I + A, B + J)}];
        M[{co + C, max(I + A, B + J)}] = max(tmp, min(I + A, B + J));
        tmp = M[{co + C, max(I + B, J + A)}];
        M[{co + C, max(I + B, J + A)}] = max(tmp, min(I + B, J + A));
      }
    }
    m = M;
  }
  int res = 0;
  for (auto i = m.begin(); i != m.end(); i++)
    res = max(res, i->second);
  cout << res << endl;
}
Problem p01944 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
typedef vector<ll> mat;
int sz;
mat mul(mat A, mat B) {
  mat C(sz);
  rep(i, sz) rep(j, sz) C[(i + j) % sz] += A[i] * B[j] % mod;
  rep(i, sz) C[i] %= mod;
  return C;
}

mat pow(mat A, ll k) {
  mat B(sz);
  B[0] = 1;
  while (k > 0) {
    if (k & 1)
      B = mul(A, B);
    A = mul(A, A);
    k /= 2;
  }
  return B;
}
int main() {
  cin >> sz;
  int m, k;
  cin >> m >> k;
  mat A(sz);
  int p[m];
  rep(i, m) {
    cin >> p[i];
    A[p[i] % sz]++;
  }
  A[0]++;
  A = pow(A, k);
  rep(i, sz) cout << A[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
typedef vector<vector<ll>> mat;
int sz;
mat mul(mat A, mat B) {
  mat C(sz, vector<ll>(sz));
  rep(i, sz) rep(j, sz) rep(k, sz) C[i][j] =
      (C[i][j] + A[i][k] * B[k][j]) % mod;
  return C;
}

mat pow(mat A, ll k) {
  mat B(sz, vector<ll>(sz));
  rep(i, sz) B[i][i] = 1;
  while (k > 0) {
    if (k & 1)
      B = mul(A, B);
    A = mul(A, A);
    k /= 2;
  }
  return B;
}
int main() {
  cin >> sz;
  int m, k;
  cin >> m >> k;
  mat A(sz, vector<ll>(sz));
  int p[m];
  rep(i, m) {
    cin >> p[i];
    rep(j, sz) { A[(j + p[i]) % sz][j]++; }
  }
  rep(i, sz) A[i][i]++;
  A = pow(A, k);
  rep(i, sz) cout << A[i][0] << endl;
  return 0;
}