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

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

int gcd(int x, int y) {
  if (x == 0)
    return y;
  if (y == 0)
    return x;
  while (x != y) {
    if (x > y)
      x -= y;
    else
      y -= x;
  }
  return x;
}

int main() {
  int N;
  cin >> N;
  // vector<vector<char>> d(H, vector<char>(W));
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A.at(i);
  vector<int> L(N + 1);
  L.at(0) = 0;
  vector<int> R(N + 2);
  R.at(N + 1) = 0;
  for (int i = 1; i <= N; i++) {
    L.at(i) = gcd(L.at(i - 1), A.at(i));
    // cout << i << ":" << L.at(i) << endl;////
  }
  for (int i = N; i > 0; i--) {
    R.at(i) = gcd(R.at(i + 1), A.at(i));
    // cout << i << ":" << R.at(i) << endl;////
  }
  int c = 0;
  for (int i = 1; i <= N; i++) {
    c = max(c, gcd(L.at(i - 1), R.at(i + 1)));
    // cout << i << ":" << c << endl;////////
  }
  cout << c << endl;
  return 0;
}


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

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

int main() {
  int N;
  cin >> N;
  // vector<vector<char>> d(H, vector<char>(W));
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A.at(i);
  vector<int> L(N + 1);
  L.at(0) = 0;
  vector<int> R(N + 2);
  R.at(N + 1) = 0;
  for (int i = 1; i <= N; i++) {
    L.at(i) = gcd(L.at(i - 1), A.at(i));
    // cout << i << ":" << L.at(i) << endl;////
  }
  for (int i = N; i > 0; i--) {
    R.at(i) = gcd(R.at(i + 1), A.at(i));
    // cout << i << ":" << R.at(i) << endl;////
  }
  int c = 0;
  for (int i = 1; i <= N; i++) {
    c = max(c, gcd(L.at(i - 1), R.at(i + 1)));
    // cout << i << ":" << c << endl;////////
  }
  cout << c << endl;
  return 0;
}
Problem p03061 (C++)

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

int gcd(int n, int m) {
  if (m == 0)
    return n;
  else
    return gcd(m, n % m);
}

int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int x[n], y[n];
  x[0] = a[0], y[n - 1] = a[n - 1];
  for (int i = 1; i < n; i++) {
    x[i] = gcd(x[i - 1], a[i]);
  }
  for (int i = n - 2; i >= 0; i++) {
    y[i] = gcd(y[i + 1], a[i]);
  }
  int ans = 0;
  ans = max({ans, x[n - 2], y[1]});
  for (int i = 1; i < n - 1; i++) {
    ans = max(ans, gcd(x[i - 1], y[i + 1]));
  }
  cout << ans << endl;
}


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

int gcd(int n, int m) {
  if (m == 0)
    return n;
  else
    return gcd(m, n % m);
}

int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int x[n], y[n];
  x[0] = a[0], y[n - 1] = a[n - 1];
  for (int i = 1; i < n; i++) {
    x[i] = gcd(x[i - 1], a[i]);
  }
  for (int i = n - 2; i >= 0; i--) {
    y[i] = gcd(y[i + 1], a[i]);
  }
  int ans = 0;
  ans = max({ans, x[n - 2], y[1]});
  for (int i = 1; i < n - 1; i++) {
    ans = max(ans, gcd(x[i - 1], y[i + 1]));
  }
  cout << ans << endl;
}
Problem p03061 (C++)

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

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, GCD = 0;
  cin >> N;
  vector<int> H(N), CD(N + 1), CDR(N + 1);
  for (int i = 0; i < N; i++)
    cin >> H[i];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= i; j++) {
      if (CD[j + 1] == 0)
        CD[j + 1] = gcd(CD[j], H[j]);
    }
    for (int j = N; j > i; j--) {
      if (CDR[j - 1] == 0)
        CDR[j - 1] = gcd(CDR[j], H[j - 1]);
    }
  }
  for (int i = 0; i < N; i++) {
    GCD = max(gcd(CD[i], CDR[i + 1]), GCD);
  }
  cout << GCD << endl;
  return 0;
}


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

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, GCD = 0;
  cin >> N;
  vector<int> H(N), CD(N + 1), CDR(N + 1);
  for (int i = 0; i < N; i++)
    cin >> H[i];
  for (int j = 0; j < N; j++) {
    CD[j + 1] = gcd(CD[j], H[j]);
  }
  for (int j = N; j > 0; j--) {
    CDR[j - 1] = gcd(CDR[j], H[j - 1]);
  }
  for (int i = 0; i < N; i++) {
    GCD = max(gcd(CD[i], CDR[i + 1]), GCD);
  }
  cout << GCD << endl;
  return 0;
}
Problem p03061 (C++)

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

int main() {
  int f, i, j, N, a[100001], m;
  vector<int> v;
  set<int> se;
  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }

  m = max(a[0], a[1]);
  auto p = se.insert(1);
  for (i = 1; i * i <= m; i++) {
    if (a[0] % i == 0) {
      p = se.insert(i);
      if (p.second)
        v.push_back(i);
      p = se.insert(a[0] / i);
      if (p.second)
        v.push_back(a[0] / i);
    }
    if (a[1] % i == 0) {
      p = se.insert(i);
      if (p.second)
        v.push_back(i);
      p = se.insert(a[1] / i);
      if (p.second)
        v.push_back(a[1] / i);
    }
  }
  sort(v.begin(), v.end(), greater<int>());

  i = 0;
  while (1) {
    f = 0;
    for (j = 0; j < N; j++) {
      if (a[j] % v[i] != 0) {
        if (f == 0) {
          f = 1;
        } else {
          f = 2;
          break;
        }
      }
    }
    if (f < 2) {
      printf("%d\n", v[i]);
      return 0;
    }
    i++;
  }
  return 0;
}

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

