prithivMLmods/Coder-Stat huggingface dataset:
Problem p02860 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  string S;
  cin >> N >> S;
  bool nazo = true;
  for (int i = 0; i < S.size() / 2; i++) {
    if (S.at(i) == S.at(i + 3)) {
      nazo = true;
    } else {

      nazo = false;
      break;
    }
  }
  if (N == 1) {
    nazo = false;
  }
  if (nazo) {
    cout << "Yes";
  } else {
    cout << "No";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  string S;
  cin >> N >> S;
  bool nazo = true;
  if (N % 2 == 0 && S.substr(0, N / 2) == S.substr(N / 2, N)) {
    nazo = true;
  } else {
    nazo = false;
  }
  if (N == 1) {
    nazo = false;
  }
  if (nazo) {
    cout << "Yes";
  } else {
    cout << "No";
  }
}
Problem p02860 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int N;
  string S;
  cin >> N >> S;

  if (N % 2 == 0) {
    int center = N / 2;
    for (int i = 0; i < N; i++) {
      if (S.at(i) != S.at(i + center)) {
        cout << "No" << endl;
        return 0;
      }
    }
    cout << "Yes" << endl;
  } else
    cout << "No" << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N;
  string S;
  cin >> N >> S;

  if (N % 2 == 0) {
    int center = N / 2;
    for (int i = 0; i < center; i++) {
      if (S.at(i) != S.at(i + center)) {
        cout << "No" << endl;
        return 0;
      }
    }
    cout << "Yes" << endl;
  } else
    cout << "No" << endl;

  return 0;
}
Problem p02860 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define prin(a) cout << (a) << endl
using ll = int64_t;
using namespace std;

