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

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
int main(void) {
  int n, a[100000] = {0}, min = 1000000000, min2 = 1000000000;
  scanf("%d", &n);
  getchar();
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    getchar();
    if (a[i] <= min) {
      min2 = min;
      min = a[i];
    }
  }
  for (int i = min2; i >= 1; i--) {
    int notcount = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] % i != 0) {
        notcount++;
      }
      if (notcount > 1) {
        goto label;
      }
    }
    if (notcount == 0 || notcount == 1) {
      printf("%d\n", i);
      return 0;
    }
  label : {}
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main(void) {
  int n, a[100000] = {0}, min = 1000000000, min2 = 1000000000;
  scanf("%d", &n);
  getchar();
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    getchar();
    if (a[i] <= min) {
      min2 = min;
      min = a[i];
    } else if (a[i] <= min2) {
      min2 = a[i];
    }
  }
  for (int i = min2; i >= 1; i--) {
    int notcount = 0;
    for (int j = 0; j < n; j++) {
      if (a[j] % i != 0) {
        notcount++;
      }
      if (notcount > 1) {
        goto label;
      }
    }
    if (notcount == 0 || notcount == 1) {
      printf("%d\n", i);
      return 0;
    }
  label : {}
  }
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

long long gcd(long long a, long long b) {
  if (a % b != 0) {
    return gcd(b, a % b);
  } else {
    return b;
  }
}

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

  vector<long long> A(N + 1);
  for (int i = 1; i < N + 1; ++i)
    cin >> A[i];

  vector<long long> left(N + 2), right(N);
  left[0] = 0, left[1] = A[1];
  right[N + 1] = 0, right[N] = A[N];
  for (int i = 2; i < N + 1; ++i) {
    left[i] = gcd(A[i], left[i - 1]);
    right[(N + 1) - i] = gcd(A[(N + 1) - i], right[(N + 1) - i + 1]);
  }

  long long gcd_max = 0;
  for (int i = 1; i < N + 1; ++i) {
    int a;
    if (left[i - 1] == 0)
      a = right[i + 1];
    else if (right[i + 1] == 0)
      a = left[i - 1];
    else
      a = gcd(left[i - 1], right[i + 1]);

    if (gcd_max < a)
      gcd_max = a;
  }

  cout << gcd_max << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string>
#include <vector>

using namespace std;

long long gcd(long long a, long long b) {
  if (a % b != 0) {
    return gcd(b, a % b);
  } else {
    return b;
  }
}

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

  vector<long long> A(N + 1);
  for (int i = 1; i < N + 1; ++i)
    cin >> A[i];

  vector<long long> left(N + 2), right(N + 2);
  left[0] = 0, left[1] = A[1];
  right[N + 1] = 0, right[N] = A[N];
  for (int i = 2; i < N + 1; ++i) {
    left[i] = gcd(A[i], left[i - 1]);
    right[(N + 1) - i] = gcd(A[(N + 1) - i], right[(N + 1) - i + 1]);
  }

  long long gcd_max = 0;
  for (int i = 1; i < N + 1; ++i) {
    int a;
    if (left[i - 1] == 0)
      a = right[i + 1];
    else if (right[i + 1] == 0)
      a = left[i - 1];
    else
      a = gcd(left[i - 1], right[i + 1]);

    if (gcd_max < a)
      gcd_max = a;
  }

  cout << gcd_max << endl;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