int main() {
  int f, i, j, N, a[100001], m;
  vector<int> v;
  set<int> se;
  scanf("%d", &N);
  for (i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }

  m = max(a[0], a[1]);
  auto p = se.insert(1);
  v.push_back(1);
  for (i = 1; i * i <= m; i++) {
    if (a[0] % i == 0) {
      p = se.insert(i);
      if (p.second)
        v.push_back(i);
      p = se.insert(a[0] / i);
      if (p.second)
        v.push_back(a[0] / i);
    }
    if (a[1] % i == 0) {
      p = se.insert(i);
      if (p.second)
        v.push_back(i);
      p = se.insert(a[1] / i);
      if (p.second)
        v.push_back(a[1] / i);
    }
  }
  sort(v.begin(), v.end(), greater<int>());

  i = 0;
  while (1) {
    f = 0;
    for (j = 0; j < N; j++) {
      if (a[j] % v[i] != 0) {
        if (f == 0) {
          f = 1;
        } else {
          f = 2;
          break;
        }
      }
    }
    if (f < 2) {
      printf("%d\n", v[i]);
      return 0;
    }
    i++;
  }
  return 0;
}
Problem p03061 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

n = int(input())
a = list(map(int, input().split()))

forward = []
backward = [0 for x in range(n)]

if n == 2:
    print(max(a[0], a[1]))
    exit(0)

for i in range(n):
    if i == 0:
        forward.append(a[i])
    else:
        forward.append(gcd(forward[i - 1], a[i]))


for i in range(n - 1, -1, -1):
    if i == n - 1:
        backward[i] = a[i]
    else:
        backward[i] = gcd(backward[i + 1], a[i])


ans = 1

for i in range(n):
    if i == 0:
        ans = max(ans, backward[1])
    elif i == n - 1:
        ans = max(ans, forward[n - 2])
    else:
        ans = max(ans, gcd(forward[i - 1], backward[i + 1]))

print(ans)


Fixed submission:
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))

forward = []
backward = [0 for x in range(n)]

if n == 2:
    print(max(a[0], a[1]))
    exit(0)

for i in range(n):
    if i == 0:
        forward.append(a[i])
    else:
        forward.append(gcd(forward[i - 1], a[i]))


for i in range(n - 1, -1, -1):
    if i == n - 1:
        backward[i] = a[i]
    else:
        backward[i] = gcd(backward[i + 1], a[i])


ans = 1

for i in range(n):
    if i == 0:
        ans = max(ans, backward[1])
    elif i == n - 1:
        ans = max(ans, forward[n - 2])
    else:
        ans = max(ans, gcd(forward[i - 1], backward[i + 1]))

print(ans)
Problem p03061 (Python)

Buggy submission (status: Runtime Error):
from math import gcd

n = int(input())
a = list(map(int, input().split()))
fromLeft = [0] * n
fromLeft[0] = a[0]
fromRight = [0] * n
fromRight[0] = a[n - 1]
for i in range(1, n):
    fromLeft[i] = gcd(fromLeft[i - 1], a[i])
    fromRight[i] = gcd(fromRight[i - 1], a[n - i - 1])
ret = fromLeft[n - 1]
for i in range(n):
    if i == 0:
        curGcd = fromRight[n - 2]
    elif i == n - 1:
        curGcd = fromLeft[n - 2]
    else:
        curGcd = gcd(fromLeft[i - 1], fromRight[n - i - 2])
    ret = max(ret, curGcd)
print(ret)


Fixed submission:
def gcd(x, y):
    while y:
        x, y = y, x % y
    return x


n = int(input())
a = list(map(int, input().split()))
fromLeft = [0] * n
fromLeft[0] = a[0]
fromRight = [0] * n
fromRight[0] = a[n - 1]
for i in range(1, n):
    fromLeft[i] = gcd(fromLeft[i - 1], a[i])
    fromRight[i] = gcd(fromRight[i - 1], a[n - i - 1])
ret = fromLeft[n - 1]
for i in range(n):
    if i == 0:
        curGcd = fromRight[n - 2]
    elif i == n - 1:
        curGcd = fromLeft[n - 2]
    else:
        curGcd = gcd(fromLeft[i - 1], fromRight[n - i - 2])
    ret = max(ret, curGcd)
print(ret)
Problem p03061 (Python)

Buggy submission (status: Time Limit Exceeded):
from functools import reduce
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
A = list(set(a))
ans = [reduce(gcd, A)]

if len(A) == 1:
    print(A[0])
    exit()
for i in a:
    b = sorted(A)
    b.remove(i)
    if a.count(i) == 1:
        ans.append(reduce(gcd, b))

print(max(ans))


Fixed submission:
from functools import reduce
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
A = list(set(a))
ans = [reduce(gcd, A)]

if len(A) == 1:
    print(A[0])
    exit()
for i in A:
    b = sorted(A)
    b.remove(i)
    if a.count(i) == 1:
        ans.append(reduce(gcd, b))

