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

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

int a[10];
unsigned long long dp[10000][10];

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

  int ast = 0;
  int f = 0;
  char id[500000];
  for (int i = 0; i < n; i++) {
    scanf(" %c", &id[i]);

    if (id[i] == '*') {
      ast++;
    } else {
      int tw = ((id[i] - '0') * (1 + ((n - i) % 2 == 0))) / 10 +
               ((id[i] - '0') * (1 + ((n - i) % 2 == 0))) % 10;
      f = (f + tw) % 10;
    }
  }

  int m;
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &a[i]);
  }

  dp[0][f] = 1;
  int i = 0;
  for (int p = 0; p < n; p++) {
    if (id[p] == '*') {
      for (int j = 0; j <= 9; j++) {
        for (int k = 0; k < m; k++) {
          int tw = (a[k] * (1 + ((n - p) % 2 == 0))) / 10 +
                   (a[k] * (1 + ((n - p) % 2 == 0))) % 10;
          int t = (j + tw) % 10;
          dp[i + 1][t] += dp[i][j];
        }
      }
      i++;
    }
  }

  printf("%llu\\n", dp[ast][0]);

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
using namespace std;

char str[200000];
int dp[1000000];
int dp2[1000000];

int main() {
  int n, m;
  int number[100];

  scanf("%d", &n);
  scanf("%s", str);
  scanf("%d", &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &number[i]);
  }

  dp[0] = 1;

  char p = 0;
  while (str[p] != '\\0') {
    memset(dp2, 0, sizeof(dp2));

    if (str[p] == '*') {
      for (int i = 999999; i >= 0; i--) {
        if (dp[i] > 0) {
          for (int j = 0; j < m; j++) {
            dp2[i + (number[j] * (p % 2 + 1)) / 10 +
                (number[j] * (p % 2 + 1)) % 10] += dp[i];
          }
        }
      }
    }

    else {
      for (int i = 999999; i >= ((str[p] - '0') * (p % 2 + 1)) / 10 +
                                    ((str[p] - '0') * (p % 2 + 1)) % 10;
           i--) {
        dp2[i] += dp[i - (((str[p] - '0') * (p % 2 + 1)) / 10 +
                          ((str[p] - '0') * (p % 2 + 1)) % 10)];
      }
    }

    for (int i = 0; i < 1000000; i++) {
      dp[i] = dp2[i];
    }

    p++;
  }

  int sum = 0;
  for (int i = 0; i <= 999999; i += 10) {
    sum += dp[i];
  }

  printf("%d\\n", sum);
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int n;
string s;
vector<int> V;
int ans;
int s_sum;

void input() {

  cin >> n;
  cin >> s;
  int m;
  cin >> m;
  while (m--) {
    int in;
    cin >> in;
    V.push_back(in);
  }
}

void solve(int odd, int even, int sum) {

  if (odd + even > 0) {
    for (int i = 0; i < V.size(); i++) {
      if (odd > 0)
        solve(odd - 1, even, sum + V[i]);
      else if (even > 0)
        solve(odd, even - 1, sum + (V[i] * 2) / 10 + (V[i] * 2) % 10);
    }
  } else {
    // cout << "ans " << s_sum+sum << endl;
    if ((s_sum + sum) % 10 == 0)
      ans++;
  }
}

