Problem p02793 (C++)

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

using namespace std;
using ll = long long;

const int mod = 1000000007;

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

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

class Eratosthenes {
public:
  vector<bool> prime;

  Eratosthenes(ll size) {
    prime.resize(size, 1);
    prime[0] = 0;
    prime[1] = 0;
    for (size_t i = 2; i * i <= size; ++i)
      if (prime[i]) {
        for (size_t j = 2; i * j <= size; ++j)
          prime[i * j] = false;
      }
  }
};

class Combination {
  vector<ll> fac;  // n!(mod M)
  vector<ll> ifac; // k!^{M-2} (mod M)
public:
  Combination(ll size) {
    fac.resize(size);
    ifac.resize(size);
    fac[0] = 1;
    ifac[0] = 1;
    for (ll i = 0; i < size; i++) {
      fac[i + 1] = fac[i] * (i + 1) % mod;
      ifac[i + 1] = ifac[i] * mpow(i + 1, mod - 2) % mod;
    }
  }

  ll mpow(ll x, ll n) {
    ll ans = 1;
    while (n != 0) {
      if (n & 1)
        ans = ans * x % mod;
      x = x * x % mod;
      n = n >> 1;
    }
    return ans;
  }

  ll comb(ll a, ll b) {
    if (a == 0 && b == 0)
      return 1;
    if (a < b || a < 0)
      return 0;
    ll tmp = ifac[a - b] * ifac[b] % mod;
    return tmp * fac[a] % mod;
  }
};

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

ll LCM(ll a, ll b) {
  ll gcd = GCD(a, b);
  return a / gcd * b;
}

string s;
ll N;

int main() {
  cin >> N;
  Eratosthenes e(1e6);
  map<int, int> m;
  vector<ll> A(N);
  REP(k, N) {
    cin >> A[k];
    ll a = A[k];
    for (int i = 2; i * i <= e.prime.size(); i++) {
      if (!e.prime[i])
        continue;
      int cnt = 0;
      while (a % i == 0)
        a /= i, cnt++;
      m[i] = max(m[i], cnt);
    }
    if (a > 1)
      m[a] = max(m[a], 1);
  }

  mint lcm = 1;
  for (auto k : m)
    REP(i, k.second) lcm *= k.first;

  mint sum = 0;
  REP(i, N) {
    mint x = lcm / A[i];
    sum += x;
  }
  cout << sum.x << endl;

  return 0;
}


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

using namespace std;
using ll = long long;

const int mod = 1000000007;

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

struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}

  mint operator-() const { return mint(-x); }

  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }

  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }

  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }

  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }

  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }

  mint &operator/=(const mint a) { return (*this) *= a.inv(); }

  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};

class Eratosthenes {
public:
  vector<bool> prime;

  Eratosthenes(ll size) {
    prime.resize(size, 1);
    prime[0] = 0;
    prime[1] = 0;
    for (size_t i = 2; i * i <= size; ++i)
      if (prime[i]) {
        for (size_t j = 2; i * j <= size; ++j)
          prime[i * j] = false;
      }
  }
};

class Combination {
  vector<ll> fac;  // n!(mod M)
  vector<ll> ifac; // k!^{M-2} (mod M)
public:
  Combination(ll size) {
    fac.resize(size);
    ifac.resize(size);
    fac[0] = 1;
    ifac[0] = 1;
    for (ll i = 0; i < size; i++) {
      fac[i + 1] = fac[i] * (i + 1) % mod;
      ifac[i + 1] = ifac[i] * mpow(i + 1, mod - 2) % mod;
    }
  }

  ll mpow(ll x, ll n) {
    ll ans = 1;
    while (n != 0) {
      if (n & 1)
        ans = ans * x % mod;
      x = x * x % mod;
      n = n >> 1;
    }
    return ans;
  }

  ll comb(ll a, ll b) {
    if (a == 0 && b == 0)
      return 1;
    if (a < b || a < 0)
      return 0;
    ll tmp = ifac[a - b] * ifac[b] % mod;
    return tmp * fac[a] % mod;
  }
};

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

ll LCM(ll a, ll b) {
  ll gcd = GCD(a, b);
  return a / gcd * b;
}

string s;
ll N;

int main() {
  cin >> N;
  Eratosthenes e(1e6 + 1e5);
  map<int, int> m;
  vector<ll> A(N);
  REP(k, N) {
    cin >> A[k];
    ll a = A[k];
    for (int i = 2; i * i <= e.prime.size(); i++) {
      if (!e.prime[i])
        continue;
      int cnt = 0;
      while (a % i == 0)
        a /= i, cnt++;
      m[i] = max(m[i], cnt);
    }
    if (a > 1)
      m[a] = max(m[a], 1);
  }

  mint lcm = 1;
  for (auto k : m)
    REP(i, k.second) lcm *= k.first;

  mint sum = 0;
  REP(i, N) {
    mint x = lcm / A[i];
    sum += x;
  }
  cout << sum.x << endl;

  return 0;
}