print(max(ans))
Problem p03061 (Python)

Buggy submission (status: Runtime Error):
from math import gcd


def main():
    N = int(input())
    nums = list(map(int, input().split()))

    L = [0 for _ in range(N + 1)]
    R = [0 for _ in range(N + 1)]

    for i, num in enumerate(nums):
        L[i + 1] = gcd(L[i], num)

    for i in range(N - 2, -1, -1):
        R[i] = gcd(R[i + 1], nums[i + 1])

    print(max(gcd(L[i], R[i]) for i in range(N)))


if __name__ == "__main__":
    main()


Fixed submission:
#!/snap/bin/pypy3
def gcd(a, b):
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)


def main():
    N = int(input())
    nums = list(map(int, input().split()))

    L = [0 for _ in range(N + 1)]
    R = [0 for _ in range(N + 1)]

    for i, num in enumerate(nums):
        L[i + 1] = gcd(L[i], num)

    for i in range(N - 2, -1, -1):
        R[i] = gcd(R[i + 1], nums[i + 1])

    print(max(gcd(L[i], R[i]) for i in range(N)))


if __name__ == "__main__":
    main()
Problem p03061 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = [int(i) for i in input().split()]

if len(A) > 1:
    result = sorted(A)[1]
else:
    result = min(A)

while True:
    counter = 0
    should_continue = False
    for e in A:
        if e % result != 0:
            counter += 1
            if counter > 1:
                should_continue = True
                break
    if should_continue:
        continue
    if counter <= 1:  # 1つは書き換えられる
        break
    else:
        result -= 1

print(result)


Fixed submission:
N = int(input())
A = [int(i) for i in input().split()]

if len(A) > 1:
    result = sorted(A)[1]
else:
    result = min(A)

while True:
    counter = 0
    should_continue = False
    for e in A:
        if e % result != 0:
            counter += 1
            if counter > 1:
                should_continue = True
                break
    if should_continue:
        result -= 1
        continue
    if counter <= 1:  # 1つは書き換えられる
        break
    else:
        result -= 1

print(result)
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++)
typedef long long int lli;
typedef pair<int, int> P;

int A[10000];
int R[10000];
int L[10000];
int n;

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

  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

int calc_l(int max_i) {
  if (max_i < 0)
    return 0;
  if (L[max_i] != 0)
    return L[max_i];
  if (max_i == 0)
    return L[0] = A[0];
  return L[max_i] = gcd(calc_l(max_i - 1), A[max_i]);
}

int calc_r(int min_i) {
  if (min_i > n - 1)
    return 0;
  if (R[min_i] != 0)
    return R[min_i];
  if (min_i == n - 1)
    return R[n - 1] = A[n - 1];
  return R[min_i] = gcd(calc_r(min_i + 1), A[min_i]);
}

int exclude_gcd(int ex_i) { return gcd(calc_l(ex_i - 1), calc_r(ex_i + 1)); }

int main() {
  cin >> n;
  REP(i, n) { cin >> A[i]; }
  int max_gcd = 0;

  // i を除いたときのgcd
  REP(i, n) {
    int cur_gcd = exclude_gcd(i);
    max_gcd = max(max_gcd, cur_gcd);
  }
  cout << max_gcd << endl;
}


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

#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long int lli;
typedef pair<int, int> P;

int A[100000];
int R[100000];
int L[100000];
int n;

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

  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

int calc_l(int max_i) {
  if (max_i < 0)
    return 0;
  if (L[max_i] != 0)
    return L[max_i];
  if (max_i == 0)
    return L[0] = A[0];
  return L[max_i] = gcd(calc_l(max_i - 1), A[max_i]);
}

int calc_r(int min_i) {
  if (min_i > n - 1)
    return 0;
  if (R[min_i] != 0)
    return R[min_i];
  if (min_i == n - 1)
    return R[n - 1] = A[n - 1];
  return R[min_i] = gcd(calc_r(min_i + 1), A[min_i]);
}

int exclude_gcd(int ex_i) { return gcd(calc_l(ex_i - 1), calc_r(ex_i + 1)); }

int main() {
  cin >> n;
  REP(i, n) { cin >> A[i]; }
  int max_gcd = 0;

  // i を除いたときのgcd
  REP(i, n) {
    int cur_gcd = exclude_gcd(i);
    max_gcd = max(max_gcd, cur_gcd);
  }
  cout << max_gcd << endl;
}
Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

int gcd(int m, int n) {
  while (m != n)
    m > n ? (m = m - n) : (n = n - m);
  return m;
}

