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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
int k;
int q[200] = {0};
int a, b;
int ans = 0;
int mem[110][5][5] = {{{0}}};
int check(int x, int y, int z); // x日目,前日y,前々日z
int main(void) {
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    cin >> a;
    cin >> b;
    q[a - 1] = b;
  }

  cout << check(0, 0, 0) << endl;
  return 0;
}
int check(int x, int y, int z) {
  if (mem[x][y][z] != 0)
    return mem[x][y][z];

  if (x == n) {

    return 1;
  }
  if (q[x] != 0) {
    if (q[x] == 1) {
      if (y != 1 || z != 1) {
        return check(x + 1, 1, y);
      } else
        return 0;
    } else if (q[x] == 2) {
      if (y != 2 || z != 2) {
        return check(x + 1, 2, y);
      } else
        return 0;
    } else if (q[x] == 3) {
      if (y != 3 || z != 3) {
        return check(x + 1, 3, y);
      } else
        return 0;
    }

  } else {

    if (y != z || y == 0) {
      int a = check(x + 1, 1, y) + check(x + 1, 2, y) + check(x + 1, 3, y);
      a %= 10000;
      mem[x][y][z] = a;
      return a;

    } else if (y == 1) {

      int a = check(x + 1, 2, y) + check(x + 1, 3, y);
      a %= 10000;
      mem[x][y][z] = a;
      return a;
    } else if (y == 2) {

      int a = check(x + 1, 1, y) + check(x + 1, 3, y);
      a %= 10000;
      return a;
    } else if (y == 3) {

      int a = check(x + 1, 1, y) + check(x + 1, 2, y);
      a %= 10000;
      mem[x][y][z] = a;
      return a;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
int k;
int q[200] = {0};
int a, b;
int ans = 0;
int check(int x, int y, int z); // x日目,前日y,前々日z
int main(void) {
  cin >> n >> k;

  for (int i = 0; i < k; i++) {
    cin >> a;
    cin >> b;
    q[a - 1] = b;
  }

  cout << check(0, 0, 0) << endl;
  return 0;
}
int check(int x, int y, int z) {

  if (x == n) {

    return 1;
  }
  if (q[x] != 0) {
    if (q[x] == 1) {
      if (y != 1 || z != 1) {
        return check(x + 1, 1, y);
      } else
        return 0;
    } else if (q[x] == 2) {
      if (y != 2 || z != 2) {
        return check(x + 1, 2, y);
      } else
        return 0;
    } else if (q[x] == 3) {
      if (y != 3 || z != 3) {
        return check(x + 1, 3, y);
      } else
        return 0;
    }

  } else {

    if (y != z || y == 0) {
      int a = check(x + 1, 1, y) + check(x + 1, 2, y) + check(x + 1, 3, y);
      a %= 10000;
      return a;

    } else if (y == 1) {

      int a = check(x + 1, 2, y) + check(x + 1, 3, y);
      a %= 10000;
      return a;
    } else if (y == 2) {

      int a = check(x + 1, 1, y) + check(x + 1, 3, y);
      a %= 10000;
      return a;
    } else if (y == 3) {

      int a = check(x + 1, 1, y) + check(x + 1, 2, y);
      a %= 10000;
      return a;
    }
  }
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int N, K;

  cin >> N >> K;

  int dp[3][3][N];
  int menu[N];

  for (int i = 0; i < N; ++i) {
    menu[i] = -1;
  }

  for (int i = 0; i < K; ++i) {
    int a, b;
    cin >> a >> b;
    menu[a - 1] = b - 1;
  }

  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      dp[i][j][1] = 0;
    }
  }

  if (menu[0] != -1 && menu[1] != -1) {
    dp[menu[0]][menu[1]][1] = 1;
  } else if (menu[0] != -1) {
    dp[menu[0]][0][1] = 1;
    dp[menu[0]][1][1] = 1;
    dp[menu[0]][2][1] = 1;
  } else if (menu[1] != -1) {
    dp[0][menu[1]][1] = 1;
    dp[1][menu[1]][1] = 1;
    dp[2][menu[1]][1] = 1;
  } else {
    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < 3; ++j) {
        dp[i][j][1] = 1;
      }
    }
  }

  for (int d = 2; d < N; ++d) {
    if (menu[d] != -1) {
      for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
          if (j != menu[d]) {
            dp[i][j][d] = 0;
          } else {
            dp[i][j][d] = dp[0][i][d - 1] + dp[1][i][d - 1] + dp[2][i][d - 1];
            if (i == j)
              dp[i][j][d] += 10000 - dp[i][i][d - 1];
            dp[i][j][d] %= 10000;
          }
        }
      }
    } else {
      for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
          dp[i][j][d] = dp[0][i][d - 1] + dp[1][i][d - 1] + dp[2][i][d - 1];
          if (i == j)
            dp[i][j][d] += 10000 - dp[i][i][d - 1];
          dp[i][j][d] %= 10000;
        }
      }
    }
  }

  int ans = 0;

  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      ans += dp[i][j][N - 1];
      ans %= 10000;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int dfs(vector<int> &menu, int now) {
  int ans = 0;

  if (now == (int)menu.size()) {
    return 1;
  }

  if (menu[now] == 0) {
    for (int c = 1; c <= 3; ++c) {
      if (now >= 2 && menu[now - 2] == c && menu[now - 1] == c)
        continue;
      if (now >= 1 && now <= (int)menu.size() - 2 && menu[now - 1] == c &&
          menu[now + 1] == c)
        continue;
      if (now <= (int)menu.size() - 3 && menu[now + 1] == c &&
          menu[now + 2] == c)
        continue;
      menu[now] = c;
      ans = (ans + dfs(menu, now + 1)) % 10000;
      menu[now] = 0;
    }
  } else {
    ans = (ans + dfs(menu, now + 1)) % 10000;
  }

  return ans;
}