vector<ll> finddivisor(ll x) { // 整数xの約数が入ったvectorを返すよ(xを含む)
  vector<ll> divisor;
  for (ll i = 1; (i * i) <= x; i++) {
    if (x % i == 0) {
      divisor.push_back(i);
      if (i * i != x) {
        divisor.push_back(x / i);
      } // 約数はペアを持つことを利用
    }
  }
  sort(divisor.begin(), divisor.end());
  // divisor.push_back(x);
  return divisor;
}
int main() {
  ll n, ans = 1;
  cin >> n;
  vector<ll> v(n), a, b;
  for (ll i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  a = finddivisor(v.at(0));
  b = finddivisor(v.at(1));

  for (int i = 0; i < a.size(); i++) {
    ll cnt = 0;
    for (int j = 0; j < n; j++) {
      if (v.at(j) % a.at(i) != 0) {
        cnt++;
      }
      if (cnt >= 2) {
        break;
      }
    }
    if (cnt < 2) {
      ans = max(ans, a.at(i));
    }
  }
  for (int i = 0; i < a.size(); i++) {
    ll cnt = 0;
    for (int j = 0; j < n; j++) {
      if (v.at(j) % b.at(i) != 0) {
        cnt++;
      }
      if (cnt >= 2) {
        break;
      }
    }
    if (cnt < 2) {
      ans = max(ans, b.at(i));
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
vector<ll> finddivisor(ll x) { // 整数xの約数が入ったvectorを返すよ(xを含む)
  vector<ll> divisor;
  for (ll i = 1; (i * i) <= x; i++) {
    if (x % i == 0) {
      divisor.push_back(i);
      if (i * i != x) {
        divisor.push_back(x / i);
      } // 約数はペアを持つことを利用
    }
  }
  sort(divisor.begin(), divisor.end());
  // divisor.push_back(x);
  return divisor;
}
int main() {
  ll n, ans = 1;
  cin >> n;
  vector<ll> v(n), a, b;
  for (ll i = 0; i < n; i++) {
    cin >> v.at(i);
  }
  a = finddivisor(v.at(0));
  b = finddivisor(v.at(1));

  for (int i = 0; i < a.size(); i++) {
    ll cnt = 0;
    for (int j = 0; j < n; j++) {
      if (v.at(j) % a.at(i) != 0) {
        cnt++;
      }
      if (cnt >= 2) {
        break;
      }
    }
    if (cnt < 2) {
      ans = max(ans, a.at(i));
    }
  }
  for (int i = 0; i < b.size(); i++) {
    ll cnt = 0;
    for (int j = 0; j < n; j++) {
      if (v.at(j) % b.at(i) != 0) {
        cnt++;
      }
      if (cnt >= 2) {
        break;
      }
    }
    if (cnt < 2) {
      ans = max(ans, b.at(i));
    }
  }
  cout << ans << endl;
}
Problem p03061 (C++)

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

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> P;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = 10000000000;
/****** define constant end ********/

/****** define variable start ******/
ll n;
vl a;
/******* define variable end *******/
ll gcd(ll a, ll b) {
  while (a && b) {
    if (a >= b)
      a %= b;
    else
      b %= a;
  }
  return a + b;
}
ll solve() {
  cin >> n;
  a.resize(n);
  REP(i, n) { cin >> a[i]; }
  vl l(n);
  vl r(n);
  l[0] = 0;
  r[n] = 0;
  vl m(n);
  REP(j, n) { l[j + 1] = gcd(l[j], a[j]); }
  for (ll j = n - 1; j >= 0; j--) {
    r[j] = gcd(r[j + 1], a[j]);
  }
  REP(i, n) { m[i] = gcd(l[i], r[i + 1]); }
  ll ans = 0;
  REP(i, n) { ans = max(m[i], ans); }
  cout << ans << endl;
  return 0;
}
int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\n", time);
  */
  return (int)end_solve;
}


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

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef pair<ll, ll> P;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)

/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = 10000000000;
/****** define constant end ********/

/****** define variable start ******/
ll n;
vl a;
/******* define variable end *******/
ll gcd(ll a, ll b) {
  while (a && b) {
    if (a >= b)
      a %= b;
    else
      b %= a;
  }
  return a + b;
}
ll solve() {
  cin >> n;
  a.resize(n);
  REP(i, n) { cin >> a[i]; }
  vl l(n + 1);
  vl r(n + 1);
  l[0] = 0;
  r[n] = 0;
  vl m(n);
  REP(j, n) { l[j + 1] = gcd(l[j], a[j]); }
  for (ll j = n - 1; j >= 0; j--) {
    r[j] = gcd(r[j + 1], a[j]);
  }
  REP(i, n) { m[i] = gcd(l[i], r[i + 1]); }
  ll ans = 0;
  REP(i, n) { ans = max(m[i], ans); }
  cout << ans << endl;
  return 0;
}
int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\n", time);
  */
  return (int)end_solve;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
//
// Created on 2019-02-09.
//
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

#define PB push_back
#define INF 1e9
#define MOD 1e9 + 7
#define EPS 1e-9

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

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

  int N;
  cin >> N;

  vector<ll> A(N), l(N, 0), r(N, 0);

  REP(i, N) { cin >> A[i]; }

  for (int i = 0; i < N - 1; ++i)
    l[i + 1] = gcd(l[i], A[i]);
  for (int i = N - 1; i >= 1; --i)
    r[i - 1] = gcd(r[i], A[i]);

  ll ans = 1;
  REP(i, N) ans = max(ans, gcd(l[i], r[i]));

  cout << ans << endl;

  return 0;
}


Fixed submission:
//
// Created on 2019-02-09.
//
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; }

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

#define PB push_back
#define INF 1e9
#define MOD 1e9 + 7
#define EPS 1e-9

typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

ll gcd(ll a, ll b) {
  if (b == 0)
    return (a);
  else
    return (gcd(b, a % b));
}

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

  int N;
  cin >> N;

  vector<ll> A(N), l(N, 0), r(N, 0);

  REP(i, N) { cin >> A[i]; }

  for (int i = 0; i < N - 1; ++i)
    l[i + 1] = gcd(l[i], A[i]);
  for (int i = N - 1; i >= 1; --i)
    r[i - 1] = gcd(r[i], A[i]);

  ll ans = 1;
  REP(i, N) ans = max(ans, gcd(l[i], r[i]));

  cout << ans << endl;

  return 0;
}
Problem p03061 (C++)

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

#define loop(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define dump(a) cerr << #a << " = " << (a) << "(L:" << __LINE__ << ")" << endl