int main() {
  int n;
  int a[100001], l[100001], r[100001];
  cin >> n;
  REP(i, n) cin >> a[i];
  l[0] = a[0];
  REP(i, n - 1) l[i + 1] = gcd(l[i], a[i + 1]);
  r[n - 1] = a[n - 1];
  for (int i = n - 1; i > 0; i--)
    r[i - 1] = gcd(r[i], a[i - 1]);
  int ans = max(l[n - 2], r[1]);
  FOR(i, 1, n - 1) ans = max(ans, gcd(l[i - 1], r[i + 1]));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

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

int main() {
  int n;
  int a[100001], l[100001], r[100001];
  cin >> n;
  REP(i, n) cin >> a[i];
  l[0] = a[0];
  REP(i, n - 1) l[i + 1] = gcd(l[i], a[i + 1]);
  r[n - 1] = a[n - 1];
  for (int i = n - 1; i > 0; i--)
    r[i - 1] = gcd(r[i], a[i - 1]);
  int ans = max(l[n - 2], r[1]);
  FOR(i, 1, n - 1) ans = max(ans, gcd(l[i - 1], r[i + 1]));
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

using namespace std;

#define PI 3.14159265
#define OO 1e9
#define SS second
#define FF first
#define Trace(n) cout << #n << " = " << n << endl;
#define ll long long
// #define endl		"\n"
#define all(v) ((v).begin()), ((v).end())
#define boom main
#define cn(n) scanf("%d", &n)
#define cnl(n) scanf("%lld", &n)
#define F(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define Fill(ar, val) memset(ar, val, sizeof(ar))
#define bit(n) (1 << (n))

int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[] = {-1, 1, 0, 0, 1, -1, 1, -1};

void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);

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

int main() {
  fast();
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  int N1 = v[0];
  int N2 = v[1];

  set<int> divs;
  map<int, int> mp;

  for (int i = 1; i * i <= N1; i++) {
    if (N1 % i == 0) {
      divs.insert(i);
      divs.insert(N1 / i);
    }
  }

  for (int i = 1; i * i <= N2; i++) {
    if (N2 % i == 0) {
      divs.insert(i);
      divs.insert(N2 / i);
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto Num : divs) {
      if (v[i] % Num == 0) {
        mp[Num]++;
      }
    }
  }

  int ans = 1;
  for (auto it : mp) {
    if (it.SS >= (n - 1))
      ans = max(ans, it.FF);
  }

  cout << ans;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS

#include <bits/stdc++.h>

using namespace std;

#define PI 3.14159265
#define OO 1e9
#define SS second
#define FF first
#define Trace(n) cout << #n << " = " << n << endl;
#define ll long long
// #define endl		"\n"
#define all(v) ((v).begin()), ((v).end())
#define boom main
#define cn(n) scanf("%d", &n)
#define cnl(n) scanf("%lld", &n)
#define F(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define Fill(ar, val) memset(ar, val, sizeof(ar))
#define bit(n) (1 << (n))

int dx[] = {0, 0, -1, 1, 1, 1, -1, -1};
int dy[] = {-1, 1, 0, 0, 1, -1, 1, -1};

void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}

int main() {
  fast();
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];

  int N1 = v[0];
  int N2 = v[1];

  set<int> divs;
  map<int, int> mp;

  for (int i = 1; i * i <= N1; i++) {
    if (N1 % i == 0) {
      divs.insert(i);
      divs.insert(N1 / i);
    }
  }

  for (int i = 1; i * i <= N2; i++) {
    if (N2 % i == 0) {
      divs.insert(i);
      divs.insert(N2 / i);
    }
  }

  for (int i = 0; i < n; i++) {
    for (auto Num : divs) {
      if (v[i] % Num == 0) {
        mp[Num]++;
      }
    }
  }

  int ans = 1;
  for (auto it : mp) {
    if (it.SS >= (n - 1))
      ans = max(ans, it.FF);
  }

  cout << ans;
}
Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  if (x > y)
    swap(x, y);
  if (y % x == 0)
    return x;
  else
    return gcd(y % x, x);
}
int main(void) {
  int n;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long g[n]; // i番目を除いたn-1この整数の最大公約数
  for (int i = 0; i < n; i++) {
    long long G = a[0];
    if (i == 0)
      G = a[1];
    for (int j = 0; j < n; j++) {
      if (i == j)
        continue;
      G = gcd(G, a[j]);
    }
    g[i] = G;
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    res = max(res, g[i]);
  }
  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) {
  if (x > y)
    swap(x, y);
  if (y % x == 0)
    return x;
  else
    return gcd(y % x, x);
}
int main(void) {
  int n;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long g[n]; // i番目を除いたn-1この整数の最大公約数
  long long r[n], l[n];
  /*
  r[i]=gcd(a[0),a[1],…,a[i]);
  l[i]=gcd(a[n-1],a[n-2],a[n-3],…,a[i]);
  g[n]=gcd(r[i],l[i]);
  */
  r[0] = a[0];
  l[n - 1] = a[n - 1];
  for (int i = 1; i < n; i++) {
    r[i] = gcd(r[i - 1], a[i]);
    l[n - i - 1] = gcd(l[n - i], a[n - 1 - i]);
  }
  g[0] = l[1];
  g[n - 1] = r[n - 2];
  for (int i = 1; i < n - 1; i++) {
    g[i] = gcd(r[i - 1], l[i + 1]);
  }
  long long res = 0;
  for (int i = 0; i < n; i++) {
    res = max(res, g[i]);
  }
  cout << res << endl;
}
Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int GCD(int a, int b) {
  int c = min(a, b), d = max(a, b);
  if (d % c == 0)
    return c;
  return GCD(c, d % c);
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  if (a[0] == a[n - 1]) {
    cout << a[0] << endl;
    return 0;
  }
  if (a.size() == 3) {
    cout << max({GCD(a[0], a[1]), GCD(a[1], a[2]), GCD(a[2], a[0])}) << endl;
    return 0;
  }
  int bef = a[0], aft = a[n - 1];
  int s = 0, g = n;
  while (s != g) {
    for (int i = s; i < (g - s) / 2; i++)
      bef = GCD(bef, a[i]);
    for (int i = (g - s) / 2; i < (g - s); i++)
      aft = GCD(aft, a[i]);
    if (bef == aft)
      s = g;
    if (bef > aft) {
      aft = bef;
      s = s + (g - s) / 2;
    }
    if (bef < aft) {
      bef = aft;
      g = s + (g - s) / 2;
    }
  }
  cout << bef << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int GCD(int a, int b) {
  int c = min(a, b), d = max(a, b);
  if (d % c == 0)
    return c;
  return GCD(c, d % c);
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  if (a[0] == a[n - 1]) {
    cout << a[0] << endl;
    return 0;
  }
  if (a.size() == 3) {
    cout << max({GCD(a[0], a[1]), GCD(a[1], a[2]), GCD(a[2], a[0])}) << endl;
    return 0;
  }
  int bef = a[0], aft = a[n - 1];
  int s = 0, g = n;
  while (s + 1 < g) {
    for (int i = s; i < (g - s) / 2; i++)
      bef = GCD(bef, a[i]);
    for (int i = (g - s) / 2; i < (g - s); i++)
      aft = GCD(aft, a[i]);
    if (bef == aft)
      s = g;
    if (bef > aft) {
      aft = bef;
      s = s + (g - s) / 2;
    }
    if (bef < aft) {
      bef = aft;
      g = s + (g - s) / 2;
    }
  }
  cout << bef << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define inf 1e16
#define M 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define mx 1000010

int main() {
  IOS;
  ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n) cin >> a[i];
  ll od = 0, ev = 0, k = 1, mx1 = k, t = 1;
  rep(i, 0, n) k = __gcd(a[i], k);
  rep(i, 0, n) {
    if (i == 0)
      t = a[1];
    else
      t = a[0];
    rep(j, 0, n) {
      if (j != i) {
        t = __gcd(a[j], t);
        if (t == k)
          break;
      }
    }
    mx1 = max(mx1, t);
  }
  cout << mx1;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define inf 1e16
#define M 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define mx 1000010

int main() {
  IOS;
  ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n) cin >> a[i];
  ll od = 0, ev = 0, k = 1, mx1 = k, t = 1;
  rep(i, 0, n) k = __gcd(a[i], k);
  rep(i, 0, n) {
    if (i == 0)
      t = a[1];
    else
      t = a[0];
    rep(j, 0, min(n, 50ll)) {
      if (j != i) {
        t = __gcd(a[j], t);
        if (t == k)
          break;
      }
    }
    mx1 = max(mx1, t);
  }
  cout << mx1;
  return 0;
}
Problem p03061 (C++)

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

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<ll> left(n, 0);
  vector<ll> right(n, 0);

  for (int i = 0; i < n; i++)
    left[i + 1] = gcd(left[i], a[i]);
  for (int i = n - 1; i >= 0; i--)
    right[i] = gcd(right[i + 1], a[i]);

  int res = 0;
  for (int i = 0; i < n; i++) {
    int l = left[i];
    int r = right[i + 1];

    if (res < gcd(l, r)) {
      res = gcd(l, r);
    }
  }
  cout << res << endl;

  return 0;
}

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

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  vector<ll> left(n + 1, 0);
  vector<ll> right(n + 1, 0);

  for (int i = 0; i < n; i++)
    left[i + 1] = gcd(left[i], a[i]);
  for (int i = n - 1; i >= 0; i--)
    right[i] = gcd(right[i + 1], a[i]);

  int res = 0;
  for (int i = 0; i < n; i++) {
    int l = left[i];
    int r = right[i + 1];

    if (res < gcd(l, r)) {
      res = gcd(l, r);
    }
  }
  cout << res << endl;

  return 0;
}
Problem p03061 (C++)

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

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

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

