Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

struct eratosthenes {
  int n;
  vector<int> f;
  vector<pair<int, int>> primes;

  eratosthenes(int n) : f(n + 1) {
    ll i, j;
    f[0] = f[1] = -1;
    for (i = 2; i <= n; i++) {
      if (!f[i]) {
        f[i] = i;
        primes.push_back(make_pair(i, 0));
        for (j = i * i; j <= n; j += i) {
          if (!f[j]) {
            f[j] = i;
          }
        }
      }
    }
  }

  vector<int> genosis(int x) {
    vector<int> g;

    while (x != 1) {
      g.push_back(f[x]);
      x /= f[x];
    }

    return g;
  }

  vector<pair<int, int>> genosiscount(int x) {
    vector<pair<int, int>> gc;
    vector<int> g = genosis(x);

    if (g.empty() == 1) {
      return {};
    }

    gc.push_back(make_pair(f[x], 0));

    for (auto &i : g) {
      if (gc.back().first == i) {
        gc.back().second++;
      } else {
        gc.push_back(make_pair(i, 1));
      }
    }

    return gc;
  }
};

ll pow(ll x, ll y) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = (ans * x) % 1000000007;
    x = (x * x) % 1000000007;
    y >>= 1;
  }
  return ans;
}

int main(void) {
  eratosthenes a(1e6 + 1);
  int N;
  int A[10010];
  int i, j;
  ll ans = 0, B;
  ll mul = 1;

  cin >> N;

  for (i = 0; i < N; i++) {
    cin >> A[i];

    for (auto &j : a.genosiscount(A[i])) {
      for (auto &k : a.primes) {
        if (j.first == k.first && j.second > k.second) {
          k.second = j.second;
        }
      }
    }
  }

  for (auto &x : a.primes) {
    B = pow(x.first, x.second);
    mul = mul * B % 1000000007;
  }

  for (i = 0; i < N; i++) {
    ll add = mul * pow(A[i], 1000000005) % 1000000007;
    ans = (ans + add) % 1000000007;
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

struct eratosthenes {
  int n;
  vector<int> f;
  vector<pair<int, int>> primes;

  eratosthenes(int n) : f(n + 1) {
    ll i, j;
    f[0] = f[1] = -1;
    for (i = 2; i <= n; i++) {
      if (!f[i]) {
        f[i] = i;
        primes.push_back(make_pair(i, 0));
        for (j = i * i; j <= n; j += i) {
          if (!f[j]) {
            f[j] = i;
          }
        }
      }
    }
  }

  vector<int> genosis(int x) {
    vector<int> g;

    while (x != 1) {
      g.push_back(f[x]);
      x /= f[x];
    }

    return g;
  }

  vector<pair<int, int>> genosiscount(int x) {
    vector<pair<int, int>> gc;
    vector<int> g = genosis(x);

    if (g.empty() == 1) {
      return {};
    }

    gc.push_back(make_pair(f[x], 0));

    for (auto &i : g) {
      if (gc.back().first == i) {
        gc.back().second++;
      } else {
        gc.push_back(make_pair(i, 1));
      }
    }

    return gc;
  }
};

ll pow(ll x, ll y) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = (ans * x) % 1000000007;
    x = (x * x) % 1000000007;
    y >>= 1;
  }
  return ans;
}

int main(void) {
  eratosthenes a(1e6 + 1);
  int N;
  int A[10010];
  int i, j;
  ll ans = 0, B;
  ll mul = 1;

  cin >> N;

  for (i = 0; i < N; i++) {
    cin >> A[i];

    for (auto &j : a.genosiscount(A[i])) {
      for (auto &k : a.primes) {
        if (j.first == k.first && j.second > k.second) {
          k.second = j.second;
        }

        if (j.first < k.first) {
          break;
        }
      }
    }
  }

  for (auto &x : a.primes) {
    B = pow(x.first, x.second);
    mul = mul * B % 1000000007;
  }

  for (i = 0; i < N; i++) {
    ll add = mul * pow(A[i], 1000000005) % 1000000007;
    ans = (ans + add) % 1000000007;
  }

  cout << ans << endl;
}