Problem p03127 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>

using namespace std;

#define rep(i, x) for (__typeof(x) i = 0; i < x; i++)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define sci(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define scii(x, y)                                                             \
  int x, y;                                                                    \
  scanf("%d %d", &x, &y);
#define sciii(x, y, z)                                                         \
  int x, y, z;                                                                 \
  scanf("%d %d %d", &x, &y, &z);
#define TC(x)                                                                  \
  sci(x);                                                                      \
  while (x--)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define debug(x)                                                               \
  { cerr << #x << " = " << x << endl; }
#define repi(i, x) for (__typeof(x) i = x - 1; i >= 0; i--)
#define fore(itr, x)                                                           \
  for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr++)
#define forei(itr, x)                                                          \
  for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr--)
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vector<int>> vvi;
const int infi = numeric_limits<int>::max();
const double eps = 0;
const int ms = 0;

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

int main() {
  ios::sync_with_stdio(false); // endl->"\n"
  cin.tie(0);

  int N;
  cin >> N;

  vi A(N);
  rep(i, N) cin >> A[i];
  int ans = infi;
  rep(i, N) {
    for (int j = i + 1; j < N; j++) {
      ans = min(ans, gcd(A[i], A[j]));
    }
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, x) for (__typeof(x) i = 0; i < x; i++)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define sci(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define scii(x, y)                                                             \
  int x, y;                                                                    \
  scanf("%d %d", &x, &y);
#define sciii(x, y, z)                                                         \
  int x, y, z;                                                                 \
  scanf("%d %d %d", &x, &y, &z);
#define TC(x)                                                                  \
  sci(x);                                                                      \
  while (x--)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define debug(x)                                                               \
  { cerr << #x << " = " << x << endl; }
#define repi(i, x) for (__typeof(x) i = x - 1; i >= 0; i--)
#define fore(itr, x)                                                           \
  for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr++)
#define forei(itr, x)                                                          \
  for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr--)
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vector<int>> vvi;
const int infi = numeric_limits<int>::max();
const double eps = 0;
const int ms = 0;

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

int main() {
  ios::sync_with_stdio(false); // endl->"\n"
  cin.tie(0);

  int N;
  cin >> N;

  vi A(N);
  rep(i, N) cin >> A[i];
  int ans = gcd(A[0], A[1]);

  for (int i = 3; i < N; i++) {

    ans = gcd(A[i], ans);
  }
  cout << ans << "\n";
  return 0;
}