map<pair<int, int>, int> g_range_gcd;

int range_gcd(const vector<int> &A, int l, int r) {
  if (r - l == 1)
    return A[l];
  if (g_range_gcd.count(make_pair(l, r)) != 0) {
    return g_range_gcd[make_pair(l, r)];
  }
  int m = (l + r) / 2;
  return gcd(range_gcd(A, l, m), range_gcd(A, m, r));
}

int range_gcd2(const vector<int> &A, int l, int r, int x) {
  if (r - l == 1) {
    return l == x ? -1 : A[l];
  }
  if (x < l || r <= x) {
    return range_gcd(A, l, r);
  }
  int m = (l + r) / 2;
  int a = range_gcd2(A, l, m, x);
  int b = range_gcd2(A, m, r, x);
  if (a == -1)
    return b;
  if (b == -1)
    return a;
  return gcd(a, b);
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  int res = 1;
  for (int i = 0; i < N; i++) {
    res = max(res, range_gcd2(A, 0, N, i));
  }
  cout << res << endl;
}

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

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

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

map<pair<int, int>, int> g_range_gcd;

int range_gcd(const vector<int> &A, int l, int r) {
  if (r - l == 1)
    return A[l];
  if (g_range_gcd.count(make_pair(l, r)) != 0) {
    return g_range_gcd[make_pair(l, r)];
  }
  int m = (l + r) / 2;
  return g_range_gcd[make_pair(l, r)] =
             gcd(range_gcd(A, l, m), range_gcd(A, m, r));
}

