Problem p02793 (C++)

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

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)

typedef long long ll;
const int MOD = 1000000007;
ll a_n(ll a, ll n);

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);

  map<int, int> lcm;
  rep(i, n) {
    map<int, int> prime = {}; // a[i]の素因数
    int temp = a.at(i);
    while ((temp % 2) == 0) {
      temp /= 2;
      if (prime.count(2))
        prime.at(2)++;
      else
        prime[2] = 1;
    }
    for (int j = 3; j * 2 <= a.at(i); j += 2) {
      while ((temp % j) == 0) {
        temp /= j;
        if (prime.count(j))
          prime.at(j)++;
        else
          prime[j] = 1;
      }
    }
    if (temp != 1)
      prime[temp] = 1;
    // primeの全キーをlcmと比較
    for (pair<int, int> p : prime) {
      int key = p.first;
      int value = p.second;
      if (lcm.count(key) && (lcm.at(key) < value))
        lcm.at(key) = value;
      if (!(lcm.count(key)))
        lcm[key] = value;
    }
  }

  ll ans = 0;
  rep(i, n) ans = (ans + a_n(a.at(i), MOD - 2));
  for (pair<int, int> p : lcm) {
    int key = p.first;
    int value = p.second;
    rep(i, value) ans = (ans * key) % MOD;
  }

  cout << ans << endl;

  return 0;
}

ll a_n(ll a, ll n) {
  ll ret = a, count = 1;
  int num = 1;
  vector<ll> memory(100);
  memory.at(0) = a;

  while (count * 2 < n) {
    ret = (ret * ret) % MOD;
    count *= 2;
    memory.at(num) = ret;
    num++;
  }
  if (count == n)
    return ret;
  else {
    bitset<100> bit = bitset<100>(n - count);
    rep(i, 100) {
      if (bit.test(i))
        ret = (ret * memory.at(i)) % MOD;
    }
    return ret;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)

typedef long long ll;
const int MOD = 1000000007;
ll a_n(ll a, ll n);

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);

  map<int, int> lcm;
  rep(i, n) {
    map<int, int> prime = {}; // a[i]の素因数
    int temp = a.at(i);
    while ((temp % 2) == 0) {
      temp /= 2;
      if (prime.count(2))
        prime.at(2)++;
      else
        prime[2] = 1;
    }
    for (int j = 3; j * j <= temp; j += 2) {
      while ((temp % j) == 0) {
        temp /= j;
        if (prime.count(j))
          prime.at(j)++;
        else
          prime[j] = 1;
      }
    }
    if (temp != 1)
      prime[temp] = 1;
    // primeの全キーをlcmと比較
    for (pair<int, int> p : prime) {
      int key = p.first;
      int value = p.second;
      if (lcm.count(key) && (lcm.at(key) < value))
        lcm.at(key) = value;
      if (!(lcm.count(key)))
        lcm[key] = value;
    }
  }

  ll ans = 0;
  rep(i, n) ans = (ans + a_n(a.at(i), MOD - 2));
  for (pair<int, int> p : lcm) {
    int key = p.first;
    int value = p.second;
    rep(i, value) ans = (ans * key) % MOD;
  }

  cout << ans << endl;

  return 0;
}

ll a_n(ll a, ll n) {
  ll ret = a, count = 1;
  int num = 1;
  vector<ll> memory(100);
  memory.at(0) = a;

  while (count * 2 < n) {
    ret = (ret * ret) % MOD;
    count *= 2;
    memory.at(num) = ret;
    num++;
  }
  if (count == n)
    return ret;
  else {
    bitset<100> bit = bitset<100>(n - count);
    rep(i, 100) {
      if (bit.test(i))
        ret = (ret * memory.at(i)) % MOD;
    }
    return ret;
  }
}