int main() {

  input();
  int num = 0, odd = 0, even = 0;
  reverse(s.begin(), s.end());

  for (int i = 0; i < s.length(); i++) {
    if (i % 2 == 0) {
      if (s[i] == '*')
        odd++;
      else
        s_sum += s[i] - '0';
    } else {
      if (s[i] == '*')
        even++;
      else {
        int tmp = s[i] - '0';
        tmp *= 2;
        s_sum += tmp / 10;
        s_sum += tmp % 10;
      }
    }
  }

  solve(odd, even, 0);

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int n;
string s;
vector<int> V;
int ans;

void input() {

  cin >> n;
  cin >> s;
  int m;
  cin >> m;
  while (m--) {
    int in;
    cin >> in;
    V.push_back(in);
  }
}

bool isValid(string str) {

  reverse(str.begin(), str.end());

  int num = 0;

  for (int i = 0; i < str.length(); i++) {

    if (i % 2) {
      // even
      int tmp = str[i] - '0';
      tmp *= 2;
      num += tmp / 10 + tmp % 10;
    } else {
      // odd
      num += str[i] - '0';
    }
  }
  return num % 10 == 0;
}

void solve(int num, string str) {

  if (num > 0) {
    for (int i = 0; i < V.size(); i++) {
      string tmp = str;
      for (int j = 0; j < tmp.length(); j++)
        if (tmp[j] == '*') {
          tmp[j] = V[i] + '0';
          break;
        }
      solve(num - 1, tmp);
    }
  } else {
    //   cout << "str " << str << endl;
    if (isValid(str))
      ans++;
  }
}

int main() {

  input();
  int num = 0;

  for (int i = 0; i < s.length(); i++)
    if (s[i] == '*')
      num++;

  solve(num, s);

  cout << ans << endl;
  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
int ans;
int n;
string ID;
vector<int> a;
int aster_count;
bool isEven[8];

void DFS(int pos, int sum) {
  if (pos == aster_count) {
    if (sum % 10 == 0) {
      ++ans;
    }
    return;
  }
  for (int i = 0; i < a.size(); ++i) {
    int sum2 = sum;
    if (isEven[pos]) {
      if (2 * a[i] < 10) {
        sum2 += 2 * a[i];
      } else {
        sum2 += (2 * a[i]) % 10 + 1;
      }
      DFS(pos + 1, sum2);
    } else {
      sum2 += (a[i]);
      DFS(pos + 1, sum2);
    }
  }
}

void solve() {
  cin >> n;
  cin >> ID;
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int input;
    cin >> input;
    a.push_back(input);
  }
  memset(isEven, 0, sizeof(isEven));
  int sum = 0;
  bool multiple_flag = false;
  for (int i = ID.size() - 1; i >= 0; --i) {
    if (ID[i] != '*') {
      if (multiple_flag) {
        int t = 2 * (ID[i] - '0');
        if (t < 10) {
          sum += t;
        } else {
          sum += t % 10 + 1;
        }
      } else {
        sum += (ID[i] - '0');
      }
    } else {
      if (multiple_flag) {
        isEven[aster_count] = true;
      } else {
        isEven[aster_count] = false;
      }
      ++aster_count;
    }
    multiple_flag = !multiple_flag;
  }
  DFS(0, sum);
  cout << ans << endl;
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;
int ans;
int n;
string ID;
vector<int> a;

void DFS(int pos, int sum) {
  if (pos == -1) {
    if (sum % 10 == 0) {
      ++ans;
    }
    return;
  }
  if (ID[pos] == '*') {
    bool multiple_flag = false;
    if ((pos % 2 && ID.size() % 2) || (pos % 2 == 0 && ID.size() % 2 == 0)) {
      multiple_flag = true;
    }
    for (int i = 0; i < a.size(); ++i) {
      int sum2 = sum;
      if (multiple_flag) {
        if (2 * a[i] < 10) {
          sum2 += 2 * a[i];
        } else {
          sum2 += (2 * a[i]) % 10 + 1;
        }
      } else {
        sum2 += (a[i]);
      }
      DFS(pos - 1, sum2);
    }
  } else {
    DFS(pos - 1, sum);
  }
}

void solve() {

  cin >> n;
  cin >> ID;
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    int input;
    cin >> input;
    a.push_back(input);
  }
  int sum = 0;
  bool multiple_flag = false;
  for (int i = ID.size() - 1; i >= 0; --i) {
    if (ID[i] != '*') {
      if (multiple_flag) {
        int t = 2 * (ID[i] - '0');
        if (t < 10) {
          sum += t;
        } else {
          sum += t % 10 + 1;
        }
      } else {
        sum += (ID[i] - '0');
      }
    }
    multiple_flag = !multiple_flag;
  }
  DFS(ID.size() - 1, sum);
  cout << ans << endl;
}

int main() {
  solve();
  return (0);
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

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

using namespace std;

int n, m;
char x[100000 + 1];
int a[10];
int k;
int p[7];
int dp[8][10];

int convert(int f, int i) {
  if ((n - i) % 2 == 1) {
    return f;
  }
  int g = f * 2;
  return (g / 10) + (g % 10);
}

int main() {
  scanf("%d%s%d", &n, x, &m);
  rep(i, m) { scanf("%d", a + i); }
  int t = 0;
  rep(i, n) {
    if (x[i] == '*') {
      p[k++] = i;
    } else {
      t += convert(x[i] - '0', i);
    }
  }
  dp[0][t % 10] = 1;
  for (int i = 1; i <= k; ++i) {
    rep(j, m) {
      int q = convert(a[j], p[i - 1]);
      rep(l, 10) { dp[i][(l + q) % 10] += dp[i - 1][l]; }
    }
  }
  /*for(int i = 0; i <= k; ++i){
          printf("i = %d\\n", i);
          rep(j, 10){
                  printf("%d ", dp[i][j]);
          }
          printf("\\n");
  }*/
  printf("%d\\n", dp[k][0]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

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

using namespace std;

int n, m;
string x;
int a[2][10];
int t;
int y;

void dfs(int i) {
  if (i == n) {
    // cout << "t = " << t << endl;
    if (t % 10 == 0) {
      ++y;
    }
    return;
  }
  if (x[i] != '*') {
    int p = x[i] - '0';
    int s;
    if (i % 2 == 0) {
      s = p;
    } else {
      p *= 2;
      s = p / 10 + (p % 10);
    }
    t += s;
    dfs(i + 1);
    t -= s;
  } else {
    rep(j, m) {
      t += a[i % 2][j];
      dfs(i + 1);
      t -= a[i % 2][j];
    }
  }
}

int main() {
  cin >> n >> x >> m;
  rep(i, m) {
    int v;
    cin >> v;
    a[0][i] = v;
    v *= 2;
    a[1][i] = v / 10 + (v % 10);
  }
  dfs(0);
  cout << y << endl;
  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 100001

int m, n, num[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, p[8], ksum, uk;
char a[11];

int dfs(int k, int sum) {
  int i, j, ret = 0, plus;
  if (k == uk) {
    if ((sum + ksum) % 10 == 0)
      return 1;
    return 0;
  }
  for (i = 0; i < m; i++) {
    if ((n - 1 - p[k]) % 2 == 0)
      plus = num[a[i] - '0'];
    else
      plus = (num[a[i] - '0'] * 2) / 10 + (num[a[i] - '0'] * 2) % 10;
    ret += dfs(k + 1, sum + plus);
  }
  return ret;
}

int main(void) {
  int i, j;
  char s[ARRAY_SIZE];
  scanf("%d%*c", &n);
  for (i = 0; i < n; i++)
    s[i] = getchar();
  scanf("%d%*c", &m);
  for (i = 0; i < m; i++) {
    a[i] = getchar();
    getchar();
  }
  ksum = uk = 0;
  for (i = 0; i < n; i++) {
    if (s[i] == '*') {
      p[uk] = i;
      uk++;
    } else {
      if ((n - 1 - i) % 2 == 0)
        ksum += num[s[i] - '0'];
      else
        ksum += (num[s[i] - '0'] * 2) / 10 + (num[s[i] - '0'] * 2) % 10;
    }
  }
  printf("%d\\n", dfs(0, 0));
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ARRAY_SIZE 100001

int m, n, num[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
char a[11], s[8][ARRAY_SIZE];

int dfs(int k, unsigned int l) {
  int i, j, ret = 0;
  if (l == n) {
    int sum = 0;
    for (i = n - 1; i >= 0; i--) {
      if ((i - (n - 1)) % 2 == 0) // 奇数番目
        sum += num[s[k][i] - '0'];
      else
        sum += (num[s[k][i] - '0'] * 2) / 10 + (num[s[k][i] - '0'] * 2) % 10;
    }
    if (sum % 10 == 0)
      return 1;
    return 0;
  }
  if (s[k][l] == '*') {
    for (i = 0; i < m; i++) {
      for (j = 0; j <= n; j++)
        s[k + 1][j] = s[k][j];
      s[k + 1][l] = a[i];
      ret += dfs(k + 1, l + 1);
    }
  } else
    ret = dfs(k, l + 1);
  return ret;
}

int main(void) {
  int i, j;
  scanf("%d%*c", &n);
  for (i = 0; i < n; i++)
    s[0][i] = getchar();
  scanf("%d%*c", &m);
  for (i = 0; i < m; i++) {
    a[i] = getchar();
    getchar();
  }
  printf("%d\\n", dfs(0, 0));
  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int dp[10][100001];

void printD() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j <= 15; j++) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
}

int main() {
  int n, m;
  string id;
  int even = 1;
  cin >> n >> id >> m;
  if (n & 1)
    even = 0;
  int a[m];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < m; i++)
    cin >> a[i];

  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    int val;
    for (int j = 0; j < 10; j++) {
      if (id[i] != '*') {
        val = (id[i] - '0') << even;
        val = val % 10 + val / 10;
        dp[(j + val) % 10][i + 1] = dp[j][i];
      } else {
        for (int k = 0; k < m; k++) {
          val = a[k] << even;
          val = val % 10 + val / 10;
          dp[(j + val) % 10][i + 1] += dp[j][i];
        }
      }
    }
    even = !even;
  }
  // printD();
  cout << dp[0][n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int n, m;
int a[10];

int dfs(int even, int sum, string id) {
  // cout << id << " " << sum << endl;
  int pos = 0;
  while (id.length() != pos && id[pos] != '*') {
    int add = (id[pos++] - '0') << even;
    even = !even;
    add = add % 10 + add / 10;
    sum += add;
  }
  if (id.length() == pos) {
    if (sum % 10 == 0)
      return 1;
    else
      return 0;
  }

  int res = 0;
  for (int i = 0; i < m; i++) {
    int add = a[i] << even;
    // cout << "begin:" << begin << " add:" << add << endl;
    add = add % 10 + add / 10;
    res += dfs(!even, sum + add, id.substr(pos + 1));
  }
  return res;
}

int main() {
  string id;
  cin >> n >> id >> m;
  for (int i = 0; i < m; i++)
    cin >> a[i];
  cout << dfs(0, 0, id) << endl;
  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m;
vector<int> a;
vector<int> stars;

int sep(int x, int t) {
  x *= t;
  if (x < 10)
    return x;
  return x % 10 + 1;
}

unsigned solve(int i, int total) {
  if (i == stars.size()) {
    return (total % 10) == 0;
  }

  int res = 0;
  for (int ai : a) {
    res += solve(i + 1, total + sep(ai, stars[i]));
  }
  return res;
}

int main() {
  string id;
  cin >> n >> id >> m;
  a = vector<int>(m);
  for (int &ai : a) {
    cin >> ai;
  }

  int base = 0;
  for (int i = 0; i < n; i++) {
    int t = 1 + ((n - i - 1) & 1);
    if (id[i] == '*') {
      stars.push_back(t);
    } else {
      base += sep(id[i] - '0', t);
      base %= 10;
    }
  }

  cout << solve(0, base) << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int n, m;
string id;
vector<int> a;

int d_val(int d, int t) {
  int x = d * t;
  if (x >= 10) {
    x = x % 10 + 1;
  }
  return x;
}

unsigned solve(int i, int total) {
  if (i == n) {
    return (total % 10) == 0;
  }

  int idx = n - i - 1;
  int t = 1 + (i & 1);

  if (id[idx] == '*') {
    unsigned s = 0;
    for (int ai : a) {
      int x = d_val(ai, t);
      s += solve(i + 1, total + x);
    }
    return s;
  }

  int x = d_val(id[idx] - '0', t);
  return solve(i + 1, total + x);
}

int main() {
  cin >> n >> id >> m;
  a = vector<int>(m);
  for (int &ai : a) {
    cin >> ai;
  }
  cout << solve(0, 0) << endl;
  return 0;
}
Problem p00990 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

int n, m;
vector<int> a;
vector<int> pos;
vector<int> b;
vector<char> ID;
int k;

inline int ev(int a) { return a / 10 + a % 10; }

int calc() {
  int sum = 0;
  REP(i, pos.size()) {
    if ((n - pos[i]) % 2 != 0)
      sum += b[pos[i]];
    else
      sum += ev(2 * b[pos[i]]);
  }
  // cout  << k + sum << endl;
  return k + sum;
}

int dfs(const int d) {
  if (d == pos.size()) {
    return (calc() % 10 == 0) ? 1 : 0;
  }
  int c = 0;
  REP(j, m) {
    b[pos[d]] = a[j];
    c += dfs(d + 1);
  }
  return c;
}

int main() {
  // 入力
  cin >> n;
  ID.reserve(n);
  b.reserve(n);
  REP(i, n) {
    cin >> ID[i];
    if (ID[i] == '*') {
      pos.push_back(i);
    } else
      b[i] = ID[i] - '0';
    if ((n - i) % 2 != 0)
      k += b[i];
    else
      k += ev(2 * b[i]);
  }
  cin >> m;
  a.reserve(m);
  REP(i, m)
  cin >> a[i];

  cout << dfs(0) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

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

int n, m;
vector<int> a;
vector<int> pos;
vector<int> b;
vector<char> ID;

inline int ev(int a) { return a / 10 + a % 10; }

int calc() {
  int sum = 0;
  // 右端から奇数番目
  for (int i = n - 1; i >= 0; i -= 2)
    sum += b[i];

  // 偶数番目
  for (int i = n - 2; i >= 0; i -= 2)
    sum += ev(2 * b[i]);

  // cout  << ID << ": " << sum << endl;
  return sum;
}

int dfs(const int d) {
  if (d == pos.size()) {
    return (calc() % 10 == 0) ? 1 : 0;
  }
  int c = 0;
  REP(j, m) {
    b[pos[d]] = a[j];
    c += dfs(d + 1);
  }
  return c;
}

int main() {
  // 入力
  cin >> n;
  ID.reserve(n);
  b.reserve(n);
  REP(i, n) {
    cin >> ID[i];
    if (ID[i] == '*') {
      pos.push_back(i);
    } else
      b[i] = ID[i] - '0';
  }
  cin >> m;
  a.reserve(m);
  REP(i, m)
  cin >> a[i];

  cout << dfs(0) << endl;

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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

ll nCr[2000][2000] = {0};

int main(void) {
  for (int i = 1; i < 1010; ++i) {
    nCr[i][0] = nCr[i][i] = 1;
  }
  for (int i = 2; i < 1010; ++i) {
    for (int j = 1; j < i; ++j) {
      nCr[i][j] = (nCr[i - 1][j - 1] + nCr[i - 1][j]) % 100000007;
    }
  }

  int r, c, a1, a2, b1, b2;
  int min_1, min_2, coef = 1;
  cin >> r >> c >> a1 >> a2 >> b1 >> b2;

  min_1 = min(abs(a1 - b1), r - abs(a1 - b1));
  min_2 = min(abs(a2 - b2), c - abs(a2 - b2));

  if (abs(a1 - b1) == r - abs(a1 - b1))
    coef *= 2;
  if (abs(a2 - b2) == c - abs(a2 - b2))
    coef *= 2;

  int res;

  if (min_1 + min_2 == 0) {
    res = 1;
  } else {
    res = (coef * (int)nCr[min_1 + min_2][min_2]) % 100000007;
  }

  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

using namespace std;

ll nCr[2000][2000] = {0};

int main(void) {
  for (int i = 1; i < 510; ++i) {
    nCr[i][0] = nCr[i][i] = 1;
  }
  for (int i = 2; i < 510; ++i) {
    for (int j = 1; j < i; ++j) {
      nCr[i][j] = (nCr[i - 1][j - 1] + nCr[i - 1][j]) % 100000007;
    }
  }

  int r, c, a1, a2, b1, b2;
  int min_1, min_2, coef = 1;
  cin >> r >> c >> a1 >> a2 >> b1 >> b2;

  min_1 = min(abs(a1 - b1), r - abs(a1 - b1));
  min_2 = min(abs(a2 - b2), r - abs(a2 - b2));
  cout << min_1 << " " << min_2 << endl;

  if (abs(a1 - b1) == r - abs(a1 - b1))
    coef *= 2;
  if (abs(a2 - b2) == r - abs(a2 - b2))
    coef *= 2;

  int res = coef * (int)nCr[min_1 + min_2][min_2];

  cout << res << endl;
  while (1)
    ;
  return 0;
}
Problem p00991 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 100000007
using namespace std;
typedef pair<int, int> P;

int d[1000][1000], ans[1000][1000];
int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
int main() {
  int r, c, x1, y1, x2, y2;
  cin >> r >> c >> x1 >> y1 >> x2 >> y2;
  queue<P> que;
  memset(d, -1, sizeof(d));
  ans[x1][y1] = 1;
  d[x1][y1] = 0;
  que.push(P(x1, y1));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    rep(i, 4) {
      int nx = (p.first + dx[i] + r) % r, ny = (p.second + dy[i] + c) % c;
      if (d[nx][ny] == -1) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
      if (d[nx][ny] == d[p.first][p.second] + 1)
        (ans[nx][ny] += ans[p.first][p.second]) %= MOD;
    }
  }
  printf("%d\\n", ans[x2][y2]);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 100000007
using namespace std;
typedef pair<int, int> P;

int d[1000][1000], ans[1000][1000];
int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
int main() {
  int r, c, x1, y1, x2, y2;
  cin >> r >> c >> x1 >> y1 >> x2 >> y2;
  queue<P> que;
  memset(d, -1, sizeof(d));
  ans[x1][y1] = 1;
  d[x1][y1] = 0;
  que.push(P(x1, y1));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    rep(i, 4) {
      int nx = p.first + dx[i], ny = p.second + dy[i];
      int a = 0;
      /*	if(nx<0||nx>=r){
                      nx=(nx+r)%r;a++;
              }
              if(ny<0||ny>=c){
                      ny=(ny+c)%c;a++;
              }
              if(a==2)continue;*/
      if (d[nx][ny] == -1) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
      if (d[nx][ny] == d[p.first][p.second] + 1)
        (ans[nx][ny] += ans[p.first][p.second]) %= MOD;
    }
  }
  printf("%d\\n", ans[x2][y2]);
}
Problem p00991 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define mod 100000007
using namespace std;

int memo[2001][2001];
int saiki(int a, int b) {
  if (memo[a][b] != -1)
    return memo[a][b];
  int c = 0;
  if (a == 0 || b == 0)
    c = 1;
  else {
    c += saiki(a - 1, b);
    c += saiki(a, b - 1);
  }
  return memo[a][b] = c % mod;
}
signed main() {
  memset(memo, -1, sizeof(memo));
  int a, b, c, d, e, f;
  cin >> a >> b >> c >> d >> e >> f;
  int g = abs(e - c), h = abs(f - d);
  int i = a - max(e, c) + min(e, c), j = b - max(f, d) + min(f, d);
  int k = min(g, i), l = min(h, j);
  if (c == e) {
    if (h == j)
      puts("2");
    else
      puts("1");
    return 0;
  }
  if (d == f) {
    if (g == i)
      puts("2");
    else
      puts("1");
    return 0;
  }
  int ans = saiki(k, l);
  if (g == i) {
    if (h == j) {
      ans *= 4;
    } else {
      ans *= 2;
    }
  } else {
    if (h == j) {
      ans *= 2;
    }
  }
  cout << ans % mod << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <unordered_map>
#include <vector>
#define int long long
#define mod 100000007
using namespace std;

int memo[2001][2001];
int saiki(int a, int b) {
  if (memo[a][b] != -1)
    return memo[a][b];
  int c = 0;
  if (a == 0 || b == 0)
    c = 1;
  else {
    c += saiki(a - 1, b);
    c += saiki(a, b - 1);
  }
  return memo[a][b] = c % mod;
}
signed main() {
  memset(memo, -1, sizeof(memo));
  int a, b, c, d, e, f;
  cin >> a >> b >> c >> d >> e >> f;
  int g = e - c, h = f - d;
  int i = a - e + c, j = b - f + d;
  int k = min(g, i), l = min(h, j);
  int ans = saiki(k, l);
  if (g == i) {
    if (h == j) {
      ans *= 4;
    } else {
      ans *= 2;
    }
  } else {
    if (h == j) {
      ans *= 2;
    }
  }
  cout << ans << endl;
}