int range_gcd2(const vector<int> &A, int l, int r, int x) {
  if (r - l == 1) {
    return l == x ? -1 : A[l];
  }
  if (x < l || r <= x) {
    return range_gcd(A, l, r);
  }
  int m = (l + r) / 2;
  int a = range_gcd2(A, l, m, x);
  int b = range_gcd2(A, m, r, x);
  if (a == -1)
    return b;
  if (b == -1)
    return a;
  return gcd(a, b);
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  int res = 1;
  for (int i = 0; i < N; i++) {
    res = max(res, range_gcd2(A, 0, N, i));
  }
  cout << res << endl;
}
Problem p03061 (C++)

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

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> l(n), r(n);
  l[0] = a[0];
  r[0] = a[n - 1];
  rep(i, n) {
    l[i + 1] = __gcd(l[i], a[i + 1]);
    r[i + 1] = __gcd(r[i], a[(n - 1) - (i + 1)]);
  }
  vector<ll> ans(n);
  ans[0] = r[n - 2];
  ans[n - 1] = l[n - 2];
  for (int i = 1; i < n - 1; ++i) {
    ans[i] = __gcd(l[i - 1], r[n - 3 - (i - 1)]);
  }
  ll ret = 0;
  rep(i, n) { ret = max(ret, ans[i]); }
  cout << ret << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define size_of_array(array) (sizeof(array) / sizeof(array[0]))
using ll = long long;
using namespace std;
using Graph = vector<vector<int>>;
using Field = vector<vector<int>>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> l(n + 1), r(n + 1);
  l[0] = a[0];
  r[0] = a[n - 1];
  rep(i, n) {
    l[i + 1] = __gcd(l[i], a[i + 1]);
    r[i + 1] = __gcd(r[i], a[(n - 1) - (i + 1)]);
  }
  vector<ll> ans(n);
  ans[0] = r[n - 2];
  ans[n - 1] = l[n - 2];
  for (int i = 1; i < n - 1; ++i) {
    ans[i] = __gcd(l[i - 1], r[n - 3 - (i - 1)]);
  }
  ll ret = 0;
  rep(i, n) { ret = max(ret, ans[i]); }
  cout << ret << endl;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000000000000LL
#define MOD 1000000007
#define writeln(n) cout << n << endl;
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
// vector<tuple<long long, long long, long long>>vec;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

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

int main() {
  int n;
  cin >> n;
  vector<llong> a(n, 0);
  REP(i, n) cin >> a[i];
  vector<llong> front(n, 0);
  front[0] = a[0];
  vector<llong> back(n, 0);
  back[n - 1] = a[n - 1];
  for (int i = 0; i < n; i++) {
    front[i + 1] = gcd(front[i], a[i + 1]);
  }
  for (int i = n - 2; i >= 0; i--) {
    back[i] = gcd(a[i], back[i + 1]);
  }
  llong ans = 0;
  for (int i = 1; i < n - 1; i++) {
    llong m = gcd(front[i - 1], back[i + 1]);
    ans = max(ans, m);
  }
  llong fro = back[1];
  llong bac = front[n - 2];
  cout << max({ans, fro, bac}) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 1000000000000000000LL
#define MOD 1000000007
#define writeln(n) cout << n << endl;
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
// vector<tuple<long long, long long, long long>>vec;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

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

int main() {
  int n;
  cin >> n;
  vector<llong> a(n, 0);
  REP(i, n) cin >> a[i];
  vector<llong> front(n, 0);
  front[0] = a[0];
  vector<llong> back(n, 0);
  back[n - 1] = a[n - 1];
  for (int i = 0; i < n - 1; i++) {
    front[i + 1] = gcd(front[i], a[i + 1]);
  }
  for (int i = n - 2; i >= 0; i--) {
    back[i] = gcd(a[i], back[i + 1]);
  }
  llong ans = 0;
  for (int i = 1; i < n - 1; i++) {
    llong m = gcd(front[i - 1], back[i + 1]);
    ans = max(ans, m);
  }
  llong fro = back[1];
  llong bac = front[n - 2];
  cout << max({ans, fro, bac}) << endl;
}
Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) {
  while (x && y) {
    x %= y;
    if (x == 0)
      break;
    y %= x;
  }
  return x + y;
}
int seg(int l, int r, int n, int nl, int nr, vector<int> t) {
  if (l >= nr || r <= nl)
    return 0; // 範囲がノードから外れている
  else if (l <= nl && r >= nr)
    return t[n]; // 範囲がノードを包含している
  else {
    int mid = (nl + nr) / 2;
    return gcd(seg(l, r, n * 2, nl, mid, t), seg(l, r, n * 2 + 1, mid, nr, t));
  }
}
int main() {
  int n;
  cin >> n;
  int N;
  for (int i = 1; i < n * 2; i *= 2)
    N = i;
  int a[n];
  vector<int> t(N * 2, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    t[i + N] = a[i];
  }
  for (int i = N - 1; i >= 1; i--) {
    t[i] = gcd(t[i * 2], t[i * 2 + 1]);
  }
  int m = 0;
  for (int i = 0; i < n; i++) {
    m = max(m, gcd(seg(0, i, 1, 0, N, t), seg(i + 1, n, 1, 0, N, t)));
  }
  cout << m << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) {
  while (x && y) {
    x %= y;
    if (x == 0)
      break;
    y %= x;
  }
  return x + y;
}
int seg(int l, int r, int n, int nl, int nr, vector<int> &t) {
  if (l >= nr || r <= nl)
    return 0; // 範囲がノードから外れている
  else if (l <= nl && r >= nr)
    return t[n]; // 範囲がノードを包含している
  else {
    int mid = (nl + nr) / 2;
    return gcd(seg(l, r, n * 2, nl, mid, t), seg(l, r, n * 2 + 1, mid, nr, t));
  }
}
int main() {
  int n;
  cin >> n;
  int N;
  for (int i = 1; i < n * 2; i *= 2)
    N = i;
  int a[n];
  vector<int> t(N * 2, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    t[i + N] = a[i];
  }
  for (int i = N - 1; i >= 1; i--) {
    t[i] = gcd(t[i * 2], t[i * 2 + 1]);
  }
  int m = 0;
  for (int i = 0; i < n; i++) {
    m = max(m, gcd(seg(0, i, 1, 0, N, t), seg(i + 1, n, 1, 0, N, t)));
  }
  cout << m << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
//
// Created by kuroneko on 2019-07-06.
//

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

ll GCD(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return GCD(b, a % b);
}

int LCM(int a, int b) {
  int g = GCD(a, b);
  return a / g * b;
}

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  int L[100000], R[100000];
  for (int i = 0; i < N; i++)
    cin >> A[i];

  L[0] = 0;
  R[N] = 0;
  L[1] = 0;
  R[N - 1] = 0;
  for (int i = 1; i < N; i++) {
    L[i + 1] = GCD(L[i], A[i - 1]);
    R[N - i] = GCD(R[N - i + 1], A[N - i]);
  }

  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, GCD(L[i], R[i]));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
