Problem p03395 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define LL long long
#define LB long double
#define x first
#define y second
#define Pair pair<int, int>
#define pb push_back
#define pf push_front
#define mp make_pair
#define LOWBIT(x) x & (-x)
using namespace std;

const int MOD = 1e9 + 7;
const LL LINF = 2e16;
const int INF = 2e9;
const int magic = 348;
const double eps = 1e-10;
const double pi = 3.14159265;

inline int getint() {
  char ch;
  int res;
  bool f;
  while (!isdigit(ch = getchar()) && ch != '-') {
  }
  if (ch == '-')
    f = false, res = 0;
  else
    f = true, res = ch - '0';
  while (isdigit(ch = getchar()))
    res = res * 10 + ch - '0';
  return f ? res : -res;
}

int n;
int a[58], b[58];
bool dp[58][58][58];
int DP[58][58];
bool Dp[58][58];
int valid[58], vtot;
int anslist[58], tot;

inline void init_dp() {
  memset(dp, false, sizeof(dp));
  int i, j, k;
  for (i = 0; i <= 50; i++)
    dp[i][i][0] = dp[i][i][1] = true;
  for (i = 0; i <= 50; i++)
    dp[i][0][1] = true;
  for (k = 2; k <= 50; k++)
    for (i = 0; i <= 50; i++)
      for (j = 0; j <= 50; j++) {
        if (dp[i][j][k - 1]) {
          dp[i][j][k] = true;
          continue;
        }
        if (dp[i % k][j][k - 1]) {
          dp[i][j][k] = true;
          continue;
        }
        dp[i][j][k] = false;
      }
  for (i = 0; i <= 50; i++)
    for (j = 0; j <= 50; j++) {
      for (k = 0; !dp[i][j][k] && k <= 50; k++) {
      }
      DP[i][j] = k;
    }
}

inline bool judge() {
  int res = -1;
  int i, j, k, cc;
  for (i = 1; i <= n; i++) {
    memset(Dp, false, sizeof(Dp));
    Dp[0][a[i]] = true;
    for (j = 0; j <= vtot - 1; j++)
      for (k = 0; k <= 50; k++)
        if (Dp[j][k]) {
          Dp[j + 1][k] = true;
          Dp[j + 1][k % valid[j + 1]] = true;
        }
    if (!Dp[vtot][b[i]])
      return false;
  }
  return true;
}

int main() {
  n = getint();
  int i;
  for (i = 1; i <= n; i++)
    a[i] = getint();
  for (i = 1; i <= n; i++)
    b[i] = getint();
  init_dp();
  int lim = -1, calc;
  for (i = 1; i <= n; i++)
    lim = max(lim, DP[a[i]][b[i]]);
  if (lim > 50) {
    printf("-1\n");
    return 0;
  }
  if (lim == 0) {
    printf("0\n");
    return 0;
  }
  anslist[1] = lim;
  ++tot;
  for (calc = lim - 1; calc >= 0; calc--) {
    vtot = 0;
    for (i = 1; i <= tot; i++)
      valid[++vtot] = anslist[i];
    for (i = calc - 1; i; i--)
      valid[++vtot] = i;
    if (judge())
      continue;
    anslist[++tot] = calc;
  }
  LL ans = 0;
  for (i = 1; i <= tot; i++)
    ans += (1ll << anslist[i]);
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define LL long long
#define LB long double
#define x first
#define y second
#define Pair pair<int, int>
#define pb push_back
#define pf push_front
#define mp make_pair
#define LOWBIT(x) x & (-x)
using namespace std;

const int MOD = 1e9 + 7;
const LL LINF = 2e16;
const int INF = 2e9;
const int magic = 348;
const double eps = 1e-10;
const double pi = 3.14159265;

inline int getint() {
  char ch;
  int res;
  bool f;
  while (!isdigit(ch = getchar()) && ch != '-') {
  }
  if (ch == '-')
    f = false, res = 0;
  else
    f = true, res = ch - '0';
  while (isdigit(ch = getchar()))
    res = res * 10 + ch - '0';
  return f ? res : -res;
}

int n;
int a[58], b[58];
bool dp[58][58][58];
int DP[58][58];
bool Dp[58][58];
int valid[58], vtot;
int anslist[58], tot;

inline void init_dp() {
  memset(dp, false, sizeof(dp));
  int i, j, k;
  for (i = 0; i <= 50; i++)
    dp[i][i][0] = dp[i][i][1] = true;
  for (i = 0; i <= 50; i++)
    dp[i][0][1] = true;
  for (k = 2; k <= 50; k++)
    for (i = 0; i <= 50; i++)
      for (j = 0; j <= 50; j++) {
        if (dp[i][j][k - 1]) {
          dp[i][j][k] = true;
          continue;
        }
        if (dp[i % k][j][k - 1]) {
          dp[i][j][k] = true;
          continue;
        }
        dp[i][j][k] = false;
      }
  for (i = 0; i <= 50; i++)
    for (j = 0; j <= 50; j++) {
      for (k = 0; !dp[i][j][k] && k <= 50; k++) {
      }
      DP[i][j] = k;
    }
}

inline bool judge() {
  int res = -1;
  int i, j, k, cc;
  for (i = 1; i <= n; i++) {
    memset(Dp, false, sizeof(Dp));
    Dp[0][a[i]] = true;
    for (j = 0; j <= vtot - 1; j++)
      for (k = 0; k <= 50; k++)
        if (Dp[j][k]) {
          Dp[j + 1][k] = true;
          Dp[j + 1][k % valid[j + 1]] = true;
        }
    if (!Dp[vtot][b[i]])
      return false;
  }
  return true;
}

int main() {
  n = getint();
  int i;
  for (i = 1; i <= n; i++)
    a[i] = getint();
  for (i = 1; i <= n; i++)
    b[i] = getint();
  init_dp();
  int lim = -1, calc;
  for (i = 1; i <= n; i++)
    lim = max(lim, DP[a[i]][b[i]]);
  if (lim > 50) {
    printf("-1\n");
    return 0;
  }
  if (lim == 0) {
    printf("0\n");
    return 0;
  }
  anslist[1] = lim;
  ++tot;
  for (calc = lim - 1; calc > 0; calc--) {
    vtot = 0;
    for (i = 1; i <= tot; i++)
      valid[++vtot] = anslist[i];
    for (i = calc - 1; i; i--)
      valid[++vtot] = i;
    if (judge())
      continue;
    anslist[++tot] = calc;
  }
  LL ans = 0;
  for (i = 1; i <= tot; i++)
    ans += (1ll << anslist[i]);
  printf("%lld\n", ans);
  return 0;
}