Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
/* lcm_prime.cpp
    素因数分解の形で最小公倍数を求めるアルゴリズム
    (オーバーフローする場合などに使える)
*/

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

map<long long, int> prime_factor(long long n) { // 素因数分解
  map<long long, int> ret;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

/* lcm_prime(vector<long long> vec)
    入力：整数のベクトル vec
    出力：すべての要素の最小公倍数を素因数分解したmap
*/
map<long long, int> lcm_prime(const vector<long long> &vec) {
  map<long long, int> ret;
  for (int i = 0; i < vec.size(); i++) {
    map<long long, int> prime_fac = prime_factor(vec[i]);
    for (auto p : prime_fac) {
      if (ret.find(p.first) != ret.end()) {
        ret[p.first] = max(ret[p.first], p.second);
      } else {
        ret[p.first] = p.second;
      }
    }
  }
  return ret;
}

template <int mod> struct ModInt {
  int val;
  ModInt() : val(0) {}
  ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
  int getmod() { return mod; }
  ModInt &operator+=(const ModInt &p) {
    if ((val += p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((val += mod - p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-val); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return val == p.val; }
  bool operator!=(const ModInt &p) const { return val != p.val; }
  ModInt inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(long long n) const {
    ModInt ret(1), mul(val);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.val;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    long long t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
const int MOD = 1000000007; // if inv is needed, this shold be prime.
using modint = ModInt<MOD>;
int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  map<long long, int> lp = lcm_prime(A);

  modint ans = 0;
  for (int i = 0; i < N; i++) {
    map<long long, int> n = lp;
    map<long long, int> pf = prime_factor(A[i]);
    for (auto p : pf) {
      n[p.first] -= p.second;
    }
    modint tmp = 1;
    for (auto p : n) {
      tmp *= modint(p.first).pow(p.second);
    }
    ans += tmp;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
/* lcm_prime.cpp
    素因数分解の形で最小公倍数を求めるアルゴリズム
    (オーバーフローする場合などに使える)
*/

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

map<long long, int> prime_factor(long long n) { // 素因数分解
  map<long long, int> ret;
  for (long long i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

/* lcm_prime(vector<long long> vec)
    入力：整数のベクトル vec
    出力：すべての要素の最小公倍数を素因数分解したmap
*/
map<long long, int> lcm_prime(const vector<long long> &vec) {
  map<long long, int> ret;
  for (int i = 0; i < vec.size(); i++) {
    map<long long, int> prime_fac = prime_factor(vec[i]);
    for (auto p : prime_fac) {
      if (ret.find(p.first) != ret.end()) {
        ret[p.first] = max(ret[p.first], p.second);
      } else {
        ret[p.first] = p.second;
      }
    }
  }
  return ret;
}

template <int mod> struct ModInt {
  int val;
  ModInt() : val(0) {}
  ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
  int getmod() { return mod; }
  ModInt &operator+=(const ModInt &p) {
    if ((val += p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((val += mod - p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-val); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return val == p.val; }
  bool operator!=(const ModInt &p) const { return val != p.val; }
  ModInt inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(long long n) const {
    ModInt ret(1), mul(val);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.val;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    long long t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
const int MOD = 1000000007; // if inv is needed, this shold be prime.
using modint = ModInt<MOD>;
int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  map<long long, int> lp = lcm_prime(A);

  modint ans = 0;
  for (int i = 0; i < N; i++) {
    ans += modint(A[i]).inverse();
  }
  for (auto p : lp) {
    ans *= modint(p.first).pow(p.second);
  }
  cout << ans << endl;
  return 0;
}