alexjercan/bugnet huggingface dataset:
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

vector<int> v;
int n, ans;

int fix(int a) {
  if (a >= 10)
    a -= 10;
  if (a < 0)
    a += 10;
  return a;
}

void dfs(vector<int> v2, int pos, int res) {
  if (ans <= res)
    return;

  for (int i = pos; i < n; i++) {
    if (v2[i] == 0)
      pos++;
    else
      break;
  }

  if (pos >= n) {
    ans = min(ans, res);
    return;
  }

  int cnt = v2[pos];
  for (int i = pos; i < n; i++) {
    v2[i] = fix(v2[i] - cnt);
    dfs(v2, pos + 1, res + 1);
  }
}

int main() {
  while (cin >> n && n) {
    string str, str2;
    v.clear();
    cin >> str >> str2;
    for (int i = 0; i < n; i++)
      v.push_back(fix(str[i] - '0' - (str2[i] - '0')));
    ans = n;
    dfs(v, 0, 0);
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

vector<int> v;
int n;

struct state {
  vector<int> v;
  int n, ans;
  state(vector<int> v, int n = 0, int ans = 0) : v(v), n(n), ans(ans) {}
};

int fix(int a) {
  if (a >= 10)
    a -= 10;
  if (a < 0)
    a += 10;
  return a;
}

void bfs() {
  queue<state> Q;
  state u(v);

  for (Q.push(u); !Q.empty();) {
    u = Q.front();
    Q.pop();

    for (int i = u.n; i < n; i++) {
      if (u.v[i] == 0)
        u.n++;
      else
        break;
    }
    if (u.n >= n) {
      cout << u.ans << endl;
      return;
    }

    int cnt = u.v[u.n];
    for (int i = u.n; i < n; i++) {
      u.v[i] = fix(u.v[i] - cnt);
      Q.push(state(u.v, u.n + 1, u.ans + 1));
    }
  }

  cout << n << endl;
}

int main() {
  while (cin >> n && n) {
    string str, str2;
    v.clear();
    cin >> str >> str2;
    for (int i = 0; i < n; i++)
      v.push_back(fix(str[i] - '0' - (str2[i] - '0')));
    bfs();
  }
}
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k;
  while (cin >> k, k) {
    string s, e;
    cin >> s >> e;
    typedef pair<string, int> P;
    set<string> m;
    P p;
    queue<P> q;
    q.push(P(s, 0));
    int ans = k;
    while (!q.empty()) {
      p = q.front();
      q.pop();
      s = p.first;
      if (s == e) {
        ans = min(ans, p.second);
        break;
      }
      i = 0;
      while (s[i] == e[i])
        i++;
      int x = e[i] - s[i];
      bool f = false;
      for (j = i; j < k; j++) {
        s[j] = '0' + ((s[j] - '0' + x + 10) % 10);
        if (m.find(s) == m.end()) {
          m.insert(s);
          q.push(P(s, p.second + 1));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k;
  while (cin >> k, k) {
    string s, e;
    cin >> s >> e;
    s.shrink_to_fit();
    e.shrink_to_fit();
    typedef pair<string, int> P;
    P p;
    queue<P> q;
    q.push(P(s, 0));
    int ans = k;
    while (!q.empty()) {
      p = q.front();
      q.pop();
      s = p.first;
      if (s == e) {
        ans = min(ans, p.second);
        break;
      }
      i = 0;
      while (s[i] == e[i])
        i++;
      int x = e[i] - s[i];
      bool f = false;
      for (j = i; j < k; j++) {
        s[j] = '0' + ((s[j] - '0' + x + 10) % 10);
        s.shrink_to_fit();
        q.push(P(s, p.second + 1));
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

map<string, int> d;

int solve(string ini, string unlock) {
  queue<string> que;
  d.clear();
  que.push(ini);
  d[ini] = 0;
  while (!que.empty()) {
    string s = que.front();
    que.pop();
    if (s == unlock)
      break;
    string t = s;
    int idx = 0, rol = 0;
    while (t[idx] == unlock[idx])
      idx++;
    while ((t[idx] - '0' + rol) % 10 != unlock[idx] - '0')
      rol++;
    for (int i = idx; i < t.size(); i++) {
      t[i] = (t[i] - '0' + rol) % 10 + '0';
      if (d.count(t) != 1) {
        que.push(t);
        d[t] = d[s] + 1;
      }
    }
  }
  return d[unlock];
}

int main() {
  int k;
  while (cin >> k, k) {
    string ini, unlock;
    cin >> ini >> unlock;
    cout << solve(ini, unlock) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

map<string, int> d;

int solve(string ini, string unlock) {
  typedef pair<int, string> P;
  priority_queue<P, vector<P>, greater<P>> que;
  d.clear();
  que.push(P(0, ini));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int c = p.first;
    string s = p.second;
    if (s == unlock)
      return c;
    if (d.count(s) == 1)
      continue;
    d[s] = c;
    for (int i = 1; i < 10; i++) {
      for (int j = 0; j < s.size(); j++) {
        for (int k = j + 1; k <= s.size(); k++) {
          string t = s.substr(0, j);
          for (int l = j; l < k; l++)
            t += (char)((s[l] - '0' + i) % 10 + '0');
          t += s.substr(k);
          if (d.count(t) != 1) {
            que.push(P(d[s] + 1, t));
          }
        }
      }
    }
  }
  return -1;
}

int main() {
  int k;
  while (cin >> k, k) {
    string ini, unlock;
    cin >> ini >> unlock;
    cout << solve(ini, unlock) << endl;
  }
  return 0;
}
Problem p01277 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <random>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
struct P {
  ll x;
  ll y;
  bool operator<(const P &p) const { return (x != p.x) ? x < p.x : y < p.y; }
};
vector<P> pos;
int N;

bool ok(const int i, const int j) {
  const ll x = (pos[i].x + pos[j].x) / 2;
  const ll y = (pos[i].y + pos[j].y) / 2;
  const ll dx = pos[j].x - x;
  const ll dy = pos[j].y - y;
  set<P> p;
  int cnt = 0;
  ll h = 0;
  for (int k = 0; k < N; k++) {
    if (k == i or k == j) {
      continue;
    }
    const ll X = pos[k].x - x;
    const ll Y = pos[k].y - y;
    const ll S = X * dx + Y * dy;
    const ll T = -X * dy + Y * dx;
    h = max(h, abs(T));
    if (S == 0) {
      cnt++;
    }
    p.insert(P{S, T});
  }
  if (cnt >= 3 or h == 0) {
    return false;
  }
  for (const auto &pp : p) {
    if (p.find(P{-pp.x, pp.y}) == p.end()) {
      return false;
    }
  }
  return true;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    ll x, y;
    cin >> x >> y;
    pos.push_back(P{2 * x, 2 * y});
  }
  mt19937 mt{random_device{}()};
  uniform_int_distribution<int> dist{0, N - 1};
  for (int i = 0; i < 30 * N; i++) {
    int u = 0;
    int v = 0;
    while (u == v) {
      u = dist(mt);
      v = dist(mt);
    }
    if (ok(u, v)) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <random>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
struct P {
  ll x;
  ll y;
  bool operator<(const P &p) const { return (x != p.x) ? x < p.x : y < p.y; }
};
vector<P> pos;
int N;

bool ok(const int i, const int j) {
  const ll x = (pos[i].x + pos[j].x) / 2;
  const ll y = (pos[i].y + pos[j].y) / 2;
  const ll dx = pos[j].x - x;
  const ll dy = pos[j].y - y;
  set<P> p;
  int cnt = 0;
  ll h = 0;
  for (int k = 0; k < N; k++) {
    if (k == i or k == j) {
      continue;
    }
    const ll X = pos[k].x - x;
    const ll Y = pos[k].y - y;
    const ll S = X * dx + Y * dy;
    const ll T = -X * dy + Y * dx;
    h = max(h, abs(T));
    if (S == 0) {
      cnt++;
    }
    p.insert(P{S, T});
  }
  if (cnt >= 3 or h == 0) {
    return false;
  }
  for (const auto &pp : p) {
    if (p.find(P{-pp.x, pp.y}) == p.end()) {
      return false;
    }
  }
  return true;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    ll x, y;
    cin >> x >> y;
    pos.push_back(P{2 * x, 2 * y});
  }
  mt19937 mt{random_device{}()};
  uniform_int_distribution<int> dist{0, N - 1};
  for (int i = 0; i < 100 * N; i++) {
    int u = 0;
    int v = 0;
    while (u == v) {
      u = dist(mt);
      v = dist(mt);
    }
    if (ok(u, v)) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;

  return 0;
}
Problem p01279 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef complex<double> Point;

int n, m;
Point p[200];
double v[200];

vector<vector<int>> g;
vector<int> match;
vector<bool> used;

bool dfs(int v) {
  used[v] = true;
  for (int u : g[v]) {
    int w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int solve() {
  int res = 0;
  match.assign(n + m, -1);
  for (int v = 0; v < m + n; v++) {
    if (match[v] < 0) {
      used.assign(n + m, false);
      if (dfs(v))
        res++;
    }
  }
  return res;
}

bool check(double x) {
  g.assign(n + m, vector<int>());

  for (int i = 0; i < n; i++) {
    for (int j = n; j < n + m; j++) {
      double dist = abs(p[i] - p[j]);
      if (dist <= x * v[i]) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  }

  return solve() == m;
}

int main() {
  while (scanf("%d %d", &n, &m), n) {
    for (int i = 0; i < n + m; i++) {
      double x, y;
      scanf("%lf %lf", &x, &y);
      p[i] = Point(x, y);
      if (i < n)
        scanf("%lf", v + i);
    }

    double l = 0.0, r = 20000.0;
    for (int i = 0; i < 100; i++) {
      double m = (l + r) / 2;
      if (check(m))
        r = m;
      else
        l = m;
    }

    printf("%.20f\\n", l);
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef complex<double> Point;

int n, m;
Point p[100];
double v[100];

vector<vector<int>> g;
vector<int> match;
vector<bool> used;

bool dfs(int v) {
  used[v] = true;
  for (int u : g[v]) {
    int w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int solve() {
  int res = 0;
  match.assign(n + m, -1);
  for (int v = 0; v < m + n; v++) {
    if (match[v] < 0) {
      used.assign(n + m, false);
      if (dfs(v))
        res++;
    }
  }
  return res;
}

bool check(double x) {
  g.assign(n + m, vector<int>());

  for (int i = 0; i < n; i++) {
    for (int j = n; j < n + m; j++) {
      double dist = abs(p[i] - p[j]);
      if (dist <= x * v[i]) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  }

  return solve() == m;
}

int main() {
  while (scanf("%d %d", &n, &m), n) {
    for (int i = 0; i < n + m; i++) {
      double x, y;
      scanf("%lf %lf", &x, &y);
      p[i] = Point(x, y);
      if (i < n)
        scanf("%lf", v + i);
    }

    double l = 0.0, r = 20000.0;
    for (int i = 0; i < 100; i++) {
      double m = (l + r) / 2;
      if (check(m))
        r = m;
      else
        l = m;
    }

    printf("%.20f\\n", l);
  }
}
Problem p01280 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int lcm(int a, int b) { return (a / __gcd(a, b)) * b; }

int N;
int A[25][25];
int ps[] = {13, 17, 23};

int main() {
  while (1) {
    scanf("%d", &N);
    if (N == 0)
      break;
    memset(A, 0, sizeof(A));
    int sz = 1;
    rep(i, N) {
      int D, T;
      scanf("%d%d", &D, &T);
      bool ok = true;
      rep(j, 3) if (ps[j] == D) ok = false;
      if (ok)
        sz = lcm(sz, D);
      rep(j, D) {
        int B;
        scanf("%d", &B);
        A[D][(j - T + D) % D] += B;
      }
    }
    vector<int> sum(sz, 0);
    int res = 0;
    rep(i, 3) res += *max_element(A[ps[i]], A[ps[i]] + 25);
    repl(i, 1, 25) {
      bool ok = true;
      rep(j, 3) if (ps[j] == i) ok = false;
      if (!ok)
        continue;
      rep(j, sz) sum[j] += A[i][j % i];
    }
    res += *max_element(all(sum));
    printf("%d\\n", res);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007

int N;
int A[25][25];
int sum[1053360];
int ps[] = {13, 17, 23};

int main() {
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    memset(sum, 0, sizeof(sum));
    memset(A, 0, sizeof(A));
    rep(i, N) {
      int D, T;
      scanf("%d%d", &D, &T);
      rep(j, D) {
        int B;
        scanf("%d", &B);
        A[D][(j + T) % D] += B;
      }
    }
    int res = 0;
    rep(i, 3) res += *max_element(A[ps[i]], A[ps[i]] + 33);
    repl(i, 1, 25) {
      bool ok = true;
      rep(j, 3) if (ps[j] == i) ok = false;
      if (!ok)
        continue;
      rep(j, 1053360) sum[j] += A[i][j % i];
    }
    res += *max_element(sum, sum + 1053360);
    printf("%d\\n", res);
  }
  return 0;
}
Problem p01280 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

vector<int> vec = {8, 9, 5, 7, 11};
int solve(int n) {
  vector<int> d(n);
  vector<vector<int>> q(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i];
    q[i].resize(d[i]);
    int t;
    cin >> t;
    for (int j = 0; j < d[i]; j++) {
      cin >> q[i][(j + (d[i] - t)) % d[i]];
    }
  }
  vector<vector<int>> gcdv(25, vector<int>(25, 0));
  for (int i = 0; i < 25; i++) {
    for (int j = 0; j < 25; j++) {
      if (i != 0 && j != 0)
        gcdv[i][j] = __gcd(i, j);
    }
  }
  vector<vector<int>> tq(24);
  for (int i = 0; i < 24; i++) {
    tq[i].assign(i + 1, 0);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < d[i]; j++)
      tq[d[i] - 1][j] += q[i][j];
  }
  int cnt = 0;
  auto calc = [&](vector<int> v) {
    int res = 0;
    for (int i = 0; i < 24; i++) {
      int sc = -1e9;
      for (int j = 0; j < i + 1; j++) {
        bool isok = true;
        for (int k = 0; k < vec.size(); k++) {
          int g = gcdv[vec[k]][i + 1];
          isok = isok && (v[k] % g == j % g);
          cnt++;
        }
        if (isok)
          sc = max(sc, tq[i][j]);
      }
      res += sc;
    }
    return res;
  };
  function<int(int, vector<int>)> dfs = [&](int idx, vector<int> v) {
    if (idx == vec.size())
      return calc(v);
    int res = 0;
    for (int i = 0; i < vec[idx]; i++) {
      v[idx] = i;
      res = max(res, dfs(idx + 1, v));
    }
    return res;
  };

  vector<int> tmp(vec.size());
  int ret = dfs(0, tmp);
  return ret;
}
int main() {
  int n;
  while (cin >> n, n) {
    cout << solve(n) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

vector<int> vec = {16, 9, 5, 7, 11};
int solve(int n) {
  vector<int> d(n);
  vector<vector<int>> q(n);
  for (int i = 0; i < n; i++) {
    cin >> d[i];
    q[i].resize(d[i]);
    int t;
    cin >> t;
    for (int j = 0; j < d[i]; j++) {
      cin >> q[i][(j + (d[i] - t)) % d[i]];
    }
  }
  auto calc = [&](vector<int> v) {
    int res = 0;
    for (int i = 0; i < n; i++) {
      int sc = -1e9;
      for (int j = 0; j < d[i]; j++) {
        bool isok = true;
        for (int k = 0; k < vec.size(); k++) {
          int g = __gcd(vec[k], d[i]);
          isok = isok && v[k] % g == j % g;
        }
        if (isok)
          sc = max(sc, q[i][j]);
      }
      res += sc;
    }
    return res;
  };
  function<int(int, vector<int>)> dfs = [&](int idx, vector<int> v) {
    if (idx == vec.size())
      return calc(v);
    int res = 0;
    for (int i = 0; i < vec[idx]; i++) {
      v[idx] = i;
      res = max(res, dfs(idx + 1, v));
    }
    return res;
  };
  vector<int> tmp(vec.size());
  return dfs(0, tmp);
}
int main() {
  int n;
  while (cin >> n, n) {
    cout << solve(n) << endl;
  }
  return 0;
}
Problem p01280 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int N;
int s[25][25];
int t[60000];
int u[25];
int lc;
bool flg[100];

int main() {
  flg[1] = flg[13] = flg[17] = flg[19] = flg[23] = true;

  while (1) {
    cin >> N;
    if (N == 0)
      break;
    memset(s, 0, sizeof(s));
    memset(t, 0, sizeof(t));
    memset(u, 0, sizeof(u));
    lc = 1;
    for (int i = 0; i < N; i++) {
      int a, b, c;
      cin >> a >> b;
      if (!flg[a])
        lc = lc / __gcd(lc, a) * a;
      for (int j = 0; j < a; j++) {
        cin >> c;
        s[a][(j - b % a + a) % a] += c;
        if (flg[a])
          u[a] = max(u[a], s[a][(j - b % a + a) % a]);
      }
    }
    int ans = 0, ans2 = 0;
    for (int i = 1; i <= 24; i++) {
      if (flg[i])
        ans += u[i];
      else
        for (int j = 0; j < lc; j++)
          t[j] += s[i][j % i];
    }
    for (int i = 0; i < lc; i++)
      ans2 = max(ans2, t[i]);
    cout << ans + ans2 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N;
int s[24][24];
int t[60000];
int u[24];
int lc;
bool flg[100];

int main() {
  flg[1] = flg[13] = flg[17] = flg[19] = flg[23] = true;

  while (1) {
    cin >> N;
    if (N == 0)
      break;
    memset(s, 0, sizeof(s));
    memset(t, 0, sizeof(t));
    memset(u, 0, sizeof(u));
    lc = 1;
    for (int i = 0; i < N; i++) {
      int a, b, c;
      cin >> a >> b;
      if (!flg[a])
        lc = lc / __gcd(lc, a) * a;
      for (int j = 0; j < a; j++) {
        cin >> c;
        s[a][(j - b % a + a) % a] += c;
        if (flg[a])
          u[a] = max(u[a], c);
      }
    }
    int ans = 0, ans2 = 0;
    for (int i = 2; i < 24; i++) {
      if (flg[i])
        ans += u[i];
      else
        for (int j = 0; j < lc; j++)
          t[j] += s[i][j % i];
    }
    for (int i = 0; i < lc; i++)
      ans2 = max(ans2, t[i]);
    cout << ans + ans2 << endl;
  }
  return 0;
}
Problem p01281 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <array>
#include <iostream>
#include <map>

using namespace std;

int main() {
  for (int H, W; cin >> H >> W, H;) {
    map<array<int, 20>, int> m[21][21];
    m[0][0][array<int, 20>()] = 1;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        for (auto e : m[i][j]) {
          auto v = e.first;
          auto d = e.second;
          if (j && v[j - 1] == 2) {
            v[j] = 3;
            m[i][j + 1][v] += d;
          } else if (i && v[j] == 0) {
            v[j] = 1;
            m[i][j + 1][v] += d;
          } else if (i == 0 || v[j] == 3 || j == 0 || v[j - 1] == 1) {
            if (i + 1 < H) {
              auto nv = v;
              nv[j] = 0;
              m[i][j + 1][nv] += d;
            }
            if (j + 1 < W && (i == 0 || v[j + 1] != 0)) {
              auto nv = v;
              nv[j] = 2;
              m[i][j + 1][nv] += d;
            }
          }
        }
        m[i][j].clear();
      }
      m[i + 1][0] = m[i][W];
    }
    int a = 0;
    for (auto e : m[H][0]) {
      a += e.second;
    }
    cout << a << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main() {
  for (int H, W; cin >> H >> W, H;) {
    map<vector<char>, int> m[21][21];
    m[0][0][vector<char>(W)] = 1;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        for (auto e : m[i][j]) {
          auto v = e.first;
          auto d = e.second;
          if (j && v[j - 1] == 2) {
            v[j] = 3;
            m[i][j + 1][v] += d;
          } else if (i && v[j] == 0) {
            v[j] = 1;
            m[i][j + 1][v] += d;
          } else if (i == 0 || v[j] == 3 || j == 0 || v[j - 1] == 1) {
            if (i + 1 < H) {
              auto nv = v;
              nv[j] = 0;
              m[i][j + 1][nv] += d;
            }
            if (j + 1 < W && (i == 0 || v[j + 1] != 0)) {
              auto nv = v;
              nv[j] = 2;
              m[i][j + 1][nv] += d;
            }
          }
        }
        m[i][j].clear();
      }
      m[i + 1][0] = m[i][W];
    }
    int a = 0;
    for (auto e : m[H][0]) {
      a += e.second;
    }
    cout << a << endl;
  }
}
Problem p01281 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
int h, w;
int dfs(vvi &f, int x, int i);
vi flags;
bool check(const vvi &f, int k, int l) {
  for (int i = max(k - 1, 0); i + 1 < h && i <= k + 1; i++) {
    for (int j = max(l - 1, 0); j + 1 < w && j <= l + 1; j++) {
      int zcnt = 0;
      set<int> s;
      for (int di = 0; di < 2; di++) {
        for (int dj = 0; dj < 2; dj++) {
          if (f[i + di][j + dj] != 0)
            s.insert(f[i + di][j + dj]);
        }
      }
      if (zcnt + s.size() == 4)
        return false;
    }
  }
  return true;
}
int impl(vvi &f, int i, int j, int x) {
  int res = 0;
  int k = 1;
  if (0 <= i + k - 1 && i + k < h && f[i + k - 1][j] == f[i + k][j]) {
    f[i + k - 1][j] = f[i + k][j] = x;
    if (check(f, i, j))
      res += dfs(f, x + 1, i);
    f[i + k - 1][j] = f[i + k][j] = 0;
  }
  if (0 <= j + k - 1 && j + k < w && f[i][j + k - 1] == f[i][j + k]) {
    f[i][j + k - 1] = f[i][j + k] = x;
    if (check(f, i, j))
      res += dfs(f, x + 1, i);
    f[i][j + k - 1] = f[i][j + k] = 0;
  }
  return res;
}
int dfs(vvi &f, int x, int i) {
  if (i == h)
    return 1;
  for (int j = 0; j < w; j++) {
    if (!f[i][j]) {
      return impl(f, i, j, x);
    }
  }
  return dfs(f, x, i + 1);
}
int solve() {
  flags.assign(h * w, 0);
  vvi tmpf(h, vi(w, 0));
  return dfs(tmpf, 1, 0);
}

int main() {
  while (cin >> h >> w, h) {
    cout << solve() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using vi = vector<int>;
using vvi = vector<vi>;
int h, w;
int dfs(vvi f, int x);
bool check(vvi f) {
  for (int i = 0; i + 1 < h; i++) {
    for (int j = 0; j + 1 < w; j++) {
      set<int> s;
      for (int di = 0; di < 2; di++) {
        for (int dj = 0; dj < 2; dj++) {
          if (f[i + di][j + dj])
            s.insert(f[i + di][j + dj]);
        }
      }
      if (s.size() == 4)
        return false;
    }
  }
  return true;
}
int impl(vvi f, int i, int j, int x) {
  int res = 0;
  for (int k = 0; k < 2; k++) {
    if (0 <= i + k - 1 && i + k < h && f[i + k - 1][j] == f[i + k][j]) {
      f[i + k - 1][j] = f[i + k][j] = x;
      if (check(f))
        res += dfs(f, x + 1);
      f[i + k - 1][j] = f[i + k][j] = 0;
    }
    if (0 <= j + k - 1 && j + k < w && f[i][j + k - 1] == f[i][j + k]) {
      f[i][j + k - 1] = f[i][j + k] = x;
      if (check(f))
        res += dfs(f, x + 1);
      f[i][j + k - 1] = f[i][j + k] = 0;
    }
  }
  return res;
}
int dfs(vvi f, int x) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!f[i][j]) {
        return impl(f, i, j, x);
      }
    }
  }
  return 1;
}
int solve() {
  vvi tmpf(h, vi(w, 0));
  return dfs(tmpf, 1);
}

int main() {
  while (cin >> h >> w, h) {
    cout << solve() << endl;
  }
  return 0;
}