//
// Created by kuroneko on 2019-07-06.
//

#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

typedef long long ll;
using namespace std;

ll GCD(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (b == 0)
    return a;
  return GCD(b, a % b);
}

int LCM(int a, int b) {
  int g = GCD(a, b);
  return a / g * b;
}

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  int L[100000 + 7], R[100000 + 7];
  for (int i = 0; i < N; i++)
    cin >> A[i];

  L[0] = 0;
  R[N] = 0;
  L[1] = 0;
  R[N - 1] = 0;
  for (int i = 1; i < N; i++) {
    L[i + 1] = GCD(L[i], A[i - 1]);
    R[N - i] = GCD(R[N - i + 1], A[N - i]);
  }

  ll ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, GCD(L[i], R[i]));
  }
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  ll count = 0, ans;
  for (int i = a[1]; i > 0; i--) {
    for (int j = 0; j < n; j++) {
      if (a[j] % i == 0)
        count++;
    }
    if (count > n - 2) {
      ans = i;
      break;
    } else
      count = 0;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  ll count = 0, ans;
  for (int i = a[1]; i > 0; i--) {
    if (a[1] % i == 0 || a[0] % i == 0) {
      for (int j = 0; j < n; j++) {
        if (a[j] % i == 0)
          count++;
      }
    }
    if (count > n - 2) {
      ans = i;
      break;
    } else
      count = 0;
  }
  cout << ans << endl;
}
Problem p03061 (C++)

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

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

ll A[10101];
ll L[10101];
ll R[10101];

int main() {

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  L[1] = A[1];
  for (int i = 2; i <= n; i++)
    L[i] = gcd(L[i - 1], A[i]);
  R[n] = A[n];
  for (int i = n - 1; i >= 1; i--)
    R[i] = gcd(R[i + 1], A[i]);

  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i == 1)
      ans = max(ans, R[2]);
    else if (i == n)
      ans = max(ans, L[n - 1]);
    else {
      ans = max(ans, gcd(L[i - 1], R[i + 1]));
    }
  }
  cout << ans << endl;

  return 0;
}

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

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

ll A[101010];
ll L[101010];
ll R[101010];

