Problem p02793 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include "bits/stdc++.h"

#define REP(i, x, y) for (ll i = (x); i < (y); i++)
#define RREP(i, x, y) for (ll i = (y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
// #define int long long

using namespace std;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

#define dump(x) cout << #x << " = " << (x) << endl
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl

typedef long long ll;

const ll MOD = 1e9 + 7;

long long modPow(long long a, long long x, long long p) {
  long long res = 1;
  while (x > 0) {
    if (x % 2 != 0) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

long long modInverse(long long a, long long p) {
  // calculates the modular multiplicative of a mod m.
  //(assuming p is prime).
  return modPow(a, p - 2, p);
}

long long modFactorial(long long a, long long p) {
  long long fact = 1;
  for (long long i = 2; i <= a; i++) {
    fact *= i;
    fact %= p;
  }
  return fact;
}

long long combination(long long n, long long k, long long p) {
  // calculates C(n,k) mod p (assuming p is prime).

  long long numerator = 1; // n * (n-1) * ... * (n-k+1)
  for (int i = 0; i < k; i++) {
    numerator = (numerator * (n - i)) % p;
  }

  long long denominator = 1; // k!
  for (int i = 1; i <= k; i++) {
    denominator = (denominator * i) % p;
  }

  // numerator / denominator mod p.
  return (numerator * modInverse(denominator, p)) % p;
}

long long getGcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  long long r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

long long getLcm(long long a, long long b) { return a * b / getGcd(a, b); }

class EFlatten {
public:
  void solve(std::istream &in, std::ostream &out) {
    ll N;
    in >> N;
    vector<ll> A(N);
    REP(i, 0, N) in >> A[i];
    vector<ll> primes = {2};
    REP(i, 3, 1e6 + 1) {
      bool isPrime = true;
      ll root = pow(i, 0.5);
      for (const auto &prime : primes) {
        if (root < prime)
          break;
        if (i % prime == 0) {
          isPrime = false;
          break;
        }
      }
      if (isPrime)
        primes.push_back(i);
    }
    unordered_map<ll, ll> lcmMap;
    REP(i, 0, N) {
      ll a = A[i];
      for (const auto &prime : primes) {
        if (a == 1)
          break;
        ll cnt = 0;
        while (a % prime == 0) {
          a /= prime;
          cnt++;
        }
        if (cnt > 0)
          lcmMap[prime] = max(lcmMap[prime], cnt);
      }
    }
    ll lcm = 1;
    for (const auto &prime : primes) {
      REP(i, 0, lcmMap[prime]) {
        lcm *= prime;
        lcm %= MOD;
      }
    }
    ll answer = 0;
    REP(i, 0, N) {
      ll b = lcm * modInverse(A[i], MOD);
      answer += b;
      answer %= MOD;
    }
    out << answer << "\n";
  }
};

signed main() {
  EFlatten solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <fstream>
#include <iostream>

#include "bits/stdc++.h"

#define REP(i, x, y) for (ll i = (x); i < (y); i++)
#define RREP(i, x, y) for (ll i = (y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
// #define int long long

using namespace std;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

#define dump(x) cout << #x << " = " << (x) << endl
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl

typedef long long ll;

const ll MOD = 1e9 + 7;

long long modPow(long long a, long long x, long long p) {
  long long res = 1;
  while (x > 0) {
    if (x % 2 != 0) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    x /= 2;
  }
  return res;
}

long long modInverse(long long a, long long p) {
  // calculates the modular multiplicative of a mod m.
  //(assuming p is prime).
  return modPow(a, p - 2, p);
}

long long modFactorial(long long a, long long p) {
  long long fact = 1;
  for (long long i = 2; i <= a; i++) {
    fact *= i;
    fact %= p;
  }
  return fact;
}

long long combination(long long n, long long k, long long p) {
  // calculates C(n,k) mod p (assuming p is prime).

  long long numerator = 1; // n * (n-1) * ... * (n-k+1)
  for (int i = 0; i < k; i++) {
    numerator = (numerator * (n - i)) % p;
  }

  long long denominator = 1; // k!
  for (int i = 1; i <= k; i++) {
    denominator = (denominator * i) % p;
  }

  // numerator / denominator mod p.
  return (numerator * modInverse(denominator, p)) % p;
}

long long getGcd(long long a, long long b) {
  if (a < b)
    swap(a, b);
  long long r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}

long long getLcm(long long a, long long b) { return a * b / getGcd(a, b); }

class EFlatten {
public:
  void solve(std::istream &in, std::ostream &out) {
    ll N;
    in >> N;
    vector<ll> A(N);
    REP(i, 0, N) in >> A[i];
    vector<ll> primes = {2};
    REP(i, 3, 1e6 + 1) {
      bool isPrime = true;
      ll root = pow(i, 0.5);
      for (const auto &prime : primes) {
        if (root < prime)
          break;
        if (i % prime == 0) {
          isPrime = false;
          break;
        }
      }
      if (isPrime)
        primes.push_back(i);
    }
    unordered_map<ll, ll> lcmMap;
    REP(i, 0, N) {
      ll a = A[i];
      if (binary_search(all(primes), a)) {
        lcmMap[a] = max(lcmMap[a], 1LL);
        continue;
      }
      for (const auto &prime : primes) {
        if (a == 1)
          break;
        ll cnt = 0;
        while (a % prime == 0) {
          a /= prime;
          cnt++;
        }
        if (cnt > 0)
          lcmMap[prime] = max(lcmMap[prime], cnt);
      }
    }
    ll lcm = 1;
    for (const auto &prime : primes) {
      REP(i, 0, lcmMap[prime]) {
        lcm *= prime;
        lcm %= MOD;
      }
    }
    ll answer = 0;
    REP(i, 0, N) {
      ll b = lcm * modInverse(A[i], MOD);
      answer += b;
      answer %= MOD;
    }
    out << answer << "\n";
  }
};

signed main() {
  EFlatten solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}