Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

#define f1 for (int x1 = 0; x1 < 3; x1++)
#define f2 for (int x2 = 0; x2 < 3; x2++)
#define f3 for (int x3 = 0; x3 < 3; x3++)

int main() {
  int n, k, a, b, p[100];
  int c1[3][3][3], c2[3][3][3];

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

  for (int i = 0; i < n - 2; i++) {
    if (!i) {
      f1 {
        f2 {
          f3 { c1[x1][x2][x3] = 1; }
        }
      }
    } else {
      int sum = 0;
      f1 {
        f2 {
          f3 { sum += c2[x3][x1][x2]; }
          f3 { c1[x1][x2][x3] = sum; }
          sum = 0;
        }
      }
    }

    f1 { c1[x1][x1][x1] = 0; }

    if (p[i]) {
      f1 {
        f2 {
          f3 {
            if (p[i] != x1 + 1) {
              c1[x1][x2][x3] = 0;
            }
          }
        }
      }
    }
    if (p[i + 1]) {
      f1 {
        f2 {
          f3 {
            if (p[i + 1] != x2 + 1) {
              c1[x1][x2][x3] = 0;
            }
          }
        }
      }
    }
    if (p[i + 2]) {
      f1 {
        f2 {
          f3 {
            if (p[i + 2] != x3 + 1) {
              c1[x1][x2][x3] = 0;
            }
          }
        }
      }
    }

    f1 {
      f2 {
        f3 {
          c2[x1][x2][x3] = c1[x1][x2][x3];
          c2[x1][x2][x3] %= 10000;
        }
      }
    }
  }

  int total = 0;

  f1 {
    f2 {
      f3 { total += c2[x1][x2][x3]; }
    }
  }

  cout << total % 10000 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n, k, a, b, p[100];

int fanc(int i, int x, int y, int z) {
  int c[3][3][3];

  if (i == 0) {
    for (int j1 = 0; j1 < 3; j1++) {
      for (int j2 = 0; j2 < 3; j2++) {
        for (int j3 = 0; j3 < 3; j3++) {
          c[j1][j2][j3] = 1;
        }
      }
    }
  }

  else {
    int sum = 0;
    for (int j1 = 0; j1 < 3; j1++) {
      for (int j2 = 0; j2 < 3; j2++) {
        for (int j3 = 0; j3 < 3; j3++) {
          sum += fanc(i - 1, j3, j1, j2);
        }
        for (int j3 = 0; j3 < 3; j3++) {
          c[j1][j2][j3] = sum;
        }
        sum = 0;
      }
    }
  }

  for (int j1 = 0; j1 < 3; j1++) {
    c[j1][j1][j1] = 0;
  }

  if (p[i]) {
    for (int j1 = 0; j1 < 3; j1++) {
      for (int j2 = 0; j2 < 3; j2++) {
        for (int j3 = 0; j3 < 3; j3++) {
          if (j1 != p[i]) {
            c[j1][j2][j3] = 0;
          }
        }
      }
    }
  }

  if (p[i + 1]) {
    for (int j1 = 0; j1 < 3; j1++) {
      for (int j2 = 0; j2 < 3; j2++) {
        for (int j3 = 0; j3 < 3; j3++) {
          if (j2 != p[i + 1]) {
            c[j1][j2][j3] = 0;
          }
        }
      }
    }
  }

  if (p[i + 2]) {
    for (int j1 = 0; j1 < 3; j1++) {
      for (int j2 = 0; j2 < 3; j2++) {
        for (int j3 = 0; j3 < 3; j3++) {
          if (j3 != p[i + 2]) {
            c[j1][j2][j3] = 0;
          }
        }
      }
    }
  }

  for (int j1 = 0; j1 < 3; j1++) {
    for (int j2 = 0; j2 < 3; j2++) {
      for (int j3 = 0; j3 < 3; j3++) {
        if (c[j1][j2][j3] > 10000) {
          c[j1][j2][j3] %= 10000;
        }
      }
    }
  }

  return c[x][y][z];
}

int total() {
  int sum = 0;
  for (int j1 = 0; j1 < 3; j1++) {
    for (int j2 = 0; j2 < 3; j2++) {
      for (int j3 = 0; j3 < 3; j3++) {
        sum += fanc(n - 3, j1, j2, j3);
      }
    }
  }
  return sum;
}

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

  cout << total() % 10000 << endl;
  return 0;
}