Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;

using namespace std;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b; // Be careful not to overflow
}

vector<int> pp;
void preparePrime(int maxp) {
  int i;
  vector<int> a(maxp + 1);
  for (i = 2; i <= maxp; i++) {
    int k = i + i;
    while (k <= maxp) {
      a[k] = 1;
      k += i;
    }
  }
  for (i = 2; i <= maxp; i++) {
    if (a[i] == 0)
      pp.push_back(i);
  }
  return;
}

void decompositPrime(ll a, vector<pair<ll, ll>> &vv) {
  int i;
  for (i = 0; i < (int)pp.size(); i++) {
    int count = 0;
    while (a % pp[i] == 0) {
      a /= pp[i];
      count++;
    }
    if (count > 0)
      vv.push_back(make_pair(pp[i], count));
    if (a == 1)
      break;
  }
  if (a > 1) {
    vv.push_back(make_pair(a, 1));
  }

  return;
}

ll mpow(ll x, ll n) { // x^n(mod M)
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll minv(ll x) { return mpow(x, MOD - 2); }

int main(int argc, char *argv[]) {
  int n;
  scanf("%d", &n);
  vector<int> a(n);

  preparePrime(1000000);

  map<ll, ll> mp;
  int i;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);

    vector<pair<ll, ll>> vv;
    decompositPrime(a[i], vv);
    int k;
    for (k = 0; k < (int)vv.size(); k++) {
      ll tmp = mp[vv[k].first];
      mp[vv[k].first] = MAX(tmp, vv[k].second);
    }
  }

  ll ans = 1;
  auto it = mp.begin();
  for (; it != mp.end(); ++it) {
    ll curr = it->first;
    ll num = it->second;
    ans = ans * mpow(curr, num) % MOD;
  }

  ll ans1 = 0;
  for (i = 0; i < n; i++) {
    ans1 = (ans1 + minv(a[i])) % MOD;
  }
  ans = ans * ans1 % MOD;

  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cstring>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#pragma warning(disable : 4996)

typedef long long ll;
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
// const long long MOD = 998244353;

using namespace std;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b; // Be careful not to overflow
}

vector<int> pp;
void preparePrime(int maxp) {
  int i;
  vector<int> a(maxp + 1);
  for (i = 2; i <= maxp; i++) {
    int k = i + i;
    while (k <= maxp) {
      a[k] = 1;
      k += i;
    }
  }
  for (i = 2; i <= maxp; i++) {
    if (a[i] == 0)
      pp.push_back(i);
  }
  return;
}

void decompositPrime(ll a, vector<pair<ll, ll>> &vv) {
  int i;
  for (i = 0; i < (int)pp.size(); i++) {
    int count = 0;
    while (a % pp[i] == 0) {
      a /= pp[i];
      count++;
    }
    if (count > 0)
      vv.push_back(make_pair(pp[i], count));
    if (a == 1)
      break;
  }
  if (a > 1) {
    vv.push_back(make_pair(a, 1));
  }

  return;
}

ll mpow(ll x, ll n) { // x^n(mod M)
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll minv(ll x) { return mpow(x, MOD - 2); }

int main(int argc, char *argv[]) {
  int n;
  scanf("%d", &n);
  vector<int> a(n);

  preparePrime(2000);

  map<ll, ll> mp;
  int i;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);

    vector<pair<ll, ll>> vv;
    decompositPrime(a[i], vv);
    int k;
    for (k = 0; k < (int)vv.size(); k++) {
      ll tmp = mp[vv[k].first];
      mp[vv[k].first] = MAX(tmp, vv[k].second);
    }
  }

  ll ans = 1;
  auto it = mp.begin();
  for (; it != mp.end(); ++it) {
    ll curr = it->first;
    ll num = it->second;
    ans = ans * mpow(curr, num) % MOD;
  }

  ll ans1 = 0;
  for (i = 0; i < n; i++) {
    ans1 = (ans1 + minv(a[i])) % MOD;
  }
  ans = ans * ans1 % MOD;

  printf("%lld\n", ans);
  return 0;
}