void solve(long long N, std::vector<long long> A) {
  map<long long, int> C;
  rep(i, N) C[A[i]]++;
  set<long long> S(A.begin(), A.end());
  vector<long long> B(S.begin(), S.end());

  N = B.size();
  if (N == 1) {
    cout << B[0] << endl;
  } else {
    long long ans = 0;
    rep(i, N) {
      if (C[B[i]] != 1)
        continue;
      long long tmp = B[i == 0];
      rep(j, N) {
        if (i == j)
          continue;
        tmp = __gcd(tmp, B[j]);
        // if (tmp < ans) break;
      }
      ans = max(ans, tmp);
    }
    assert(ans);
    cout << ans << endl;
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}


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

#define loop(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define dump(a) cerr << #a << " = " << (a) << "(L:" << __LINE__ << ")" << endl

void solve(long long N, std::vector<long long> A) {
  map<long long, int> C;
  rep(i, N) C[A[i]]++;
  set<long long> S(A.begin(), A.end());
  vector<long long> B(S.begin(), S.end());

  N = B.size();
  if (N == 1) {
    cout << B[0] << endl;
  } else {
    long long ans = 0;
    rep(i, N) ans = __gcd(ans, B[i]);
    rep(i, N) {
      if (C[B[i]] != 1)
        continue;
      long long tmp = B[i == 0];
      rep(j, N) {
        if (i == j)
          continue;
        tmp = __gcd(tmp, B[j]);
        // if (tmp < ans) break;
      }
      ans = max(ans, tmp);
    }
    assert(ans);
    cout << ans << endl;
  }
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  solve(N, std::move(A));
  return 0;
}
Problem p03061 (C++)

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

using namespace std;

long gcd(long a, long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  int n;
  cin >> n;
  vector<long> a(n), l(n), r(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  l[0] = 0;
  r[n] = 0;
  for (int i = 0; i < n; i++) {
    l[i + 1] = gcd(l[i], a[i]);
    int j = n - 1 - i;
    r[j] = gcd(r[j + 1], a[j]);
  }

  long res = 1;
  for (int i = 0; i < n; i++) {
    res = max(res, gcd(l[i], r[i + 1]));
  }

  cout << res << endl;
}

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

using namespace std;

long gcd(long a, long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  int n;
  cin >> n;
  vector<long> a(n), l(n + 1), r(n + 1);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  l[0] = 0;
  r[n] = 0;
  for (int i = 0; i < n; i++) {
    l[i + 1] = gcd(l[i], a[i]);
    int j = n - 1 - i;
    r[j] = gcd(r[j + 1], a[j]);
  }

  long res = 1;
  for (int i = 0; i < n; i++) {
    res = max(res, gcd(l[i], r[i + 1]));
  }

  cout << res << endl;
}
Problem p03061 (C++)

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

using namespace std;

#define sp(x) cout << setprecision(x);
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define print(s) cout << (s) << endl
#define lint long long
#define yes "Yes"
#define no "No"

typedef pair<int, int> P;

lint euclid(lint a, lint b) {
  lint temp;

  if (a < b) {
    temp = a;
    a = b;
    b = temp;
  }
  if (b < 1)
    return -1;

  if (a % b == 0)
    return b;
  return euclid(b, a % b);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lint n;
  cin >> n;
  vector<lint> a(n);
  REP(i, n) cin >> a[i];
  vector<lint> l(n);                               // それより左のgcd
  vector<lint> r(n);                               // それより右のgcd
  l[0] = a[0];                                     // l0=a0
  r[n - 1] = a[n - 1];                             // ln-1=an-1
  REP(i, n - 1) l[i + 1] = euclid(a[i + 1], l[i]); // 最初にl1  =gcd(a1,  l0)
  for (int i = n - 1; i >= 0; i--)
    r[i - 1] = euclid(a[i - 1], r[i]); // 最初にln-2=gcd(al-2,ln-1)

  /*
  12 15 18
  --------
  12  3  3 l:そこより左の全てのgcd
  3   3 18 r:そこより右の全てのgcd
  */

  lint tmp = r[1];
  FOR(i, 1, n - 1) { // tmp=a[i]以外のgcdを求め、ans=max(tmp)を求める
    tmp =
        max(tmp, euclid(l[i - 1],
                        r[i + 1])); // tmp=gcd(それより左のgcd,それより右のgcd)
  }
  tmp = max(tmp, l[n - 2]);
  print(tmp);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout << setprecision(x);
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define print(s) cout << (s) << endl
#define lint long long
#define yes "Yes"
#define no "No"

typedef pair<int, int> P;

lint euclid(lint a, lint b) {
  lint temp;

  if (a < b) {
    temp = a;
    a = b;
    b = temp;
  }
  if (b < 1)
    return -1;

  if (a % b == 0)
    return b;
  return euclid(b, a % b);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lint n;
  cin >> n;
  vector<lint> a(n);
  REP(i, n) cin >> a[i];
  vector<lint> l(n);                               // それより左のgcd
  vector<lint> r(n);                               // それより右のgcd
  l[0] = a[0];                                     // l0=a0
  r[n - 1] = a[n - 1];                             // ln-1=an-1
  REP(i, n - 1) l[i + 1] = euclid(a[i + 1], l[i]); // 最初にl1  =gcd(a1,  l0)
  for (int i = n - 1; i > 0; i--)
    r[i - 1] = euclid(a[i - 1], r[i]); // 最初にln-2=gcd(al-2,ln-1)

  /*
  12 15 18
  --------
  12  3  3 l:そこより左の全てのgcd
  3   3 18 r:そこより右の全てのgcd
  */

  lint tmp = r[1];
  FOR(i, 1, n - 1) { // tmp=a[i]以外のgcdを求め、ans=max(tmp)を求める
    tmp =
        max(tmp, euclid(l[i - 1],
                        r[i + 1])); // tmp=gcd(それより左のgcd,それより右のgcd)
  }
  tmp = max(tmp, l[n - 2]);
  print(tmp);
  return 0;
}
Problem p03061 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define foreach(it, v)                                                         \
  for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second

typedef unsigned int uint;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef set<int> si;

int N;
int A[10010];
int l[10010];
int r[10010];

int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  while (y) {
    x = x % y;
    swap(x, y);
  }
  return x;
}

int main() {
  scanf("%d", &N);
  rep(i, N) { scanf("%d", &A[i]); }
  l[0] = A[0];
  for (int i = 1; i < N; ++i)
    l[i] = gcd(l[i - 1], A[i]);
  r[N - 1] = A[N - 1];
  for (int i = N - 2; i >= 0; --i)
    r[i] = gcd(r[i + 1], A[i]);
  int ans = max(l[N - 2], r[1]);
  for (int i = 1; i < N - 1; ++i)
    ans = max(ans, gcd(l[i - 1], r[i + 1]));
  cout << ans << "\n";
  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define foreach(it, v)                                                         \
  for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second

typedef unsigned int uint;
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef set<int> si;

int N;
int A[100010];
int l[100010];
int r[100010];

int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  while (y) {
    x = x % y;
    swap(x, y);
  }
  return x;
}

int main() {
  scanf("%d", &N);
  rep(i, N) { scanf("%d", &A[i]); }
  l[0] = A[0];
  for (int i = 1; i < N; ++i)
    l[i] = gcd(l[i - 1], A[i]);
  r[N - 1] = A[N - 1];
  for (int i = N - 2; i >= 0; --i)
    r[i] = gcd(r[i + 1], A[i]);
  int ans = max(l[N - 2], r[1]);
  for (int i = 1; i < N - 1; ++i)
    ans = max(ans, gcd(l[i - 1], r[i + 1]));
  cout << ans << "\n";
  return 0;
}
Problem p03061 (C++)

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

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> gcd_L(N), gcd_R(N + 1);

  rep(i, N) { cin >> A[i]; }

  gcd_L[0] = 0;
  gcd_R[N] = 0;
  // i番目をかえる
  for (int i = 0; i < N; i++) {
    gcd_L[i + 1] = gcd(A[i], gcd_L[i]);
  }

  for (int i = N - 1; i >= 0; i--) {
    gcd_R[i] = gcd(A[i], gcd_R[i + 1]);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, gcd(gcd_L[i], gcd_R[i + 1]));
  }

  cout << ans << endl;

  /*
  rep(i,N){
      cout << A[i] << " ";
  }cout << endl;
  */
}


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

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> gcd_L(N + 3), gcd_R(N + 3);

  rep(i, N) { cin >> A[i]; }

  gcd_L[0] = 0;
  gcd_R[N] = 0;
  // i番目をかえる
  for (int i = 0; i < N; i++) {
    gcd_L[i + 1] = gcd(A[i], gcd_L[i]);
  }

  for (int i = N - 1; i >= 0; i--) {
    gcd_R[i] = gcd(A[i], gcd_R[i + 1]);
  }

  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, gcd(gcd_L[i], gcd_R[i + 1]));
  }

  cout << ans << endl;

  /*
  rep(i,N){
      cout << A[i] << " ";
  }cout << endl;
  */
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#include <bits/stdc++.h>
#define pb(X) push_back(X)
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);
using namespace std;