int main() {

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  L[1] = A[1];
  for (int i = 2; i <= n; i++)
    L[i] = gcd(L[i - 1], A[i]);
  R[n] = A[n];
  for (int i = n - 1; i >= 1; i--)
    R[i] = gcd(R[i + 1], A[i]);

  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (i == 1)
      ans = max(ans, R[2]);
    else if (i == n)
      ans = max(ans, L[n - 1]);
    else {
      ans = max(ans, gcd(L[i - 1], R[i + 1]));
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03061 (C++)

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

int gcd(int a, int b) {
  while (a && b) {
    (a < b) ? b %= a : a %= b;
  }
  return a + b;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &a : A) {
    cin >> a;
  }

  int gcd_max = -1;
  for (int ii = 0; ii < N; ii++) {
    int gcd_sub = (ii == 0) ? A[1] : A[0];
    for (int jj = 0; jj < N; jj++) {
      if (jj == ii)
        continue;
      gcd_sub = gcd(gcd_sub, A[jj]);
    }
    gcd_max = max(gcd_max, gcd_sub);
  }
  cout << gcd_max << endl;
  return 0;
}


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

int gcd(int a, int b) {
  while (a && b) {
    (a < b) ? b %= a : a %= b;
  }
  return a + b;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (auto &a : A) {
    cin >> a;
  }

  vector<int> gcd_front(N);
  vector<int> gcd_back(N);

  gcd_front[0] = A[0];
  for (int ii = 1; ii < N; ii++) {
    gcd_front[ii] = gcd(gcd_front[ii - 1], A[ii]);
  }

  gcd_back[N - 1] = A[N - 1];
  for (int ii = N - 2; ii >= 0; ii--) {
    gcd_back[ii] = gcd(gcd_back[ii + 1], A[ii]);
  }

  int gcd_max = max(gcd_back[1], gcd_front[N - 2]);
  for (int ii = 1; ii < N - 1; ii++) {
    gcd_max = max(gcd_max, gcd(gcd_front[ii - 1], gcd_back[ii + 1]));
  }
  cout << gcd_max << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int N;
vector<int> A;

int gcd(int a, int b) {
  if (b > a) {
    swap(a, b);
  }
  while (b != 0) {
    int na = b;
    int nb = a % b;
    a = na;
    b = nb;
  }
  return a;
}

int l[100005];
int r[100005];

int main() {
  cin >> N;
  A.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i + 1];
  }
  if (N == 2) {
    cout << max(A[1], A[2]) << endl;
    return 0;
  }
  l[0] = 0;
  r[N + 1] = 0;
  for (int i = 1; i <= N; i++) {
    l[i] = gcd(l[i - 1], A[i]);
    r[N + 1 - i] = gcd(r[N + 2 - i], A[N + 1 - i]);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, gcd(l[i - 1], r[i + 1]));
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int N;
vector<int> A;

int gcd(int a, int b) {
  if (b > a) {
    swap(a, b);
  }
  while (b != 0) {
    int na = b;
    int nb = a % b;
    a = na;
    b = nb;
  }
  return a;
}

int l[100005];
int r[100005];

int main() {
  cin >> N;
  A.resize(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> A[i + 1];
  }
  if (N == 2) {
    cout << max(A[1], A[2]) << endl;
    return 0;
  }
  l[0] = 0;
  r[N + 1] = 0;
  for (int i = 1; i <= N; i++) {
    l[i] = gcd(l[i - 1], A[i]);
    r[N + 1 - i] = gcd(r[N + 2 - i], A[N + 1 - i]);
  }
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    ans = max(ans, gcd(l[i - 1], r[i + 1]));
  }
  cout << ans << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> l(n);
  vector<int> r(n);
  for (int i = 0; i < n; i++) {
    l[i + 1] = gcd(l[i], a[i]);
  }
  for (int i = 0; i < n; i++) {
    r[i + 1] = gcd(r[i], a[n - i - 1]);
  }

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> l(n + 1);
  vector<int> r(n + 1);
  for (int i = 0; i < n; i++) {
    l[i + 1] = gcd(l[i], a[i]);
  }
  for (int i = 0; i < n; i++) {
    r[i + 1] = gcd(r[i], a[n - i - 1]);
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;

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);
  rep(i, n) { cin >> a[i]; }
  vector<int> l(n), r(n);

  rep(i, n) l[i + 1] = gcd(l[i], a[i]);
  for (int i = n - 2; i >= 0; i--)
    r[i] = gcd(r[i + 1], a[i + 1]);
  int ans = 0;
  rep(i, n) ans = max(ans, gcd(l[i], r[i]));
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;

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);
  rep(i, n) { cin >> a[i]; }
  vector<int> l(n), r(n);

  rep(i, n - 1) l[i + 1] = gcd(l[i], a[i]);
  for (int i = n - 2; i >= 0; i--)
    r[i] = gcd(r[i + 1], a[i + 1]);
  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 <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

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

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

  for (int i = 0; i <= n; i++) {
    if (MAX < gcd(l[i], r[i]))
      MAX = gcd(l[i], r[i]);
  }

  cout << MAX << endl;
  return 0;
}

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

int gcd(int a, int b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

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

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

  for (int i = 0; i <= n; i++) {
    if (MAX < gcd(l[i], r[i]))
      MAX = gcd(l[i], r[i]);
  }

  cout << MAX << endl;
  return 0;
}
Problem p03061 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll u = pow(n, 0.5);
  vector<ll> v;
  ll now = a[0];
  for (int i = 1; i < n; i++) {
    if (i % u == 0) {
      v.push_back(now);
      now = a[i];
    } else {
      now = gcd(now, a[i]);
    }
  }
  v.push_back(now);
  // cout<<u;return 0;
  // cout<<t[0]<<t[1]<<t[2];return 0;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll y = i / u;
    ll r;
    if (y == 0)
      r = v[1];
    else
      r = v[0];
    for (int j = 0; j < v.size(); j++) {
      if (j == y)
        continue;
      r = gcd(r, v[j]);
    }
    for (int j = y * u; j < y * u + u; j++) {
      if (j == i)
        continue;
      r = gcd(r, a[j]);
    }
    ans = max(r, ans);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
int main() {
  ll n;
  cin >> n;
  ll a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll u = pow(n, 0.5);
  vector<ll> v;
  ll now = a[0];
  for (int i = 1; i < n; i++) {
    if (i % u == 0) {
      v.push_back(now);
      now = a[i];
    } else {
      now = gcd(now, a[i]);
    }
  }
  v.push_back(now);
  // cout<<u;return 0;
  // cout<<t[0]<<t[1]<<t[2];return 0;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll y = i / u;
    ll r;
    if (y == 0)
      r = v[1];
    else
      r = v[0];
    for (int j = 0; j < v.size(); j++) {
      if (j == y)
        continue;
      r = gcd(r, v[j]);
    }
    for (int j = y * u; j < y * u + u; j++) {
      if (j == i || j >= n)
        continue;
      r = gcd(r, a[j]);
    }
    ans = max(r, ans);
  }
  cout << ans << endl;
}