Problem p03670 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
const int mod = 1e9 + 7;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int n, m, k, c[N][N], f[2][N][1 << 20], nxt[1 << 20][N];
void inc(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int main() {
  n = gi() - 1, m = gi(), k = gi();
  memset(c, -1, sizeof(c));
  while (k--) {
    int x = gi(), y = gi();
    c[x][y] = gi() ^ 1;
  }
  for (int i = 0; i < (1 << n); i++) {
    k = -1;
    for (int j = n - 1; j >= 0; j--) {
      nxt[i][j] = k;
      if (i >> j & 1)
        k = j;
    }
  }
  int now = 0, lst = 1;
  f[lst][n][0] = 1;
  for (int i = 1; i <= m; i++) {
    memset(f[now], 0, sizeof(f[now]));
    memcpy(f[now][0], f[lst][n], sizeof(f[now][0]));
    for (int j = 0; j < n; j++)
      for (int k = 0; k < (1 << n); k++)
        if (f[now][j][k]) {
          for (int t = (k >> j & 1); t < 2; t++)
            if (c[i][j + 1] != t) {
              if (t == (k >> j & 1))
                inc(f[now][j + 1][k], f[now][j][k]);
              else if (nxt[k][j] != -1)
                inc(f[now][j + 1][k + (1 << j) - (1 << nxt[k][j])],
                    f[now][j][k]);
              else
                inc(f[now][j + 1][k + (1 << j)], f[now][j][k]);
            }
        }
    swap(now, lst);
  }
  int ans = 0;
  for (int i = 0; i < (1 << n); i++)
    inc(ans, f[lst][n][i]);
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 21;
const int mod = 1e9 + 7;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-')
    ch = getchar();
  if (ch == '-')
    o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int n, m, k, c[N][N], f[2][N][1 << 20], nxt[1 << 20][N];
void inc(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
int main() {
  n = gi() - 1, m = gi(), k = gi();
  memset(c, -1, sizeof(c));
  while (k--) {
    int x = gi(), y = gi();
    c[x][y] = gi() ^ 1;
  }
  for (int i = 0; i < (1 << n); i++) {
    k = -1;
    for (int j = n - 1; j >= 0; j--) {
      nxt[i][j] = k;
      if (i >> j & 1)
        k = j;
    }
  }
  int now = 0, lst = 1;
  f[lst][n][0] = 1;
  for (int i = 1; i <= m; i++) {
    memset(f[now], 0, sizeof(f[now]));
    memcpy(f[now][0], f[lst][n], sizeof(f[now][0]));
    for (int j = 0; j < n; j++)
      for (int k = 0; k < (1 << n); k++)
        if (f[now][j][k]) {
          for (int t = (k >> j & 1); t < 2; t++)
            if (c[i][j + 1] != t) {
              if (t == (k >> j & 1))
                inc(f[now][j + 1][k], f[now][j][k]);
              else if (nxt[k][j] != -1)
                inc(f[now][j + 1][k + (1 << j) - (1 << nxt[k][j])],
                    f[now][j][k]);
              else
                inc(f[now][j + 1][k + (1 << j)], f[now][j][k]);
            }
        }
    swap(now, lst);
  }
  int ans = 0;
  for (int i = 0; i < (1 << n); i++)
    inc(ans, f[lst][n][i]);
  printf("%d\n", ans);
  return 0;
}