int n, a, b, c, d, q, ans;

int gcd(int a, int b) {
  while (a && b) {
    if (a > b) {
      a %= b;
    } else {
      b %= a;
    }
  }
  return max(a, b);
}
int main() {
  INIT cin >> n;
  vector<int> a(n);
  vector<int> g1(n + 2), g2(n + 2);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    g1[i] = gcd(g1[i - 1], a[i]);
  }
  for (int i = n; i > 0; --i) {
    g2[i] = gcd(g2[i + 1], a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, gcd(g1[i - 1], g2[i + 1]));
  }
  cout << ans;
  return 0;
}

Fixed submission:
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#include <bits/stdc++.h>
#define pb(X) push_back(X)
#define INIT                                                                   \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);
using namespace std;

int n, a, b, c, d, q, ans;

int gcd(int a, int b) {
  while (a && b) {
    if (a > b) {
      a %= b;
    } else {
      b %= a;
    }
  }
  return max(a, b);
}
int main() {
  INIT cin >> n;
  vector<int> a(n + 1);
  vector<int> g1(n + 2), g2(n + 2);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    g1[i] = gcd(g1[i - 1], a[i]);
  }
  for (int i = n; i > 0; --i) {
    g2[i] = gcd(g2[i + 1], a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, gcd(g1[i - 1], g2[i + 1]));
  }
  cout << ans;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int gcd(int a, int b);

int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int left, right, max_gcd = 0;
  for (int n = 0; n < N; n++) {
    left = 0;
    right = 0;
    for (int i = 0; i < n; i++)
      left = gcd(left, A[i]);
    for (int i = N - 1; i > n; i--)
      right = gcd(right, A[i]);
    max_gcd = max(max_gcd, gcd(left, right));
  }
  cout << max_gcd << endl;
}

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}


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

