Problem p02793 (C++)

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

using namespace std;
const long long MOD = 1e9 + 7;

vector<long long> primes;

void gen_primes() {
  primes.push_back(2);
  for (long long i = 3; i * i <= 1e6; i += 2) {
    bool is_ok = true;
    for (int j = 0; j < primes.size(); j++) {
      if (i % primes[j] == 0) {
        is_ok = false;
        break;
      }
    }
    if (is_ok)
      primes.push_back(i);
  }
}

bool is_prime(long long n) {
  if (n == 1)
    return false;
  for (int i = 0; primes[i] * primes[i] <= n; i++) {
    if (n % primes[i] == 0)
      return false;
  }
  return true;
}

long long modpow(long long a, long long n, long long mod) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ret;
}

long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

int main() {
  long long n, a[10009];
  map<long long, long long> mp;
  cin >> n;
  gen_primes();
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (is_prime(a[i]))
      mp[a[i]] = max(1LL, mp[a[i]]);
    else {
      long long t = a[i];
      for (int j = 0; j < primes.size(); j++) {
        long long cnt = 0;
        while (t % primes[j] == 0) {
          cnt++;
          t /= primes[j];
        }
        if (cnt > 0)
          mp[primes[j]] = max(cnt, mp[primes[j]]);
      }
      if (t > 1)
        mp[t] = max(1LL, mp[t]);
    }
  }
  long long lcm = 1;
  for (auto p : mp) {
    lcm = (lcm * modpow(p.first, p.second, MOD)) % MOD;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans = (ans + ((lcm * modinv(a[i], MOD)) % MOD)) % MOD;
  }
  cout << ans << endl;
}


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

using namespace std;
const long long MOD = 1e9 + 7;

vector<long long> primes;

void gen_primes() {
  primes.push_back(2);
  for (long long i = 3; i * i <= 1e7; i += 2) {
    bool is_ok = true;
    for (int j = 0; j < primes.size(); j++) {
      if (i % primes[j] == 0) {
        is_ok = false;
        break;
      }
    }
    if (is_ok)
      primes.push_back(i);
  }
}

bool is_prime(long long n) {
  if (n == 1)
    return false;
  for (int i = 0; primes[i] * primes[i] <= n; i++) {
    if (n % primes[i] == 0)
      return false;
  }
  return true;
}

long long modpow(long long a, long long n, long long mod) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ret;
}

long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

int main() {
  long long n, a[10009];
  map<long long, long long> mp;
  cin >> n;
  gen_primes();
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (is_prime(a[i]))
      mp[a[i]] = max(1LL, mp[a[i]]);
    else {
      long long t = a[i];
      for (int j = 0; j < primes.size(); j++) {
        long long cnt = 0;
        while (t % primes[j] == 0) {
          cnt++;
          t /= primes[j];
        }
        if (cnt > 0)
          mp[primes[j]] = max(cnt, mp[primes[j]]);
      }
      if (t > 1)
        mp[t] = max(1LL, mp[t]);
    }
  }
  long long lcm = 1;
  for (auto p : mp) {
    lcm = (lcm * modpow(p.first, p.second, MOD)) % MOD;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans = (ans + ((lcm * modinv(a[i], MOD)) % MOD)) % MOD;
  }
  cout << ans << endl;
}