Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define MOD (1000000007LL)

using namespace std;

long long expmod(long long b, long long i) {
  if (i == 0)
    return 1;
  long long ans = 1;
  long long e = expmod(b, i / 2);
  e *= e;
  e %= MOD;
  if (i % 2 == 1)
    e *= b;
  e %= MOD;
  return e;
}

long long inv(long long n) { return expmod(n, MOD - 2); }

int main(void) {
  vector<long long> primes;
  primes.reserve(100000);
  primes.push_back(2);
  for (long long n = 3; n <= 1000000; n += 2) {
    for (auto itr = primes.begin(); (*itr) * (*itr) <= n && itr != primes.end();
         itr++) {
      if (n % (*itr) == 0)
        goto next;
    }
    primes.push_back(n);
  // cout << n << endl;
  next:;
  }

  int N;
  cin >> N;
  map<long long, long long> fact;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    long long AA = A[i];
    map<long long, long long> factt;
    for (auto itr = primes.begin(); itr != primes.end(); itr++) {
      while (AA % *itr == 0) {
        factt[*itr]++;
        AA /= *itr;
      }
      if (AA == 1)
        break;
    }
    for (auto itr = factt.begin(); itr != factt.end(); itr++) {
      if (fact[itr->first] < itr->second)
        fact[itr->first] = itr->second;
    }
  }

  long long lcm = 1;
  for (auto itr = fact.begin(); itr != fact.end(); itr++) {
    lcm *= expmod(itr->first, itr->second);
    lcm %= MOD;
  }
  // cout << lcm << endl;

  long long ans = 0;
  for (int i = 0; i < N; i++) {
    long long B = lcm * inv(A[i]);
    B %= MOD;
    ans += B;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}


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

#define MOD (1000000007LL)

using namespace std;

long long expmod(long long b, long long i) {
  if (i == 0)
    return 1;
  long long ans = 1;
  long long e = expmod(b, i / 2);
  e *= e;
  e %= MOD;
  if (i % 2 == 1)
    e *= b;
  e %= MOD;
  return e;
}

long long inv(long long n) { return expmod(n, MOD - 2); }

int main(void) {
  vector<long long> primes;
  primes.reserve(100000);
  primes.push_back(2);
  for (long long n = 3; n <= 1000000; n += 2) {
    for (auto itr = primes.begin(); (*itr) * (*itr) <= n && itr != primes.end();
         itr++) {
      if (n % (*itr) == 0)
        goto next;
    }
    primes.push_back(n);
  // cout << n << endl;
  next:;
  }

  int N;
  cin >> N;
  map<long long, long long> fact;
  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    long long AA = A[i];
    map<long long, long long> factt;
    for (auto itr = primes.begin(); itr != primes.end(); itr++) {
      while (AA % *itr == 0) {
        factt[*itr]++;
        AA /= *itr;
      }
      if (AA == 1)
        break;
      if (*itr * *itr > AA) {
        factt[AA]++;
        break;
      }
    }
    for (auto itr = factt.begin(); itr != factt.end(); itr++) {
      if (fact[itr->first] < itr->second)
        fact[itr->first] = itr->second;
    }
  }

  long long lcm = 1;
  for (auto itr = fact.begin(); itr != fact.end(); itr++) {
    lcm *= expmod(itr->first, itr->second);
    lcm %= MOD;
  }
  // cout << lcm << endl;

  long long ans = 0;
  for (int i = 0; i < N; i++) {
    long long B = lcm * inv(A[i]);
    B %= MOD;
    ans += B;
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}