Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

const int mod = 1000000007;
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;
  }
};

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

int main() {

  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  vector<map<ll, int>> f(n);
  rep(i, n) f[i] = prime_factorization(a[i]);

  // 各素因数の最大値を取得
  map<int, int> mx;
  rep(i, n) {
    for (auto t : f[i]) {
      int tei = t.first;
      int zyo = t.second;
      if (mx[tei] < zyo)
        mx[tei] = zyo;
    }
  }
  // lcm
  mint x = 1;
  for (auto t : mx) {
    int tei = t.first;
    int zyo = t.second;
    x *= pow(tei, zyo);
  }

  mint ans = 0;
  rep(i, n) {
    mint tmp = 1;
    for (auto t : mx) {
      int tei = t.first;
      int zyo = t.second;
      tmp = x / a[i];
    }
    ans += tmp;
  }
  cout << ans.x << endl;
  return 0;
}

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

const int mod = 1000000007;
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;
  }
};

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

int main() {

  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  vector<map<ll, int>> f(n);
  rep(i, n) f[i] = prime_factorization(a[i]);

  // 各素因数の最大値を取得
  map<int, int> mx;
  rep(i, n) {
    for (auto t : f[i]) {
      int tei = t.first;
      int zyo = t.second;
      if (mx[tei] < zyo)
        mx[tei] = zyo;
    }
  }
  // lcm
  mint x = 1;
  for (auto t : mx) {
    int tei = t.first;
    int zyo = t.second;
    x *= pow(tei, zyo);
  }

  mint ans = 0;
  rep(i, n) { ans += x / a[i]; }
  cout << ans.x << endl;
  return 0;
}