Problem p02793 (C++)

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

vector<ll> calc_primes(ll n) {
  vector<ll> primes;
  vector<bool> filtered(n + 1);

  for (ll i = 2; i <= n; i++) {
    filtered[i] = false;
  }

  for (ll i = 2; i <= n; i++) {
    if (!filtered[i]) {
      primes.push_back(i);
      for (ll j = i; j <= n; j += i) {
        filtered[j] = true;
      }
    }
  }
  return primes;
}

ll pow_mod(ll n, ll m, ll mod) {
  if (m == 0)
    return 1;
  if (m % 2 == 0) {
    ll tmp = pow_mod(n, m / 2, mod);
    return (tmp * tmp) % mod;
  } else {
    return (n * pow_mod(n, m - 1, mod)) % mod;
  }
}

unordered_map<ll, ll> prime_factors(ll n, vector<ll> &primes) {
  unordered_map<ll, ll> ans;
  for (ll p : primes) {
    while (n % p == 0) {
      if (ans.count(p) == 0) {
        ans[p] = 0;
      }
      ans[p]++;
      n /= p;
    }
  }
  if (n > 1) {
    if (ans.count(n) == 0) {
      ans[n] = 0;
    }
    ans[n]++;
  }
  return ans;
}

int main() {
  const ll mod = 1000000007;
  vector<ll> primes = calc_primes(1000000);

  ll n;
  cin >> n;

  vector<ll> a(n);
  vector<unordered_map<ll, ll>> f(n);
  unordered_map<ll, ll> lcm;

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    f[i] = prime_factors(a[i], primes);
    for (auto factor : f[i]) {
      lcm[factor.first] = max(factor.second, lcm[factor.first]);
    }
  }

  // for (auto factor : lcm) {
  //   cout << factor.first << " " << factor.second << endl;
  // }

  ll lcm_all = 1;
  for (auto factor : lcm) {
    lcm_all *= pow_mod(factor.first, factor.second, mod);
    lcm_all %= mod;
  }

  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans += (lcm_all * pow_mod(a[i], mod - 2, mod)) % mod;
    ans %= mod;
  }
  cout << ans << endl;
}


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

vector<ll> calc_primes(ll n) {
  vector<ll> primes;
  vector<bool> filtered(n + 1);

  for (ll i = 2; i <= n; i++) {
    filtered[i] = false;
  }

  for (ll i = 2; i <= n; i++) {
    if (!filtered[i]) {
      primes.push_back(i);
      for (ll j = i; j <= n; j += i) {
        filtered[j] = true;
      }
    }
  }
  return primes;
}

ll pow_mod(ll n, ll m, ll mod) {
  if (m == 0)
    return 1;
  if (m % 2 == 0) {
    ll tmp = pow_mod(n, m / 2, mod);
    return (tmp * tmp) % mod;
  } else {
    return (n * pow_mod(n, m - 1, mod)) % mod;
  }
}

unordered_map<ll, ll> prime_factors(ll n, vector<ll> &primes) {
  unordered_map<ll, ll> ans;
  for (ll p : primes) {
    while (n % p == 0) {
      if (ans.count(p) == 0) {
        ans[p] = 0;
      }
      ans[p]++;
      n /= p;
    }
  }
  if (n > 1) {
    if (ans.count(n) == 0) {
      ans[n] = 0;
    }
    ans[n]++;
  }
  return ans;
}

int main() {
  const ll mod = 1000000007;
  vector<ll> primes = calc_primes(1000);

  ll n;
  cin >> n;

  vector<ll> a(n);
  vector<unordered_map<ll, ll>> f(n);
  unordered_map<ll, ll> lcm;

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    f[i] = prime_factors(a[i], primes);
    for (auto factor : f[i]) {
      lcm[factor.first] = max(factor.second, lcm[factor.first]);
    }
  }

  // for (auto factor : lcm) {
  //   cout << factor.first << " " << factor.second << endl;
  // }

  ll lcm_all = 1;
  for (auto factor : lcm) {
    lcm_all *= pow_mod(factor.first, factor.second, mod);
    lcm_all %= mod;
  }

  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans += (lcm_all * pow_mod(a[i], mod - 2, mod)) % mod;
    ans %= mod;
  }
  cout << ans << endl;
}