int gcd(int a, int b);

int main() {
  int N;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int left[N], right[N], max_gcd = 0;
  left[0] = 0;
  right[N - 1] = 0;
  for (int i = 1; i < N; i++) {
    left[i] = gcd(left[i - 1], A[i - 1]);
    right[N - i - 1] = gcd(right[N - i], A[N - i]);
  }
  for (int i = 0; i < N; i++) {
    max_gcd = max(max_gcd, gcd(left[i], right[i]));
  }
  cout << max_gcd << endl;
}

int gcd(int a, int b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
Problem p03061 (C++)

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

int main() {
  int N;
  cin >> N;
  multiset<long> A;
  long temp;
  for (int i = 0; i < N; i++) {
    cin >> temp;
    A.insert(temp);
  }
  int count;
  long j;
  for (j = *(++A.begin()); j > 0; j--) {
    count = 0;
    for (auto a : A) {
      if (a % j != 0)
        count++;
    }
    if (count < 2)
      break;
  }
  cout << j;
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  multiset<long> A;
  long temp;
  for (int i = 0; i < N; i++) {
    cin >> temp;
    A.insert(temp);
  }
  int count;
  long j;
  for (j = *(++A.begin()); j > 0; j--) {
    count = 0;
    for (auto a : A) {
      if (a % j != 0)
        count++;
      if (count > 1)
        break;
    }
    if (count < 2)
      break;
  }
  cout << j;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define re return
#define ll long long
#define ull unsigned ll
#define ld long double
#define f first
#define s second
#define pi acos(-1)
#define oo (ll)1e9 + 8
#define OO 1e18
#define EPS 1e-9
#define Endl '\n'
#define all(v) (v).begin(), (v).end()
#define FX(n) fixed << setprecision(n)
#define mm(o, k) memset(o, k, sizeof o)
#define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
const int maxn = 20 + 5;
const int N = 1e4 + 5, M = N * 2;
ll a[maxn];
ll awl[maxn];
ll a5r[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  for (int i = 1; i <= n; ++i)
    awl[i] = __gcd(a[i], awl[i - 1]);
  for (int i = n; i >= 1; --i)
    a5r[i] = __gcd(a[i], a5r[i + 1]);
  ll ans = awl[n];
  for (int i = 0; i <= n; ++i)
    ans = max(ans, __gcd(awl[i], a5r[i + 2]));
  printf("%lld\n", ans);
  re 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define re return
#define ll long long
#define ull unsigned ll
#define ld long double
#define f first
#define s second
#define pi acos(-1)
#define oo (ll)1e9 + 8
#define OO 1e18
#define EPS 1e-9
#define Endl '\n'
#define all(v) (v).begin(), (v).end()
#define FX(n) fixed << setprecision(n)
#define mm(o, k) memset(o, k, sizeof o)
#define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
using namespace std;
const int maxn = 1e5 + 5;
const int N = 1e4 + 5, M = N * 2;
ll a[maxn];
ll awl[maxn];
ll a5r[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);
  for (int i = 1; i <= n; ++i)
    awl[i] = __gcd(a[i], awl[i - 1]);
  for (int i = n; i >= 1; --i)
    a5r[i] = __gcd(a[i], a5r[i + 1]);
  ll ans = awl[n];
  for (int i = 0; i <= n; ++i)
    ans = max(ans, __gcd(awl[i], a5r[i + 2]));
  printf("%lld\n", ans);
  re 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define clr(a, x) memset(a, x, sizeof(a))
#define SZ(x) ((int)(x).size())
#define lson rt << 1
#define rson rt << 1 | 1
#define pb push_back
#define fi first
#define se second
#define what_is(x) cerr << #x << " " << x << endl;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename T> inline void _read(T &x) { cin >> x; }
void R() {}
template <typename T, typename... U> void R(T &head, U &...tail) {
  _read(head);
  R(tail...);
}

template <typename T> inline void _write(const T &x) { cout << x << ' '; }
void W() { cout << endl; }
template <typename T, typename... U> void W(const T &head, const U &...tail) {
  _write(head);
  W(tail...);
}

void go();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  go();
  return 0;
}
/****************ackun*********************/
const int MAXN = 25;
int a[MAXN];
int bl[MAXN], br[MAXN];
void go() {
  int n;
  R(n);
  rep(i, 1, n) { R(a[i]); }
  bl[1] = a[1];
  rep(i, 2, n) { bl[i] = __gcd(bl[i - 1], a[i]); }
  br[n] = a[n];
  per(i, n - 1, 1) { br[i] = __gcd(br[i + 1], a[i]); }
  int ans = max(bl[n - 1], br[2]);
  rep(i, 2, n - 1) { ans = max(ans, __gcd(bl[i - 1], br[i + 1])); }
  W(ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define clr(a, x) memset(a, x, sizeof(a))
#define SZ(x) ((int)(x).size())
#define lson rt << 1
#define rson rt << 1 | 1
#define pb push_back
#define fi first
#define se second
#define what_is(x) cerr << #x << " " << x << endl;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename T> inline void _read(T &x) { cin >> x; }
void R() {}
template <typename T, typename... U> void R(T &head, U &...tail) {
  _read(head);
  R(tail...);
}

template <typename T> inline void _write(const T &x) { cout << x << ' '; }
void W() { cout << endl; }
template <typename T, typename... U> void W(const T &head, const U &...tail) {
  _write(head);
  W(tail...);
}

void go();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  go();
  return 0;
}
/****************ackun*********************/
const int MAXN = 1e5 + 10;
int a[MAXN];
int bl[MAXN], br[MAXN];
void go() {
  int n;
  R(n);
  rep(i, 1, n) { R(a[i]); }
  bl[1] = a[1];
  rep(i, 2, n) { bl[i] = __gcd(bl[i - 1], a[i]); }
  br[n] = a[n];
  per(i, n - 1, 1) { br[i] = __gcd(br[i + 1], a[i]); }
  int ans = max(bl[n - 1], br[2]);
  rep(i, 2, n - 1) { ans = max(ans, __gcd(bl[i - 1], br[i + 1])); }
  W(ans);
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
int gcd(int a, int b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}
int main() {
  int n;
  cin >> n;
  int a[100000];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int L[100000];
  for (int i = 1; i < n; i++) {
    L[0] = 0;
    L[i] = gcd(L[i - 1], a[i - 1]);
  }
  int R[100000];
  for (int i = n - 2; i >= 0; i--) {
    R[n - 1] = 0;
    R[i] = gcd(R[i + 1], a[i + 1]);
  }
  int M[100000];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    M[i] = gcd(L[i], R[i]);
    ans = max(ans, M[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
int gcd(int a, int b) {
  if (b == 0) {
    return a;
  } else {
    return (gcd(b, a % b));
  }
}
int main() {
  int n;
  cin >> n;
  int a[100000];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int L[100000];
  for (int i = 1; i < n; i++) {
    L[0] = 0;
    L[i] = gcd(L[i - 1], a[i - 1]);
  }
  int R[100000];
  for (int i = n - 2; i >= 0; i--) {
    R[n - 1] = 0;
    R[i] = gcd(R[i + 1], a[i + 1]);
  }
  int M[100000];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    M[i] = gcd(L[i], R[i]);
    ans = max(ans, M[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

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

ll euq_gcd(ll a, ll b) { return b ? euq_gcd(b, a % b) : a; }

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  for (ll i = 1; i <= N; i++)
    cin >> A[i];

  vector<ll> L(N), R(N + 2);
  L[0] = R[N + 1] = 0;
  for (ll i = 1; i <= N; i++) {
    L[i] = euq_gcd(L[i - 1], A[i]);
    R[N + 1 - i] = euq_gcd(R[N + 2 - i], A[N + 1 - i]);
  }

  ll max = -1;
  for (ll i = 1; i <= N; i++) {
    ll temp = euq_gcd(L[i - 1], R[i + 1]);
    if (temp > max)
      max = temp;
  }

  cout << max << endl;

  return 0;
}

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

ll euq_gcd(ll a, ll b) { return b ? euq_gcd(b, a % b) : a; }

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  for (ll i = 1; i <= N; i++)
    cin >> A[i];

  vector<ll> L(N), R(N + 2);
  L[0] = R[N + 1] = 0;
  for (ll i = 1; i < N; i++) {
    L[i] = euq_gcd(L[i - 1], A[i]);
    R[N + 1 - i] = euq_gcd(R[N + 2 - i], A[N + 1 - i]);
  }

  ll max = -1;
  for (ll i = 1; i <= N; i++) {
    ll temp = euq_gcd(L[i - 1], R[i + 1]);
    if (temp > max)
      max = temp;
  }

  cout << max << endl;

  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
/**
 *    created: 13.05.2020 23:20:39
 **/
#include <bits/stdc++.h>
#define int long long
const int INF = 1e18L + 5;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;

signed main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> l(n), r(n);
  for (int i = 0; i < n; i++)
    l[i + 1] = __gcd(l[i], a[i]);
  for (int i = n - 1; i > 0; i--)
    r[i - 1] = __gcd(r[i], a[i]);

  int ans = 0;
  rep(i, n) ans = max(ans, __gcd(l[i], r[i]));
  cout << ans << endl;
  return 0;
}

Fixed submission:
/**
 *    created: 13.05.2020 23:20:39
 **/
#include <bits/stdc++.h>
#define int long long
const int INF = 1e18L + 5;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;

signed main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> l(n), r(n);
  for (int i = 0; i < n - 1; i++)
    l[i + 1] = __gcd(l[i], a[i]);
  for (int i = n - 1; i > 0; i--)
    r[i - 1] = __gcd(r[i], a[i]);

  int ans = 0;
  rep(i, n) ans = max(ans, __gcd(l[i], r[i]));
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

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

#define INF 1LL << 60
#define pb push_back
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPR(i, b, e) for (int i = (b); i <= (e); i++)

int N;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

/**
 * 累積GCDの計算
 * L[i] := GCD(A[0]..A[i-1])
 * R[i] := GCD(A[i]..A[N-1])
 */
vi L, R;
int cumulativeGcd(vi A) {
  L.resize(N + 1, 0);
  R.resize(N + 1, 0);
  for (int i = 0; i <= N; i++)
    L[i + 1] = gcd(L[i], A[i]);
  for (int i = N - 1; i >= 0; i--)
    R[i] = gcd(R[i + 1], A[i]);
}

int main() {
  cin >> N;
  vi A(N);
  REP(i, N) cin >> A[i];

  cumulativeGcd(A);

  int M = 0;
  REP(i, N) { M = max(M, gcd(L[i], R[i + 1])); }

  cout << M << endl;

  return 0;
}


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

#define INF 1LL << 60
#define pb push_back
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPR(i, b, e) for (int i = (b); i <= (e); i++)

int N;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

/**
 * 累積GCDの計算
 * L[i] := GCD(A[0]..A[i-1])
 * R[i] := GCD(A[i]..A[N-1])
 */
vi L, R;
int cumulativeGcd(vi A) {
  L.resize(N + 1, 0);
  R.resize(N + 1, 0);
  for (int i = 0; i < N; i++)
    L[i + 1] = gcd(L[i], A[i]);
  for (int i = N - 1; i >= 0; i--)
    R[i] = gcd(R[i + 1], A[i]);
}

int main() {
  cin >> N;
  vi A(N);
  REP(i, N) cin >> A[i];

  cumulativeGcd(A);

  int M = 0;
  REP(i, N) { M = max(M, gcd(L[i], R[i + 1])); }

  cout << M << endl;

  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> //sort(all(変数),greater<型名>())　で降順に
#include <cmath>     //切り上げceil(値)
#include <deque>
#include <iomanip> //setprecision(数字)
#include <iostream>
#include <list>
#include <map>
#include <numeric> //xとyの最大公約数は__gcd(x,y),xとyの最小公倍数は(x*y)/__gcd
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <tuple>
#include <utility>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define vll vector<ll>
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define mod 1000000007
using ll = long long;
using namespace std;

int main() {
  ll n;
  cin >> n;
  vll a(n), l(n), r(n + 1), m(n);
  rep(i, n) { cin >> a[i]; }
  l[0] = 0;
  r[n] = 0;
  rep(i, n) { l[i + 1] = __gcd(l[i], a[i]); }
  for (int i = n - 1; i >= 0; i--) {
    r[i] = __gcd(r[i + 1], a[i]);
  }
  rep(i, n) { m[i] = __gcd(l[i], r[i + 1]); }
  int max = 0;
  rep(i, n) {
    if (max < m[i]) {
      max = m[i];
    }
  }
  cout << max;
  return 0;
}


Fixed submission:
#include <algorithm> //sort(all(変数),greater<型名>())　で降順に
#include <cmath>     //切り上げceil(値)
#include <deque>
#include <iomanip> //setprecision(数字)
#include <iostream>
#include <list>
#include <map>
#include <numeric> //xとyの最大公約数は__gcd(x,y),xとyの最小公倍数は(x*y)/__gcd
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <tuple>
#include <utility>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define vll vector<ll>
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define mod 1000000007
using ll = long long;
using namespace std;

int main() {
  ll n;
  cin >> n;
  vll a(n), l(n), r(n + 1), m(n);
  rep(i, n) { cin >> a[i]; }
  l[0] = 0;
  r[n] = 0;
  rep(i, n - 1) { l[i + 1] = __gcd(l[i], a[i]); }
  for (int i = n - 1; i >= 0; i--) {
    r[i] = __gcd(r[i + 1], a[i]);
  }
  rep(i, n) { m[i] = __gcd(l[i], r[i + 1]); }
  int max = 0;
  rep(i, n) {
    if (max < m[i]) {
      max = m[i];
    }
  }
  cout << max;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
/// mAAria

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <stdio.h>
#include <string>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define inf 1000000000
#define mod 1000000007
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep1(i, a, b) for (int i = a; i >= b; i--)
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
#define MAX 500000
#define K 30

int a[11111], pre[11111], suf[11111], ans;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 1; i <= n; i++)
    pre[i] = __gcd(pre[i - 1], a[i]);
  for (int i = n; i >= 1; i--)
    suf[i] = __gcd(suf[i + 1], a[i]);

  for (int i = 1; i <= n; i++)
    ans = max(ans, __gcd(pre[i - 1], suf[i + 1]));
  cout << ans << endl;

  return 0;
}

///....


Fixed submission:
/// mAAria

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <stdio.h>
#include <string>
using namespace __gnu_pbds;
using namespace std;
#define int long long
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
#define inf 1000000000
#define mod 1000000007
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define rep1(i, a, b) for (int i = a; i >= b; i--)
#define dmp(x) cerr << "line " << __LINE__ << " " << #x << ":" << x << endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define mt make_tuple
#define MAX 500000
#define K 30

int a[111111], pre[111111], suf[111111], ans;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  for (int i = 1; i <= n; i++)
    pre[i] = __gcd(pre[i - 1], a[i]);
  for (int i = n; i >= 1; i--)
    suf[i] = __gcd(suf[i + 1], a[i]);

  for (int i = 1; i <= n; i++)
    ans = max(ans, __gcd(pre[i - 1], suf[i + 1]));
  cout << ans << endl;

  return 0;
}

///....
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<char, LL> P;
const LL mod = 998244353;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> ca(n + 1, 0), cb(n + 1, 0);
  for (int i = 0; i <= n; i++) {
    ca[i + 1] = __gcd(ca[i], a[i]);
  }
  for (int i = n; i >= 1; i--) {
    cb[i - 1] = __gcd(cb[i], a[i - 1]);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, __gcd(ca[i], cb[i + 1]));
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long LL;
typedef pair<char, LL> P;
const LL mod = 998244353;
const LL LINF = 1LL << 62;
const int INF = 1 << 30;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> ca(n + 1, 0), cb(n + 1, 0);
  for (int i = 0; i < n; i++) {
    ca[i + 1] = __gcd(ca[i], a[i]);
  }
  for (int i = n; i >= 1; i--) {
    cb[i - 1] = __gcd(cb[i], a[i - 1]);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = max(ans, __gcd(ca[i], cb[i + 1]));
  }
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<long long> data(n);
  for (int i = 0; i < n; i++) {
    cin >> data[i];
  }
  sort(data.begin(), data.end());
  for (long long i = data[1]; i > 0; i--) {
    int c = 0;
    for (int j = 0; j < n; j++) {
      if (data[j] % i != 0)
        c++;
    }
    if (c < 2) {
      cout << i << endl;
      return 0;
    }
  }
}


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

int main() {
  int n;
  cin >> n;
  vector<long long> data(n);
  for (int i = 0; i < n; i++) {
    cin >> data[i];
  }
  sort(data.begin(), data.end());
  for (long long i = data[1]; i > 0; i--) {
    int c = 0;
    for (int j = 0; j < n; j++) {
      if (data[j] % i != 0)
        c++;
      if (c == 2)
        break;
    }
    if (c < 2) {
      cout << i << endl;
      return 0;
    }
  }
}
Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll j = 0; j < N; ++j)
    cin >> A[j];

  vector<ll> cache(N);
  cache[0] = A[1];
  for (ll j = 1; j < N; ++j) {
    cache[j] = A[0];
  }
  for (ll j = 1; j < N; ++j) {
    for (ll k = 0; k < N; ++k) {
      if (j != k)
        cache[k] = gcd(cache[k], A[j]);
    }
  }
  ll ans = cache[0];
  for (ll j = 0; j < N; ++j) {
    ans = max(ans, cache[j]);
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (ll j = 0; j < N; ++j)
    cin >> A[j];

  vector<ll> cache(N);
  cache[0] = A[1];
  for (ll j = 1; j < N; ++j) {
    cache[j] = A[0];
  }
  ll tmp = A[0];
  for (ll j = 2; j < N; ++j) {
    tmp = gcd(tmp, A[j - 1]);
    cache[j] = tmp;
  }
  tmp = A[N - 1];
  for (ll j = N - 2; j >= 0; --j) {
    tmp = gcd(tmp, A[j + 1]);
    cache[j] = gcd(cache[j], tmp);
  }
  ll ans = cache[0];
  for (ll j = 0; j < N; ++j) {
    ans = max(ans, cache[j]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const ll MAX = 510000;
const ll MOD = 1000000007;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];

  vector<ll> right(N);
  vector<ll> left(N);

  left[0] = a[0];
  right[N - 2] = a[N - 1];

  repn(i, N) { left[i] = gcd(left[i - 1], a[i]); }
  for (ll i = N - 3; i >= 0; i--) {
    right[i] = gcd(right[i + 1], a[i + 1]);
  }

  ll ans = 1;
  ans = max(left[N - 2], right[0]);

  rep(i, N) { ans = max(ans, gcd(left[i], right[i + 1])); }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define repn(i, x) for (int i = 1; i <= x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())

const ll MAX = 510000;
const ll MOD = 1000000007;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N);
  rep(i, N) cin >> a[i];

  vector<ll> right(N + 2);
  vector<ll> left(N + 2);

  left[0] = a[0];
  right[N - 2] = a[N - 1];

  repn(i, N) { left[i] = gcd(left[i - 1], a[i]); }
  for (ll i = N - 3; i >= 0; i--) {
    right[i] = gcd(right[i + 1], a[i + 1]);
  }

  ll ans = 1;
  ans = max(left[N - 2], right[0]);

  rep(i, N) { ans = max(ans, gcd(left[i], right[i + 1])); }
  cout << ans << endl;
}
Problem p03061 (C++)

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

int gcd(int a, int b) {
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n), l(n), r(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++)
    l[i + 1] = gcd(l[i], a[i]);
  for (int i = n - 1; i >= 1; i--)
    r[i - 1] = gcd(r[i], a[i]);

  int ans = 1;
  for (int i = 0; i < n; i++) {
    ans = max(ans, gcd(l[i], r[i]));
  }
  cout << ans << endl;

  return 0;
}

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

int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n), l(n), r(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++)
    l[i + 1] = gcd(l[i], a[i]);
  for (int i = n - 1; i >= 1; i--)
    r[i - 1] = gcd(r[i], a[i]);

  int ans = 1;
  for (int i = 0; i < n; i++) {
    ans = max(ans, gcd(l[i], r[i]));
  }
  cout << ans << endl;

  return 0;
}