int main() {
  int N, K;
  vector<int> menu;

  cin >> N >> K;

  menu.resize(N);

  fill(menu.begin(), menu.end(), 0);

  for (int i = 0; i < K; ++i) {
    int a, b;
    cin >> a >> b;
    menu[a - 1] = b;
  }

  cout << dfs(menu, 0) << endl;

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

#include <iostream>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, m, n) for (int i = m; i < n; i++)

int main() {
  int n, k;
  std::cin >> n >> k;

  int date[100];
  int dp[100][4][4];
  rep(i, 100) {
    date[i] = 0;
    rep(a, 4) rep(b, 4) dp[i][a][b] = 0;
  }

  int d, p;
  rep(i, k) {
    std::cin >> d >> p;
    date[d - 1] = p;
  }

  if (date[0] == 0)
    dp[0][0][1] = dp[0][0][2] = dp[0][0][3] = 1;
  else
    dp[0][0][date[0]] = 1;
  repr(i, 1, n) {
    if (date[i] == 0) {
      repr(p, 1, 4) rep(a, 4) rep(b, 4) if (a != b || b != p) dp[i][a][p] +=
          dp[i - 1][b][a] % 10000;
    } else {
      rep(a, 4) rep(b, 4) if (a != b || b != date[i]) dp[i][a][date[i]] +=
          dp[i - 1][b][a] % 10000;
    }
  }
  int total = 0;
  rep(a, 4) rep(b, 4) total += dp[n - 1][a][b];
  std::cout << total % 10000 << std::endl;
}

Error: TLE

Fixed submission:

#include <iostream>

#define rep(i, n) for (int i = 0; i < n; i++)

int date[100]; // ignore after date[n]
int func(int ind, int n) {
  while (date[++ind])
    ;
  if (ind == n)
    return 1;

  int total = 0;
  for (int p = 1; p <= 3; p++) {
    if (ind != 0 && ind != 1 && date[ind - 2] == p && date[ind - 1] == p)
      continue;
    if (ind != 0 && ind != n - 1 && date[ind - 1] == p && date[ind + 1] == p)
      continue;
    if (ind != n - 1 && ind != n - 2 && date[ind + 1] == p &&
        date[ind + 2] == p)
      continue;
    date[ind] = p;

    total += func(ind, n);
  }
  date[ind] = 0;
  return total;
}

int main() {
  int n, k;
  std::cin >> n >> k;

  rep(i, 100) date[i] = 0;
  int d, p;
  rep(i, k) {
    std::cin >> d >> p;
    date[d - 1] = p;
  }
  std::cout << func(-1, n) % 10000 << std::endl;
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>

using namespace std;
#define mod 10000
#define MAX_N (100 + 1)
int n, k;
int sche[MAX_N];

int dp[MAX_N][4][4];
int cnt(int day, int one, int two) {
  if (day == n)
    return 1;
  if (dp[day][one][two] >= 0)
    return dp[day][one][two];
  if (sche[day] > 0) {
    if (one == two && two == sche[day])
      return dp[day][one][two] = 0;
    return dp[day][one][two] = cnt(day + 1, sche[day], one);
  }
  int ret = 0;
  for (int i = 1; i <= 3; i++) {
    if (one == two && two == i)
      continue;
    ret += cnt(day + 1, i, one);
    ret %= mod;
  }
  return dp[day][one][two] = ret % mod;
}

int main() {
  cin >> n >> k;
  memset(sche, -1, sizeof(sche));
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int d, p;
    cin >> d >> p;
    d--;
    sche[d] = p;
  }
  //    for (int i = 0; i < n; i++) cout << sche[i] << ' ';cout << endl;
  cout << cnt(0, 0, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>

using namespace std;
#define mod 10000
#define MAX_N (100 + 1)
int n, k;
int sche[MAX_N];

int cnt(int day, int one, int two) {
  if (day == n)
    return 1;
  if (sche[day] > 0) {
    if (one == two && two == sche[day])
      return 0;
    return cnt(day + 1, sche[day], one);
  }
  int ret = 0;
  for (int i = 1; i <= 3; i++) {
    if (one == two && two == i)
      continue;
    ret += cnt(day + 1, i, one);
    ret %= mod;
  }
  return ret % mod;
}

int main() {
  cin >> n >> k;
  memset(sche, -1, sizeof(sche));
  for (int i = 0; i < k; i++) {
    int d, p;
    cin >> d >> p;
    d--;
    sche[d] = p;
  }
  //    for (int i = 0; i < n; i++) cout << sche[i] << ' ';cout << endl;
  cout << cnt(0, -1, -1) << endl;
  return 0;
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define rept(X, Y, Z) for (int(X) = (Y); (X) < (Z); ++(X))
#define MOD 10000

int N, K, tmp[2];
int memo[101][4][4];
int def[101];

int func(int d, int op, int tp) {
  if (memo[d][op][tp] != -1)
    return memo[d][op][tp];

  int sum = 0;

  if (d == N)
    return 1;

  rept(i, 1, 4) {
    if (def[d] != 0 && def[d] != i || op == tp && i == op)
      continue;

    sum += func(d + 1, i, op) % MOD;
  }

  return memo[d][op][tp] = sum % MOD;
}

int main() {
  memset(memo, -1, sizeof(memo));

  scanf("%d%d", &N, &K);

  rep(i, K) {
    scanf("%d%d", tmp, tmp + 1);

    def[tmp[0] - 1] = tmp[1];
  }

  std::cout << func(0, 0, 0) % MOD << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define rept(X, Y, Z) for (int(X) = (Y); (X) < (Z); ++(X))
#define MOD 10000

int N, K, tmp[2];
int memo[101][4][4];
int def[101];

int func(int d, int op, int tp) {
  int sum = 0;

  if (d == N)
    return 1;

  rept(i, 1, 4) {
    if (def[d] != 0 && def[d] != i || op == tp && i == op)
      continue;

    sum += func(d + 1, i, op) % MOD;
  }

  return sum % MOD;
}

int main() {
  scanf("%d%d", &N, &K);

  rep(i, K) {
    scanf("%d%d", tmp, tmp + 1);

    def[tmp[0] - 1] = tmp[1];
  }

  std::cout << func(0, 0, 0) % MOD << std::endl;

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

int n, k, sc[100], dp[100][100][3];

int ans(int N, int K, int flag) {
  if (dp[N][K][flag] > 0)
    return dp[N][K][flag];
  if (flag == 2)
    return 0;
  long long int ret = 0;
  if (K != sc[N] && sc[N] >= 0)
    return 0;
  if (N == n - 1)
    return 1;
  for (int i = 0; i < 3; i++) {
    if (i == K)
      ret += ans(N + 1, i, flag + 1);
    else
      ret += ans(N + 1, i, 0);
  }
  return dp[N][K][flag] = ret % 10000;
}

int main() {
  int answer = 0;
  for (int i = 0; i < 100; i++) {
    sc[i] = -1;
  }
  scanf("%d %d", &n, &k);
  for (int i = 0; i < k; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    sc[a - 1] = b - 1;
  }
  for (int i = 0; i < 3; i++) {
    answer += ans(0, i, 0);
    answer %= 10000;
  }
  printf("%d\\n", answer);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>

using namespace std;

int n, k, sc[100] = {}, dp[100][100][3];

int ans(int N, int K, int flag) {
  if (flag == 2)
    return 0;
  int ret = 0;
  if (K != sc[N] && sc[N] >= 0)
    return 0;
  if (N == n - 1)
    return 1;
  for (int i = 0; i < 3; i++) {
    if (i == K)
      ret += ans(N + 1, i, flag + 1);
    else
      ret += ans(N + 1, i, 0);
  }
  return dp[N][K][flag] = ret % 10000;
}

int main() {
  int answer = 0;
  for (int i = 0; i < 100; i++) {
    sc[i] = -1;
  }
  scanf("%d %d", &n, &k);
  for (int i = 0; i < k; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    sc[a - 1] = b - 1;
  }
  for (int i = 0; i < 3; i++) {
    answer += ans(0, i, 0);
    answer %= 10000;
  }
  printf("%d\\n", answer);
  return 0;
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int k, a, b, day[101], n, memo[101][4][4];
int dfs(int j, int d1, int d2);
int main() {
  cin >> n >> k;
  fill(day, day + 101, 0);
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    day[a] = b;
  }
  cout << dfs(1, 0, 0) << endl;
  return (0);
}

int dfs(int j, int d1, int d2) {
  int ret = 0;
  if (j == n + 1)
    return 1;
  if (memo[j][d1][d2] != -1)
    return memo[j][d1][d2];
  else if (day[j] != 0) {
    if (d1 != d2 || d2 != day[j])
      ret += dfs(j + 1, d2, day[j]);
  } else {
    for (int i = 1; i <= 3; i++) {
      if (d1 != d2 || d2 != i)
        ret += dfs(j + 1, d2, i);
    }
  }
  return memo[j][d1][d2] = ret % 10000;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, k, a, b, day[101], ans;
void dfs(int j, int d1, int d2);
int main() {

  cin >> n >> k;
  fill(day, day + 101, 0);
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    day[a] = b;
  }
  ans = 0;
  dfs(1, 0, 0);
  cout << ans << endl;
  return (0);
}

void dfs(int j, int d1, int d2) {
  if (j == n + 1)
    ans = (ans + 1) % 10000;
  else if (day[j] != 0) {
    if (d1 != d2 || d2 != day[j])
      dfs(j + 1, d2, day[j]);
  } else {
    for (int i = 1; i <= 3; i++) {
      if (d1 != d2 || d2 != i)
        dfs(j + 1, d2, i);
    }
  }
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <array>
#include <iostream>
#include <numeric>
#include <vector>

int f(int, int, int);
int N;
int yotei[100];
int dp[111][4][4];

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int k, date;
  std::cin >> N >> k;
  for (int i = 0; i < k; i++) {
    std::cin >> date;
    std::cin >> yotei[date - 1];
  }

  for (int i = 0; i < 111; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = -1;

  std::cout << f(0, 0, 0) << "\\n";
  return 0;
}

int f(int day, int kinou, int ototui) {
  if (day == N)
    return 1;
  if (dp[day][kinou][ototui] >= 0)
    return dp[day][kinou][ototui];
  int ans = 0;

  for (int i = 1; i <= 3; i++) {
    if ((i == kinou && i == ototui) || (yotei[day] != 0 && i != yotei[day]))
      continue;

    ans += f(day + 1, i, kinou);
  }

  dp[day][kinou][ototui] = ans % 10000;
  return ans % 10000;
}

Error: TLE

Fixed submission:

#include <array>
#include <iostream>
#include <numeric>
#include <vector>

int f(int, int, int);
int N;
int yotei[100];

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int k, date;
  std::cin >> N >> k;
  for (int i = 0; i < k; i++) {
    std::cin >> date;
    std::cin >> yotei[date - 1];
  }

  std::cout << f(0, 0, 0) << "\\n";
  return 0;
}

int f(int day, int kinou, int ototui) {
  if (day == N)
    return 1;
  int ans = 0;

  for (int i = 1; i <= 3; i++) {
    if ((i == kinou && i == ototui) || (yotei[day] != 0 && i != yotei[day]))
      continue;

    ans += f(day + 1, i, kinou);
  }

  return ans % 10000;
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int n, k, p[110], dp[110][4][4]; // day,plan|1 origin!

int s(int now, int typ, int ren) { // now>>n日目まで、同種類ren日連続
  if (ren == 3)
    return 0;
  if (now == n)
    return 1;
  if (dp[now][typ][ren] != -1)
    return dp[now][typ][ren];
  if (p[now] != -1) {
    if (typ == p[now])
      dp[now][typ][ren] = s(now + 1, typ, ren + 1);
    else {
      dp[now][typ][ren] = s(now + 1, p[now], 1);
    }
  } else {
    dp[now][typ][ren] = s(now + 1, typ, ren + 1) +
                        s(now + 1, (typ + 1) % 3, 1) +
                        s(now + 1, (typ + 2) % 3, 1);
  }
  dp[now][typ][ren] %= 10000;
  return dp[now][typ][ren];
}

int main() {
  cin >> n >> k;

  for (int i = 0; i <= n; i++) {
    p[i] = -1;
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = -1;
    }
  }

  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    p[a - 1] = b % 3;
  }

  cout << s(0, 0, 0) % 10000 << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
// K[1-]
int n, k;
int K[101] = {};
int ans = 0;

void sol(int now, int yest, int dob) {

  if (dob == 2) {
    return;
  }

  if (now == n + 1) {
    ans++;
    ans %= 10000;
    return;
  }

  if (K[now] != -1) {
    if (yest == K[now]) {
      sol(now + 1, K[now], dob + 1);
      return;
    } else {
      sol(now + 1, K[now], 0);
      return;
    }
  }

  else {
    if (now != 1) {
      sol(now + 1, yest % 3, dob + 1);
    } else {
      sol(now + 1, yest % 3, 0);
    }

    sol(now + 1, (yest + 1) % 3, 0);
    sol(now + 1, (yest + 2) % 3, 0);
    return;
  }
}

int main() {

  cin >> n >> k;

  for (int i = 0; i < n + 1; i++) {
    K[i] = -1;
  }

  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    K[a] = b % 3;
  }

  sol(1, -1, 0);

  cout << ans % 10000 << endl;

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

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

int dp[101][4][4]; // 何日目から、どのぱすたを、何日連続で食べているか
int plan[101]; // 先に決められている予定　要素：日付

#define MOD 10000;

int main(void) {
  int day;
  int k;
  cin >> day >> k;
  memset(plan, 0, sizeof(plan));
  for (int i = 0; i < k; ++i) {
    int d, p;
    cin >> d >> p;
    plan[d] = p;
  }
  memset(dp, 0, sizeof(dp));

  for (int i = 1; i <= 3; ++i) {
    if (plan[1] != 0)
      dp[1][plan[1]][1] = 1;
    else
      dp[1][i][1] = 1;
  }

  for (int d = 2; d <= day; ++d) { // どの日から？
    for (int p = 1; p <= 3; ++p) { // どのパスタを？
      if (plan[d]) {
        dp[d][plan[d]][1] =
            (dp[d - 1][(plan[d] + 1) % 3 + 1][1] +
             dp[d - 1][(plan[d] + 1) % 3 + 1][2] +
             dp[d - 1][plan[d] % 3 + 1][1] + dp[d - 1][plan[d] % 3 + 1][2]) %
            MOD;
        dp[d][plan[d]][2] = dp[d - 1][plan[d]][1] % MOD;
      } else {
        dp[d][p][1] =
            (dp[d - 1][(p + 1) % 3 + 1][1] + dp[d - 1][(p + 1) % 3 + 1][2] +
             dp[d - 1][p % 3 + 1][1] + dp[d - 1][p % 3 + 1][2]) %
            MOD;
        dp[d][p][2] = dp[d - 1][p][1] % MOD;
      }
    }
  }
  int ans = (dp[day][1][1] + dp[day][2][1] + dp[day][3][1] + dp[day][1][2] +
             dp[day][2][2] + dp[day][3][2]) %
            MOD;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int dp[101][4][4]; // 何日目から、どのぱすたを、何日連続で食べているか
int plan[101]; // 先に決められている予定　要素：日付

#define MOD 10000;

int main(void) {
  while (1) {
    int day;
    int k;
    cin >> day >> k;
    memset(plan, 0, sizeof(plan));
    for (int i = 0; i < k; ++i) {
      int d, p;
      cin >> d >> p;
      plan[d] = p;
    }
    memset(dp, 0, sizeof(dp));

    for (int i = 1; i <= 3; ++i) {
      if (plan[1] != 0)
        dp[1][plan[1]][1] = 1;
      else
        dp[1][i][1] = 1;
    }

    for (int d = 2; d <= day; ++d) { // どの日から？
      for (int p = 1; p <= 3; ++p) { // どのパスタを？
        if (plan[d]) {
          dp[d][plan[d]][1] =
              (dp[d - 1][(plan[d] + 1) % 3 + 1][1] +
               dp[d - 1][(plan[d] + 1) % 3 + 1][2] +
               dp[d - 1][plan[d] % 3 + 1][1] + dp[d - 1][plan[d] % 3 + 1][2]) %
              MOD;
          dp[d][plan[d]][2] = dp[d - 1][plan[d]][1] % MOD;
        } else {
          dp[d][p][1] =
              (dp[d - 1][(p + 1) % 3 + 1][1] + dp[d - 1][(p + 1) % 3 + 1][2] +
               dp[d - 1][p % 3 + 1][1] + dp[d - 1][p % 3 + 1][2]) %
              MOD;
          dp[d][p][2] = dp[d - 1][p][1] % MOD;
        }
      }
    }
    int ans = dp[day][1][1] + dp[day][2][1] + dp[day][3][1] + dp[day][1][2] +
              dp[day][2][2] + dp[day][3][2];
    cout << ans << endl;
  }
  return 0;
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N, K, A, B;

int dfs(vector<vector<vector<int>>> &dp, vector<bool> &dec, vector<int> &pasta,
        int pppasta, int ppasta, int n) {
  if (n == N + 1)
    return 1;
  if (dp[pppasta][ppasta][n] != -1) {
    return dp[pppasta][ppasta][n];
  }
  dp[pppasta][ppasta][n] = 0;
  if (dec[n]) {
    if (n - 2 >= 0 && pppasta == ppasta && ppasta == pasta[n])
      return 0;
    else
      return dp[pppasta][ppasta][n] =
                 dfs(dp, dec, pasta, ppasta, pasta[n], n + 1);
  }
  for (int i = 1; i <= 3; i++) {
    pasta[n] = i;
    if (n - 2 >= 0 && pppasta == ppasta && ppasta == pasta[n])
      continue;
    dp[pppasta][ppasta][n] += dfs(dp, dec, pasta, ppasta, pasta[n], n + 1);
  }
  dp[pppasta][ppasta][n] %= 10000;
  return dp[pppasta][ppasta][n];
}

int main() {
  cin >> N >> K;
  vector<bool> dec(N + 1, false);
  vector<int> pasta(N + 1, 0);
  vector<vector<vector<int>>> dp(
      4, vector<vector<int>>(4, vector<int>(N + 1, -1)));
  for (int i = 0; i < K; i++) {
    cin >> A >> B;
    dec[A] = true;
    pasta[A] = B;
  }
  cout << dfs(dp, dec, pasta, 0, 0, 1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int N, K, A, B;

int dfs(vector<bool> &dec, vector<int> &pasta, int n) {
  int res = 0;
  if (n == N + 1)
    return 1;
  if (dec[n]) {
    if (n - 2 >= 0 && pasta[n - 2] == pasta[n - 1] && pasta[n - 1] == pasta[n])
      return 0;
    else
      return dfs(dec, pasta, n + 1);
  }
  for (int i = 1; i <= 3; i++) {
    pasta[n] = i;
    if (n - 2 >= 0 && pasta[n - 2] == pasta[n - 1] && pasta[n - 1] == pasta[n])
      continue;
    res += dfs(dec, pasta, n + 1);
  }
  return res;
}

int main() {
  cin >> N >> K;
  vector<bool> dec(N + 1);
  vector<int> pasta(N + 1);
  for (int i = 0; i < K; i++) {
    cin >> A >> B;
    dec[A] = 1;
    pasta[A] = B;
  }
  cout << dfs(dec, pasta, 1) % 10000 << endl;
  return 0;
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int N, K, mem[100][4][4], decided[100];

int dp(int n, int b1, int b2) {
  if (n == N)
    return 1;
  if (mem[n][b1][b2] != -1)
    return mem[n][b1][b2];
  int ans = 0;
  if (decided[n] != 0) {
    if (b1 == b2 && decided[n] == b1)
      return 0;
    mem[n][b1][b2] = dp(n + 1, decided[n], b1);
    return mem[n][b1][b2];
  }
  for (int i = 1; i <= 3; i++) {
    if (b1 == b2 && b1 == i)
      continue;
    ans += dp(n + 1, i, b1);
  }
  mem[n][b1][b2] = ans % 10000;
  return ans % 10000;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        mem[i][j][k] = -1;
  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    decided[a - 1] = b;
  }
  cout << dp(0, 0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int N, K, mem[100][3][3], decided[100];

int dp(int n, int b1, int b2) {
  if (n == N)
    return 1;
  int ans = 0;
  if (decided[n] != 0) {
    if (b1 == b2 && decided[n] == b1)
      return 0;
    mem[n][b1][b2] = dp(n + 1, decided[n], b1);
    return mem[n][b1][b2];
  }
  for (int i = 1; i <= 3; i++) {
    if (b1 == b2 && b1 == i)
      continue;
    ans += dp(n + 1, i, b1);
  }
  return ans % 10000;
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    decided[a - 1] = b;
  }
  cout << dp(0, 0, 0) << endl;
}