// define gcd and lcm (using 64bit)
ll gcd(ll a, ll b) {
  if (a < b) {
    //^はxor(bitwise operator)。以下三行でswap(a,b)を行っている。
    a ^= b;
    b ^= a;
    a ^= b;
  }
  // only when b==0,this program finds that b means false and returns a.
  return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  int n;
  string s;
  cin >> n >> s;
  bool ok = true;
  if (n % 2 == 1) {
    ok = false;
  } else {
    string t, u;
    t = s.substr(0, (n / 2));
    u = s.substr(3, (n / 2));
    if (t != u)
      ok = false;
  }
  cout << (ok ? "Yes" : "No") << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define prin(a) cout << (a) << endl
using ll = int64_t;
using namespace std;

// define gcd and lcm (using 64bit)
ll gcd(ll a, ll b) {
  if (a < b) {
    //^はxor(bitwise operator)。以下三行でswap(a,b)を行っている。
    a ^= b;
    b ^= a;
    a ^= b;
  }
  // only when b==0,this program finds that b means false and returns a.
  return b ? gcd(b, a % b) : a;
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main() {
  int n;
  string s;
  cin >> n >> s;
  bool ok = true;
  if (n % 2 == 1) {
    ok = false;
  } else {
    string t, u;
    t = s.substr(0, (n / 2));
    u = s.substr((n / 2), (n / 2));
    if (t != u)
      ok = false;
  }
  cout << (ok ? "Yes" : "No") << endl;
  return 0;
}
Problem p02860 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;

int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;
  int cnt = 0;

  if (N % 2 != 0) {
    cout << "No" << endl;
  } else {
    rep(i, N) {
      if (S.at(i) == S.at(i + (N / 2)))
        cnt++;
    }
    if (cnt == N / 2) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;

int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;
  int cnt = 0;

  if (N % 2 != 0) {
    cout << "No" << endl;
  } else {
    rep(i, N / 2) {
      if (S.at(i) == S.at(i + (N / 2)))
        cnt++;
    }
    if (cnt == N / 2) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}
Problem p02860 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

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

  string S;
  getline(cin, S);
  if (n % 2 == 0 && (S.substr(0, n / 2) == S.substr(n / 2, n))) {
    cout << "Yes";
  } else {
    cout << "No";
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

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

  string S;
  cin >> S;
  if (n % 2 == 0 && (S.substr(0, n / 2) == S.substr(n / 2, n))) {
    cout << "Yes";
  } else {
    cout << "No";
  }
}
Problem p02860 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
s = input()
ans = "No"
if n % 2 == 0:
    ans = "Yes"
    for i in range(n / 2):
        if s[i] != s[n / 2 + i]:
            ans = "No"
            break
print(ans)


Fixed submission:
n = int(input())
s = input()
ans = "No"
if n % 2 == 0:
    ans = "Yes"
    for i in range(n // 2):
        if s[i] != s[n // 2 + i]:
            ans = "No"
            break
print(ans)
Problem p02861 (Python)

Buggy submission (status: Runtime Error):
import sys
from itertools import permutations
from math import factorial

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def distance2(x1, y1, x2, y2):
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5


def main():
    N = int(readline())
    C = [tuple(map(int, readline().split())) for _ in range(N)]

    dist = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(i + 1, N):
            dist[i][j] = dist[j][i] = distance2(*C[i], *C[j])

    ans = 0
    for perm in permutations(range(N)):
        for i in range(N - 1):
            ans += dist[perm[i]][perm[i + 1]]

    print(ans / factorial(N))
    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from itertools import permutations
from math import factorial

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def distance2(x1, y1, x2, y2):
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5


def main():
    N = int(readline())
    C = [tuple(map(int, readline().split())) for _ in range(N)]

    dist = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(i + 1, N):
            dist[i][j] = dist[j][i] = distance2(C[i][0], C[i][1], C[j][0], C[j][1])

    ans = 0
    for perm in permutations(range(N)):
        for i in range(N - 1):
            ans += dist[perm[i]][perm[i + 1]]

    print(ans / factorial(N))
    return


if __name__ == "__main__":
    main()
Problem p02861 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n;
int x[10], y[10];

double dist(int i, int j) {
  double dx = x[i] - x[j];
  double dy = y[i] - y[j];
  return pow(dx * dx + dy * dy, 0.5);
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> y[i];
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    v[i] = i + 1;
  double sum = 0.0;
  do {
    for (int i = 0; i < n - 1; i++)
      sum + dist(x[i], v[i + 1]);

  } while (next_permutation(v.begin(), v.end()));
  int Factorial = 1;
  for (int i = 2; i <= n; i++)
    Factorial *= i;
  cout << fixed << setprecision(10) << sum / Factorial << endl;
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int n;
int x[10], y[10];

double dist(int i, int j) {
  double dx = x[i] - x[j];
  double dy = y[i] - y[j];
  return pow(dx * dx + dy * dy, 0.5);
}

void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> x[i] >> y[i];
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    v[i] = i + 1;
  double sum = 0.0;
  do {
    for (int i = 0; i < n - 1; i++)
      sum += dist(v[i], v[i + 1]);

  } while (next_permutation(v.begin(), v.end()));
  int Factorial = 1;
  for (int i = 2; i <= n; i++)
    Factorial *= i;
  cout << fixed << setprecision(10) << sum / Factorial << endl;
}

int main() {
  solve();
  return 0;
}
Problem p02861 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < N; i++)
using namespace std;

int N;
int x[10], y[10];
double dist(int i, int j) {
  double dx = x[i] - x[j];
  double dy = y[i] - y[j];
  return pow(dx * dx + dy * dy, 0.5);
}

void solve() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> x[i] >> y[i];
  double sum = 0.0;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    v[i] = i + 1;
  do {
    for (int i = 0; i <= N - 1; i++)
      sum += dist(v[i], v[i + 1]);

  } while (next_permutation(v.begin(), v.end()));
  int Factorial = 1;
  for (int i = 2; i <= N; i++)
    Factorial *= i;
  cout << fixed << setprecision(10) << sum / Factorial << endl;
  return;
}

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (int i = 0; i < N; i++)
using namespace std;

int N;
int x[10], y[10];
double dist(int i, int j) {
  double dx = x[i] - x[j];
  double dy = y[i] - y[j];
  return pow(dx * dx + dy * dy, 0.5);
}

void solve() {
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> x[i] >> y[i];
  double sum = 0.0;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    v[i] = i + 1;
  do {
    for (int i = 0; i < N - 1; i++)
      sum += dist(v[i], v[i + 1]);

  } while (next_permutation(v.begin(), v.end()));
  int Factorial = 1;
  for (int i = 2; i <= N; i++)
    Factorial *= i;
  cout << fixed << setprecision(10) << sum / Factorial << endl;
  return;
}

int main() {
  solve();
  return 0;
}
Problem p02861 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int N;
int x[7], y[7];

double dist(int i, int j) {
  int dx = x[i] - x[j];
  int dy = y[i] - y[j];
  return pow(dx * dx + dy * dy, 0.5);
}

void solve() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> x[i] >> y[i];
  double sum;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    v.at(i) = i;
  do {
    for (int i = 0; i < N - 1; i++)
      sum += dist(v.at(i), v.at(i + 1));
  } while (next_permutation(v.begin(), v.end()));
  int dn = 1;
  for (int i = 2; i <= N; i++)
    dn *= i;
  cout << fixed << setprecision(10) << sum / dn << endl;
  return;
}

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

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

int N;
int x[10], y[10];

double dist(int i, int j) {
  int dx = x[i] - x[j];
  int dy = y[i] - y[j];
  return pow(dx * dx + dy * dy, 0.5);
}

void solve() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> x[i] >> y[i];
  double sum;
  vector<int> v(N);
  for (int i = 0; i < N; i++)
    v.at(i) = i;
  do {
    for (int i = 0; i < N - 1; i++)
      sum += dist(v.at(i), v.at(i + 1));
  } while (next_permutation(v.begin(), v.end()));
  int dn = 1;
  for (int i = 2; i <= N; i++)
    dn *= i;
  cout << fixed << setprecision(10) << sum / dn << endl;
  return;
}

int main(void) {
  solve();
  return 0;
}
Problem p02861 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define All(a) a.begin(), a.end()
#define INF 1000000007
const int MOD = 1000000007;
// accumulate(vec.begin(), vec.end(), 0)

int gcd(int x, int y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}

bool isPrime(int n) {
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;

  for (int i = 3; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  double sum = 0;
  int cnt = 0;
  vector<int> x(n);
  vector<int> y(n);
  vector<int> order(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  for (int i = 0; i < n; i++)
    order[i] = i;

  do {
    for (int i = 0; i < n - 1; i++) {
      double x_diff = x[order[i]] - x[order[i + 1]];
      double y_diff = y[order[i]] - y[order[i + 1]];
      double distance = x_diff * x_diff + y_diff * y_diff;
      sum += sqrt(distance);
    }
    cnt++;
  } while (next_permutation(All(order)));

  printf("%.10Lf\n", sum / cnt);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define All(a) a.begin(), a.end()
#define INF 1000000007
const int MOD = 1000000007;
// accumulate(vec.begin(), vec.end(), 0)

int gcd(int x, int y) {
  if (x % y == 0)
    return y;
  else
    return gcd(y, x % y);
}

bool isPrime(int n) {
  if (n < 2)
    return false;
  else if (n == 2)
    return true;
  else if (n % 2 == 0)
    return false;

  for (int i = 3; i <= sqrt(n); i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}

int main() {
  int n;
  cin >> n;
  double sum = 0;
  int cnt = 0;
  vector<int> x(n);
  vector<int> y(n);
  vector<int> order(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  for (int i = 0; i < n; i++)
    order[i] = i;

  do {
    for (int i = 0; i < n - 1; i++) {
      double x_diff = x[order[i]] - x[order[i + 1]];
      double y_diff = y[order[i]] - y[order[i + 1]];
      double distance = x_diff * x_diff + y_diff * y_diff;
      sum += sqrt(distance);
    }
    cnt++;
  } while (next_permutation(All(order)));

  printf("%.10f\n", sum / cnt);
}
Problem p02861 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

struct Point {
  double x;
  double y;
  Point() {}
  Point(int x, int y) {
    this->x = x;
    this->y = y;
  }
};

long long factorial(int n) {
  if (n == 1)
    return 1;
  else
    return n * factorial(n - 1);
}

bool operator<(const Point &ls, const Point &rs) {
  if (ls.x < rs.x)
    return true;
  if (ls.x == rs.x)
    return ls.x < rs.y;
  return false;
}

int main() {
  int n;
  cin >> n;
  vector<Point> v;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back(Point(x, y));
  }
  sort(v.begin(), v.end());
  double sum = 0;
  do {
    for (int i = 0; i < n - 1; i++) {
      double xdiff = v[i].x - v[i + 1].x;
      double ydiff = v[i].y - v[i + 1].y;
      sum += sqrt(xdiff * xdiff + ydiff * ydiff);
    }
  } while (next_permutation(v.begin(), v.end()));
  printf("%.7f\n", sum / factorial(n));
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

struct Point {
  double x;
  double y;
  Point() {}
  Point(int x, int y) {
    this->x = x;
    this->y = y;
  }
};

long long factorial(int n) {
  if (n == 1)
    return 1;
  else
    return n * factorial(n - 1);
}

bool operator<(const Point &ls, const Point &rs) {
  if (ls.x < rs.x)
    return true;
  if (ls.x == rs.x)
    return ls.y < rs.y;
  return false;
}

int main() {
  int n;
  cin >> n;
  vector<Point> v;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back(Point(x, y));
  }
  sort(v.begin(), v.end());
  double sum = 0;
  do {
    for (int i = 0; i < n - 1; i++) {
      double xdiff = v[i].x - v[i + 1].x;
      double ydiff = v[i].y - v[i + 1].y;
      sum += sqrt(xdiff * xdiff + ydiff * ydiff);
    }
  } while (next_permutation(v.begin(), v.end()));
  printf("%.7f\n", sum / factorial(n));
}
Problem p02861 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
/*
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iomainp>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
*/
typedef long long ll;
typedef unsigned long long ull;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> x(N), y(N);
  REP(i, N) {
    cin >> x[i];
    cin >> y[i];
  }
  x[N] = x[0];
  y[N] = y[0];
  double long tmp = 0;
  REP(i, N) {
    REP(j, N) {

      if (i == j) {
        ;
      } else {
        int x_diff = x[i] - x[j];
        ll x_val = x_diff * x_diff;
        int y_diff = y[i] - y[j];
        ll y_val = y_diff * y_diff;
        long double val = sqrt(x_val + y_val);
        tmp += val;
      }
    }
  }

  long double ans = tmp / N;
  cout << fixed << setprecision(10) << ans << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
/*
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iomainp>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>
*/
typedef long long ll;
typedef unsigned long long ull;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N;
  cin >> N;
  vector<int> x(N + 1), y(N + 1);
  REP(i, N) {
    cin >> x[i];
    cin >> y[i];
  }
  x[N] = x[0];
  y[N] = y[0];
  double long tmp = 0;
  REP(i, N) {
    REP(j, N) {

      if (i == j) {
        ;
      } else {
        int x_diff = x[i] - x[j];
        ll x_val = x_diff * x_diff;
        int y_diff = y[i] - y[j];
        ll y_val = y_diff * y_diff;
        long double val = sqrt(x_val + y_val);
        tmp += val;
      }
    }
  }

  long double ans = tmp / N;
  cout << fixed << setprecision(10) << ans << "\n";

  return 0;
}
Problem p02861 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
#define endl "\n"
#define ll long long int
#define vi vector<ll>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<ll, ll>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define pb push_back;
#define f first
#define s second

using namespace std;

signed main() {

  std::ios::sync_with_stdio(false);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  double a, b, f = 1;
  string s;
  std::vector<double> vec[n + 1];
  for (int i = 1; i <= n; ++i) {
    /* code */
    cin >> a >> b;
    vec[i].push_back(a);
    vec[i].push_back(b);
    s += '0' + i;
    f *= i;
  }
  double ans = 0;
  do {
    for (int i = 1; i < n; ++i) {
      /* code */
      int r = s[i] - '0';
      int t = s[i - 1] - '0';
      double c = pow(vec[r][0] - vec[t][0], 2);
      double d = pow(vec[r][1] - vec[t][1], 2);
      ans += sqrt(c + d);
    }
  } while (next_permutation(s.begin(), s.end()));
  cout << fixed << setprecision(10) << ans / f;
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>
#define endl "\n"
#define ll long long int
#define vi vector<ll>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<ll, ll>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define pb push_back;
#define f first
#define s second

using namespace std;

signed main() {

  int n;
  cin >> n;
  double a, b, f = 1;
  string s;
  std::vector<double> vec[n + 1];
  for (int i = 1; i <= n; ++i) {
    /* code */
    cin >> a >> b;
    vec[i].push_back(a);
    vec[i].push_back(b);
    s += '0' + i;
    f *= i;
  }
  double ans = 0;
  do {
    for (int i = 1; i < n; ++i) {
      /* code */
      int r = s[i] - '0';
      int t = s[i - 1] - '0';
      double c = pow(vec[r][0] - vec[t][0], 2);
      double d = pow(vec[r][1] - vec[t][1], 2);
      ans += sqrt(c + d);
    }
  } while (next_permutation(s.begin(), s.end()));
  cout << fixed << setprecision(10) << ans / f;
  return 0;
}
Problem p02861 (Python)

Buggy submission (status: Runtime Error):
from itertools import permutations
from functools import reduce
from operator import mul


def solve(string):
    n, *xy = map(int, string.split())
    (*xy,) = zip(*[iter(xy)] * 2)
    return str(
        sum(
            sum(
                ((xy[p0][0] - xy[p1][0]) ** 2 + (xy[p0][1] - xy[p1][1]) ** 2) ** 0.5
                for p0, p1 in zip(list(i), list(i)[1:])
            )
            for i in permutations(range(n), n)
        )
        / reduce(mul, range(1, n + 1))
    )


if __name__ == "__main__":
    print(solve(input()))


Fixed submission:
from itertools import permutations
from functools import reduce
from operator import mul


def solve(string):
    n, *xy = map(int, string.split())
    (*xy,) = zip(*[iter(xy)] * 2)
    return str(
        sum(
            sum(
                ((xy[p0][0] - xy[p1][0]) ** 2 + (xy[p0][1] - xy[p1][1]) ** 2) ** 0.5
                for p0, p1 in zip(list(i), list(i)[1:])
            )
            for i in permutations(range(n), n)
        )
        / reduce(mul, range(1, n + 1))
    )


if __name__ == "__main__":
    n = int(input())
    print(solve("{}\n".format(n) + "\n".join([input() for _ in range(n)])))
Problem p02861 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iterator>
#include <set>
using namespace std;
#define forr(a, n) for (int i = a; i <= n; i++)
#define MAX 1000000007LL
#define mod 998244353
#define forn(n, a) for (int i = n; i >= a; i--)
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pii pair<int, int>
#define ll long long
const int N = (int)1e5 + 9;
ll power(ll x, ll y) {
  ll ans = 1;
  while (y > 0) {
    if (y % 2 == 1)
      ans = ans * x;
    x = x * x;
    y = y >> 1;
  }
  return ans;
}
int n;
ll fact[10];
vector<int> x;
vector<int> y;
string s = "";
double ans = 0;
int main() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
// freopen("out.txt" , "w" , stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  fact[0] = 1;
  fact[1] = 1;
  forr(2, 9) fact[i] = fact[i - 1] * i;
  cin >> n;
  forr(0, n - 1) {
    s = s + to_string(i);
    int a, b;
    cin >> a >> b;
    x.pb(a);
    y.pb(b);
  }

  do {
    // cout<<s<<"\n";
    double value = 0;
    for (int i = 1; i < n; i++) {
      int j = int(s[i]) - 48;
      int k = int(s[i - 1]) - 48;
      value = value + sqrt((x[j] - x[k]) * (x[j] - x[k]) +
                           (y[j] - y[k]) * (y[j] - y[k]));
    }
    // cout<<s<<" "<<value<<"\n";
    ans += value;
  } while (next_permutation(s.begin(), s.end()));

  ans = (double)ans / (double)fact[n];
  printf("%.10f", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iterator>
#include <set>
using namespace std;
#define forr(a, n) for (int i = a; i <= n; i++)
#define MAX 1000000007LL
#define mod 998244353
#define forn(n, a) for (int i = n; i >= a; i--)
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pii pair<int, int>
#define ll long long
const int N = (int)1e5 + 9;
ll power(ll x, ll y) {
  ll ans = 1;
  while (y > 0) {
    if (y % 2 == 1)
      ans = ans * x;
    x = x * x;
    y = y >> 1;
  }
  return ans;
}
int n;
ll fact[10];
vector<int> x;
vector<int> y;
string s = "";
double ans = 0;
int main() {
  // #ifndef ONLINE_JUDGE
  // freopen("in.txt" , "r" , stdin);
  // // freopen("out.txt" , "w" , stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  fact[0] = 1;
  fact[1] = 1;
  forr(2, 9) fact[i] = fact[i - 1] * i;
  cin >> n;
  forr(0, n - 1) {
    s = s + to_string(i);
    int a, b;
    cin >> a >> b;
    x.pb(a);
    y.pb(b);
  }

  do {
    // cout<<s<<"\n";
    double value = 0;
    for (int i = 1; i < n; i++) {
      int j = int(s[i]) - 48;
      int k = int(s[i - 1]) - 48;
      value = value + sqrt((x[j] - x[k]) * (x[j] - x[k]) +
                           (y[j] - y[k]) * (y[j] - y[k]));
    }
    // cout<<s<<" "<<value<<"\n";
    ans += value;
  } while (next_permutation(s.begin(), s.end()));

  ans = (double)ans / (double)fact[n];
  printf("%.10f", ans);
  return 0;
}
Problem p02861 (Python)

Buggy submission (status: Runtime Error):
from itertools import permutations

import numpy as np

N = int(input())

xys = []
for _ in range(N):
    xys.append(list(map(int, input().split())))

combs = np.array(list(permutations(xys, N)))
ret = np.array(
    [np.linalg.norm(combs[:, i] - combs[:, i + 1], axis=1) for i in range(N - 1)]
).sum() / len(combs)

print(f"{ret:.10f}")


Fixed submission:
from itertools import permutations

import numpy as np

N = int(input())

xys = []
for _ in range(N):
    xys.append(list(map(int, input().split())))

combs = np.array(list(permutations(xys, N)))
ret = np.array(
    [np.linalg.norm(combs[:, i] - combs[:, i + 1], axis=1) for i in range(N - 1)]
).sum() / len(combs)

print("{:.10f}".format(ret))
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; --i)
#define pp pair<ll, ll>
#define ppp pair<ll, pp>
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define fi first
#define se second
const ll mod = 1e9 + 7, INF = 1e16;
const ll N = 1000010;
ll fact[N], inv[N];
ll ppow(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      ans = ans * x;
    x = x * x;
    n >>= 1;
    x %= mod;
    ans %= mod;
  }
  return ans;
}
void setinv(ll n) {
  fact[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % mod; // 階乗を求める
    inv[i] =
        ppow(fact[i], (ll)mod - 2) % mod; // フェルマーの小定理で逆元を求める
  }
}
ll conv(ll n, ll r) { // nCr
  if (n - r < 0)
    return 0;
  return fact[n] * inv[r] % mod * inv[n - r] % mod;
}
ll x, y, a, b;
int main() {
  setinv(1000005);
  cin >> x >> y;
  b = (2 * y - x) / 3;
  if ((2 * y - x) % 3 || b < 0) {
    cout << 0 << endl;
    return 0;
  }
  a = y - 2 * b;
  cout << conv(a + b, a) << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; --i)
#define pp pair<ll, ll>
#define ppp pair<ll, pp>
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define fi first
#define se second
const ll mod = 1e9 + 7, INF = 1e16;
const ll N = 1000010;
ll fact[N], inv[N];
ll ppow(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      ans = ans * x;
    x = x * x;
    n >>= 1;
    x %= mod;
    ans %= mod;
  }
  return ans;
}
void setinv(ll n) {
  fact[0] = 1;
  inv[0] = 1;
  for (int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % mod; // 階乗を求める
    inv[i] =
        ppow(fact[i], (ll)mod - 2) % mod; // フェルマーの小定理で逆元を求める
  }
}
ll conv(ll n, ll r) { // nCr
  if (n - r < 0)
    return 0;
  return fact[n] * inv[r] % mod * inv[n - r] % mod;
}
ll x, y, a, b;
int main() {
  setinv(1000005);
  cin >> x >> y;
  b = (2 * y - x) / 3;
  if ((2 * y - x) % 3 || b < 0) {
    cout << 0 << endl;
    return 0;
  }
  a = y - 2 * b;
  if (a < 0) {
    cout << 0 << endl;
    return 0;
  }
  cout << conv(a + b, a) << endl;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const int mod = 1e9 + 7;
const int M = 510000;

ll fac[M], finv[M], inv[M];

// 前処理
void Cinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < M; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll C(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
  } else if (2 * y - x < 0 || 2 * x - y < 0) {
    cout << 0 << endl;
  } else {
    ll n = (2 * y - x) / 3;
    ll m = (2 * x - y) / 3;
    Cinit();
    cout << C(n + m, m) << endl;
  }
}


Fixed submission:
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const int mod = 1e9 + 7;
const int M = 10000000;

ll fac[M], finv[M], inv[M];

// 前処理
void Cinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < M; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

ll C(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

int main() {
  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3) {
    cout << 0 << endl;
  } else if (2 * y - x < 0 || 2 * x - y < 0) {
    cout << 0 << endl;
  } else {
    ll n = (2 * y - x) / 3;
    ll m = (2 * x - y) / 3;
    Cinit();
    cout << C(n + m, m) << endl;
  }
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <math.h>
#include <time.h>
using namespace std;
long long mod = 1000000007; // 10^9 + 7
//---------------------------------------------------------------------------------------------------
vector<long long> f(1000010);
vector<long long> rf(1000010);
long long n;
long long inv(long long x) {
  long long res = 1;
  long long k = mod - 2;
  long long y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod; // kが存在かつ値 k == 1
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}
void init() {
  f.at(0) = 1;
  for (long long i = 1; i < 1000010; i++) {
    f.at(i) = (f.at(i - 1) * i) % mod;
    rf.at(i) = inv(f.at(i)); // 逆元も同時にメモ
  } // n!をmodで割っていったものを求めている
}
//---------------------------------------------------------------------------------------------------
long long C(int n, int k) {
  long long a = f.at(n);      // = n!%mod
  long long b = rf.at(n - k); // = (n-k)!%mod
  long long c = rf.at(k);     // = k!%mod

  long long bc = (b * c) % mod;

  return (a * bc) % mod;
}
/*メンチン待ちクイズ*/
void solve() {
  long long x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0 || y > 2 * x || x > 2 * y) {
    cout << 0 << endl;
  } // 絶対に到達しようがないとき
  /*nCkについて考える*/
  n = (x + y) / 3;
  long long k;
  k = (x - n);
  k = min(k, n - k);
  if (k == 0) {
    cout << 1 << endl;
  } else {
    init();
    cout << C(n, k) << endl;
  }
}
int main() {
  solve();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#include <math.h>
#include <time.h>
using namespace std;
long long mod = 1000000007; // 10^9 + 7
//---------------------------------------------------------------------------------------------------
vector<long long> f(1000010);
vector<long long> rf(1000010);
long long n;
long long inv(long long x) {
  long long res = 1;
  long long k = mod - 2;
  long long y = x;
  while (k) {
    if (k & 1)
      res = (res * y) % mod; // kが存在かつ値 k == 1
    y = (y * y) % mod;
    k /= 2;
  }
  return res;
}
void init() {
  f.at(0) = 1;
  for (long long i = 1; i < 1000010; i++) {
    f.at(i) = (f.at(i - 1) * i) % mod;
    rf.at(i) = inv(f.at(i)); // 逆元も同時にメモ
  } // n!をmodで割っていったものを求めている
}
//---------------------------------------------------------------------------------------------------
long long C(int n, int k) {
  long long a = f.at(n);      // = n!%mod
  long long b = rf.at(n - k); // = (n-k)!%mod
  long long c = rf.at(k);     // = k!%mod

  long long bc = (b * c) % mod;

  return (a * bc) % mod;
}
/*メンチン待ちクイズ*/
void solve() {
  long long x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0 || y > 2 * x || x > 2 * y) {
    cout << 0 << endl;
  } // 絶対に到達しようがないとき
  else {
    /*nCkについて考える*/
    n = (x + y) / 3;
    long long k;
    k = (x - n);
    k = min(k, n - k);
    if (k == 0) {
      cout << 1 << endl;
    } else {
      init();
      cout << C(n, k) << endl;
    }
  }
}
int main() {
  solve();
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

#define int long long

template <class T> void fr(T &a) {
  T w = 0, s = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      s = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  a = w * s;
}
template <class T> T fr() {
  T a;
  fr(a);
  return a;
}
#define R() fr<int>()

const int p = 1e9 + 7, N = 1e6;
int fac[N + 10] = {1};

int qpow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans = ans * a % p;
    a = a * a % p, b /= 2;
  }
  return ans;
}

int C(int n, int m) {
  return fac[n] * qpow(fac[n - m], p - 2) % p * qpow(fac[m], p - 2) % p;
}

signed main() {
  int x = R(), y = R(), z = (x + y) / 3, a = y - z, b = x - z;
  if (z * 3 != x + y)
    return !printf("0\n");
  else {
    for (int i = 1; i <= N; i++)
      fac[i] = fac[i - 1] * i % p;
    if (a >= 0)
      return !printf("%lld\n", C(z, a));
    return !printf("0\n");
  }
  return 0;
}
/*
(i,j) to (i+1,j+2) = a
(i,j) to (i+2,j+1) = b

a+2b=x
2a+b=y

3a+3b=x+y

a+b=(x+y)/3

a=y-(x+y)/3
b=x-(x+y)/3
*/

Fixed submission:
#include <cstdio>

#define int long long

template <class T> void fr(T &a) {
  T w = 0, s = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      s = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    w = w * 10 + c - '0', c = getchar();
  a = w * s;
}
template <class T> T fr() {
  T a;
  fr(a);
  return a;
}
#define R() fr<int>()

const int p = 1e9 + 7, N = 1e6;
int fac[N + 10] = {1};

int qpow(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans = ans * a % p;
    a = a * a % p, b /= 2;
  }
  return ans;
}

int C(int n, int m) {
  return fac[n] * qpow(fac[n - m], p - 2) % p * qpow(fac[m], p - 2) % p;
}

signed main() {
  int x = R(), y = R(), z = (x + y) / 3, a = y - z, b = x - z;
  if (z * 3 != x + y)
    return !printf("0\n");
  else {
    for (int i = 1; i <= N; i++)
      fac[i] = fac[i - 1] * i % p;
    if (a >= 0 && a <= z)
      return !printf("%lld\n", C(z, a));
    return !printf("0\n");
  }
  return 0;
}
/*
(i,j) to (i+1,j+2) = a
(i,j) to (i+2,j+1) = b

a+2b=x
2a+b=y

3a+3b=x+y

a+b=(x+y)/3

a=y-(x+y)/3
b=x-(x+y)/3
*/
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAX = 1e6;
const int MOD = 1e9 + 7;

int mult(int a, int b) { return (a * 1ll * b) % MOD; }

int binpow(int a) {
  int res = 1;
  int b = MOD - 2;
  while (b) {
    if (b & 1)
      res = mult(res, a);
    a = mult(a, a);
    b >>= 1;
  }
  return res;
}

int f[MAX * 3];

int calc() {
  f[0] = 1;
  for (int i = 1; i <= MAX * 2; ++i)
    f[i] = mult(f[i - 1], i);
}

int c(int n, int k) { return mult(f[n], binpow(mult(f[k], f[n - k]))); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3)
    cout << 0, exit(0);
  int m = -1, n = -1;
  for (int i = 0; i <= MAX; ++i) {
    m = x - 2 * i;
    if (2 * m + i == y) {
      n = i;
      break;
    }
  }
  if (m == -1 || n == -1)
    cout << 0, exit(0);
  calc();
  cout << c(n + m, m);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAX = 1e6;
const int MOD = 1e9 + 7;

int mult(int a, int b) { return (a * 1ll * b) % MOD; }

int binpow(int a) {
  int res = 1;
  int b = MOD - 2;
  while (b) {
    if (b & 1)
      res = mult(res, a);
    a = mult(a, a);
    b >>= 1;
  }
  return res;
}

int f[MAX * 3];

int calc() {
  f[0] = 1;
  for (int i = 1; i <= MAX * 2; ++i)
    f[i] = mult(f[i - 1], i);
}

int c(int n, int k) { return mult(f[n], binpow(mult(f[k], f[n - k]))); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int x, y;
  cin >> x >> y;
  if ((x + y) % 3)
    cout << 0, exit(0);
  int m = -1, n = -1;
  for (int i = 0; i <= MAX; ++i) {
    m = x - 2 * i;
    if (2 * m + i == y) {
      n = i;
      break;
    }
  }
  if (m < 0 || n < 0)
    cout << 0, exit(0);
  calc();
  cout << c(n + m, m);
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
using namespace std;
ll bigmod(ll a, ll b, ll m) {
  ll ans = 1;
  a %= m;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b >>= 1;
    a = (a * a) % m;
  }
  return ans;
}
ll facto[1000009];
int main() {
  facto[0] = facto[1] = 1;
  for (int i = 1; i <= 1000002; i++)
    facto[i] = (facto[i - 1] * i) % mod;
  ll x, y;
  cin >> x >> y;
  ll d = x + y;
  if (d % 3)
    cout << 0 << endl;
  else {
    d /= 3;
    if (x - d < 0)
      cout << 0 << endl;
    else {
      ll e = bigmod(facto[x - d], mod - 2, mod);
      ll f = bigmod(facto[2 * d - x], mod - 2, mod);
      ll ans = (facto[d] * e) % mod;
      ans = ans * f;
      ans %= mod;
      cout << ans << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define filein freopen("input.txt", "r", stdin)
#define fileout freopen("output.txt", "w", stdout)
using namespace std;
ll bigmod(ll a, ll b, ll m) {
  ll ans = 1;
  a %= m;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b >>= 1;
    a = (a * a) % m;
  }
  return ans;
}
ll facto[1000009];
int main() {
  facto[0] = facto[1] = 1;
  for (int i = 1; i <= 1000002; i++)
    facto[i] = (facto[i - 1] * i) % mod;
  ll x, y;
  cin >> x >> y;
  ll d = x + y;
  if (d % 3)
    cout << 0 << endl;
  else {
    d /= 3;
    if (x - d < 0)
      cout << 0 << endl;
    else if (2 * d - x < 0)
      cout << 0 << endl;
    else {
      ll e = bigmod(facto[x - d], mod - 2, mod);
      ll f = bigmod(facto[2 * d - x], mod - 2, mod);
      ll ans = (facto[d] * e) % mod;
      ans = ans * f;
      ans %= mod;
      cout << ans << endl;
    }
  }
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int nMax = 2000001, MOD = 1e9 + 7;
ULL inv[nMax], Fact[nMax], invF[nMax];
ULL Comb(int n, int r) {
  return (((Fact[n] * invF[r]) % MOD) * invF[n - r]) % MOD;
}

int main() {
  inv[1] = Fact[0] = Fact[1] = invF[0] = invF[1] = 1;
  for (int i = 2; i < nMax; i++) {
    inv[i] = ((MOD - MOD / i) * inv[MOD % i]) % MOD;
    Fact[i] = (Fact[i - 1] * i) % MOD;
    invF[i] = (invF[i - 1] * inv[i]) % MOD;
  }
  int X, Y, A, B, ans = 0;
  cin >> X >> Y;
  int K = abs(Y - X);
  if (X <= Y) {
    A = X - K;
    B = Y - 2 * K;
  } else {
    A = Y - K;
    B = X - 2 * K;
  }
  if (A == B && A % 3 == 0) {
    ans = Comb(2 * A / 3 + K, A / 3);
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int nMax = 2000001, MOD = 1e9 + 7;
ULL inv[nMax], Fact[nMax], invF[nMax];
ULL Comb(int n, int r) {
  return (((Fact[n] * invF[r]) % MOD) * invF[n - r]) % MOD;
}

int main() {
  inv[1] = Fact[0] = Fact[1] = invF[0] = invF[1] = 1;
  for (int i = 2; i < nMax; i++) {
    inv[i] = ((MOD - MOD / i) * inv[MOD % i]) % MOD;
    Fact[i] = (Fact[i - 1] * i) % MOD;
    invF[i] = (invF[i - 1] * inv[i]) % MOD;
  }
  int X, Y, A, B, ans = 0;
  cin >> X >> Y;
  int K = abs(Y - X);
  if (X <= Y) {
    A = X - K;
    B = Y - 2 * K;
  } else {
    A = Y - K;
    B = X - 2 * K;
  }
  if (A == B && A % 3 == 0 && A >= 0) {
    ans = Comb(2 * A / 3 + K, A / 3);
  }
  cout << ans << "\n";
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

int n, m;
int mod = 1000000007;
vector<long long> fac, inv, finv;

void combinit() {
  fac.resize(n + m + 1);
  inv.resize(n + m + 1);
  finv.resize(n + m + 1);

  fac[0] = fac[1] = 1;
  inv[1] = 1;
  finv[0] = finv[1] = 1;
  for (int i = 2; i < n + m + 1; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

long long combination(int n, int k) {
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

int main(int argc, char const *argv[]) {
  long long X, Y;
  cin >> X >> Y;
  int ans;
  if ((X + Y) % 3 == 0) {
    m = (2 * Y - X) / 3;
    n = Y - 2 * m;
    combinit();
    ans = (int)combination(n + m, n);
  } else {
    ans = 0;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

int n, m;
int mod = 1000000007;
vector<long long> fac, inv, finv;

void combinit() {
  fac.resize(n + m + 1);
  inv.resize(n + m + 1);
  finv.resize(n + m + 1);

  fac[0] = fac[1] = 1;
  inv[1] = 1;
  finv[0] = finv[1] = 1;
  for (int i = 2; i < n + m + 1; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    finv[i] = finv[i - 1] * inv[i] % mod;
  }
}

long long combination(int n, int k) {
  return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}

int main(int argc, char const *argv[]) {
  long long X, Y;
  cin >> X >> Y;
  int ans;
  if ((X + Y) % 3 == 0) {
    m = (2 * Y - X) / 3;
    n = Y - 2 * m;
    if (n < 0 || m < 0) {
      ans = 0;
      cout << ans << endl;
      return 0;
    }
    combinit();
    ans = (int)combination(n + m, n);
  } else {
    ans = 0;
  }

  cout << ans << endl;

  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int m = 1e6, M = 1e9 + 7;
ll inverse[3 * m], fact[3 * m], invfact[3 * m];

ll nCr(ll n, ll r) {
  ll ans = fact[n] * invfact[r] % M;
  return (ans * invfact[n - r] % M);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  // good luck, kittu

  inverse[1] = 1;
  for (ll i = 2; i < 3 * m; i++)
    inverse[i] = M - M / i * inverse[M % i] % M;
  fact[0] = invfact[0] = 1;
  for (ll i = 1; i < 3 * m; i++) {
    fact[i] = fact[i - 1] * i % M;
    invfact[i] = invfact[i - 1] * inverse[i] % M;
  }

  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0)
    return cout << "0\n", 0;

  ll n = (2 * y - x) / 3, m = (2 * x - y) / 3;
  ll ans = nCr(n + m, n);
  ans %= M;
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int m = 1e6, M = 1e9 + 7;
ll inverse[3 * m], fact[3 * m], invfact[3 * m];

ll nCr(ll n, ll r) {
  ll ans = fact[n] * invfact[r] % M;
  return (ans * invfact[n - r] % M);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  // good luck, kittu

  inverse[1] = 1;
  for (ll i = 2; i < 3 * m; i++)
    inverse[i] = M - M / i * inverse[M % i] % M;
  fact[0] = invfact[0] = 1;
  for (ll i = 1; i < 3 * m; i++) {
    fact[i] = fact[i - 1] * i % M;
    invfact[i] = invfact[i - 1] * inverse[i] % M;
  }

  ll x, y;
  cin >> x >> y;
  if ((x + y) % 3 != 0)
    return cout << "0\n", 0;

  ll n = (2 * y - x) / 3, m = (2 * x - y) / 3;
  if (n < 0 || m < 0)
    return cout << "0\n", 0;

  ll ans = nCr(n + m, n);
  ans %= M;
  cout << ans << "\n";
  return 0;
}
Problem p02862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>

#define MOD_BIG 1000000007
#define MOD_BIG2 998244353
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using ll = long long;
using std::vector;

template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
  vec.resize(len);
  for (auto &v : vec)
    ndarray(v, args...);
}

//////////////////////////////////////

//////////////////////////////////////

int main() {

  ll X, Y;
  cin >> X >> Y;

  if ((2 * X - Y) % 3 != 0 || (-X + 2 * Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll a, b, n;
  a = (2 * X - Y) / 3;
  b = (-X + 2 * Y) / 3;
  n = a + b;

  vector<ll> m(n + 1), mi(n + 1), inv(n + 1);
  m[0] = 1;
  m[1] = 1;
  mi[0] = 1;
  mi[1] = 1;
  inv[1] = 1;
  for (int i = 2; i <= n; i++) {
    m[i] = m[i - 1] * i % MOD_BIG;
    inv[i] = MOD_BIG - inv[MOD_BIG % i] * (MOD_BIG / i) % MOD_BIG;
    mi[i] = mi[i - 1] * inv[i] % MOD_BIG;
  }

  ll ans = m[n] * (mi[a] * mi[b] % MOD_BIG) % MOD_BIG;

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <fstream>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>

#define MOD_BIG 1000000007
#define MOD_BIG2 998244353
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;
using ll = long long;
using std::vector;

template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args>
void ndarray(vector<T> &vec, int len, Args... args) {
  vec.resize(len);
  for (auto &v : vec)
    ndarray(v, args...);
}

//////////////////////////////////////

//////////////////////////////////////

int main() {

  ll X, Y;
  cin >> X >> Y;

  if ((2 * X - Y) % 3 != 0 || (-X + 2 * Y) % 3 != 0) {
    cout << 0 << endl;
    return 0;
  }
  ll a, b, n;
  a = (2 * X - Y) / 3;
  b = (-X + 2 * Y) / 3;
  n = a + b;

  if (a < 0 || b < 0) {
    cout << 0 << endl;
    return 0;
  }
  vector<ll> m(n + 1), mi(n + 1), inv(n + 1);
  m[0] = 1;
  m[1] = 1;
  mi[0] = 1;
  mi[1] = 1;
  inv[1] = 1;
  for (int i = 2; i <= n; i++) {
    m[i] = m[i - 1] * i % MOD_BIG;
    inv[i] = MOD_BIG - inv[MOD_BIG % i] * (MOD_BIG / i) % MOD_BIG;
    mi[i] = mi[i - 1] * inv[i] % MOD_BIG;
  }

  ll ans = m[n] * (mi[a] * mi[b] % MOD_BIG) % MOD_BIG;

  cout << ans << endl;

  return 0;
}