Problem p02934 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long int
#define pb push_back
#define rep(i, a, b, c) for (ll i = (a); i <= (b); i += (c))
#define repb(i, a, b, c) for (ll i = (a); i >= (b); i -= (c))
#define MOD 1000000007
#define ld long double
#define mp make_pair
#define vpll vector<pair<ll, ll>>
#define vll vector<ll>
#define vld vector<ld>
#define vvll vector<vector<ll>>
#define vvld vector<vector<ld>>
#define pll pair<ll, ll>
#define pqll priority_queue<ll>
#define mll map<ll, ll>
#define mlc map<ll, char>
#define um unordered_map
#define umll um<ll, ll>
#define umlc um<ll, char>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define ln cout << "\n";
#define endl "\n"
#define test                                                                   \
  ll T;                                                                        \
  cin >> T;                                                                    \
  while (T--)
#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define show(w, size)                                                          \
  for (ll i = 0; i < size; i++)                                                \
    cout << w[i] << " ";
#define print(a) cout << a << "\n";
#define mset(dp, no) memset(dp, no, sizeof(dp))
#define input(a, n) rep(i, 0, n - 1, 1) cin >> a[i];
#define countbit __builtin_popcount // Number of setbits in decimal notation
#define fbo(k)                                                                 \
  find_by_order(k) // Valid for ordered set                  //Number of items
                   // strictly smaller than k
#define ook(k)                                                                 \
  order_of_key(k) // Valid for ordered set                  //K-th element in a
                  // set (counting from zero)
#define lb(v, val) lower_bound(all(v), val) - v.begin()
#define ub(v, val) upper_bound(all(v), val) - v.begin()
#define in insert
#define SS stringstream
// #define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  else if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll isPrime(ll n) {
  ll p = (ll)sqrt(n);
  rep(i, 2, p, 1) if (n % i == 0) return 0;
  return 1;
} // reuturn 1 if prime
ll pow(ll b, ll e) {
  if (e == 0)
    return 1;
  else if (e % 2 == 0) {
    ll a = pow(b, e / 2);
    return a * a;
  } else {
    ll a = pow(b, e / 2);
    return b * a * a;
  }
}
ll powm(ll x, ll y, ll m = MOD) {
  x = x % m;
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x;
    res %= m;
    y = y >> 1;
    x = x * x;
    x %= m;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll modInverse(ll a, ll m) { return powm(a, m - 2, m); }
bool issq(ll n) {
  ll p = sqrt(n);
  return p * p == n;
}

vll prime;
void seive(ll n) {
  prime.resize(n + 1, 1);
  prime[0] = 0, prime[1] = 0;
  for (ll i = 2; i * i <= n; i++)
    if (prime[i])
      for (ll j = i * i; j <= n; j += i)
        prime[j] = 0;
}

ll extended_GCD(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
ll mulInv(ll a, ll mod = MOD) {
  ll x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0)
    x += mod;
  return x;
}

ll find(ll num[], ll rem[], ll k, ll prod) {
  // Compute product of all numbers
  ll result = 0;
  // Apply above formula
  for (ll i = 0; i < k; i++) {
    ll pp = prod / num[i];
    result += rem[i] * mulInv(pp, num[i]) * pp;
  }

  return result % prod;
}

bool cmp(pair<ll, ll> &p1, pair<ll, ll> &p2) {
  if (p1.ff == p2.ff) {
    return p1.ss > p2.ss;
  }
  return p1.ff < p2.ff;
}
bool isPalindrome(string s) {
  ll i, j;
  for (i = 0, j = s.length() - 1; i <= j; i++, j--) {
    if (s[i] != s[j]) {
      return 0;
    }
  }
  return 1;
}

bool isSubSeq(string str1, string str2, ll m, ll n) {
  // Base Cases
  if (m == 0)
    return true;
  if (n == 0)
    return false;
  // If last characters of two strings are matching
  if (str1[m - 1] == str2[n - 1])
    return isSubSeq(str1, str2, m - 1, n - 1);
  // If last characters are not matching
  return isSubSeq(str1, str2, m, n - 1);
}

void modBigNumber(string num, ll m) {
  // Store the modulus of big number
  vector<int> vec;
  ll mod = 0;

  // Do step by step division
  for (int i = 0; i < num.size(); i++) {

    int digit = num[i] - '0';

    // Update modulo by concatenating
    // current digit.
    mod = mod * 10 + digit;

    // Update quotient
    int quo = mod / m;
    if ((vec.size() != 0) || (quo != 0)) // to remove initiale zeros
      vec.push_back(quo);
    // Update mod for next iteration.
    mod = mod % m;
  }
  // cout << "\nRemainder : " << mod << "\n";
  // cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
  return;
}

ll sumofdigits(ll a) {
  ll val = 0;
  while (a > 0) {
    val += a % 10;
    a /= 10;
  }
  return val;
}

ll stn(string s) // stn=string_to_number
{
  SS s1;
  s1 << s;
  ll num;
  s1 >> num;
  return num;
}

string nts(ll n) // nts=number_to_string
{
  SS s1;
  s1 << n;
  string s;
  s1 >> s;
  return s;
}

struct BIT {
  vll v;
  BIT(ll n) { v.resize(n + 1, 0); }
  void update_BIT(ll index, ll n) {
    index++;
    while (index < v.size()) {
      v[index] += n;
      index += index & (-index);
    }
  }
  ll get_sum(ll index) {
    ll sum = 0;
    index++;
    while (index > 0) {
      sum += v[index];
      index -= index & (-index);
    }
    return sum;
  }
};

class DSU {
public:
  vll v, size;

  DSU(ll n) {
    v.resize(n + 1);
    size.resize(n + 1, 1);
    rep(i, 1, n, 1) v[i] = i;
  }

  ll find_set(ll a) {
    if (v[a] == a)
      return a;
    else
      return v[a] = find_set(v[a]);
  }

  void union_sets(ll a, ll b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
      if (size[a] < size[b]) {
        v[a] = b;
        size[b] += size[a];
      } else {
        v[b] = a;
        size[a] += size[b];
      }
    }
  }
};

using namespace std;
int main() {
  fastIO ll n;
  cin >> n;
  ll a[n], val = 1;
  ld sum = 0;
  rep(i, 0, n - 1, 1) {
    cin >> a[i];
    val = lcm(a[i], val);
  }
  rep(i, 0, n - 1, 1) sum += val / a[i];
  cout << setprecision(8) << val / sum;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long int
#define pb push_back
#define rep(i, a, b, c) for (ll i = (a); i <= (b); i += (c))
#define repb(i, a, b, c) for (ll i = (a); i >= (b); i -= (c))
#define MOD 1000000007
#define ld long double
#define mp make_pair
#define vpll vector<pair<ll, ll>>
#define vll vector<ll>
#define vld vector<ld>
#define vvll vector<vector<ll>>
#define vvld vector<vector<ld>>
#define pll pair<ll, ll>
#define pqll priority_queue<ll>
#define mll map<ll, ll>
#define mlc map<ll, char>
#define um unordered_map
#define umll um<ll, ll>
#define umlc um<ll, char>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define ln cout << "\n";
#define endl "\n"
#define test                                                                   \
  ll T;                                                                        \
  cin >> T;                                                                    \
  while (T--)
#define isvowel(a) (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define show(w, size)                                                          \
  for (ll i = 0; i < size; i++)                                                \
    cout << w[i] << " ";
#define print(a) cout << a << "\n";
#define mset(dp, no) memset(dp, no, sizeof(dp))
#define input(a, n) rep(i, 0, n - 1, 1) cin >> a[i];
#define countbit __builtin_popcount // Number of setbits in decimal notation
#define fbo(k)                                                                 \
  find_by_order(k) // Valid for ordered set                  //Number of items
                   // strictly smaller than k
#define ook(k)                                                                 \
  order_of_key(k) // Valid for ordered set                  //K-th element in a
                  // set (counting from zero)
#define lb(v, val) lower_bound(all(v), val) - v.begin()
#define ub(v, val) upper_bound(all(v), val) - v.begin()
#define in insert
#define SS stringstream
// #define db(x) cout <<#x<<": "<<x<<'\n';
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

ll gcd(ll a, ll b) {
  if (a < b)
    return gcd(b, a);
  else if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
ll isPrime(ll n) {
  ll p = (ll)sqrt(n);
  rep(i, 2, p, 1) if (n % i == 0) return 0;
  return 1;
} // reuturn 1 if prime
ll pow(ll b, ll e) {
  if (e == 0)
    return 1;
  else if (e % 2 == 0) {
    ll a = pow(b, e / 2);
    return a * a;
  } else {
    ll a = pow(b, e / 2);
    return b * a * a;
  }
}
ll powm(ll x, ll y, ll m = MOD) {
  x = x % m;
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x;
    res %= m;
    y = y >> 1;
    x = x * x;
    x %= m;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll modInverse(ll a, ll m) { return powm(a, m - 2, m); }
bool issq(ll n) {
  ll p = sqrt(n);
  return p * p == n;
}

vll prime;
void seive(ll n) {
  prime.resize(n + 1, 1);
  prime[0] = 0, prime[1] = 0;
  for (ll i = 2; i * i <= n; i++)
    if (prime[i])
      for (ll j = i * i; j <= n; j += i)
        prime[j] = 0;
}

ll extended_GCD(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }
  ll x1, y1;
  ll gcd = extended_GCD(b % a, a, x1, y1);
  x = y1 - (b / a) * x1;
  y = x1;
  return gcd;
}
ll mulInv(ll a, ll mod = MOD) {
  ll x, y;
  extended_GCD(a, mod, x, y);
  if (x < 0)
    x += mod;
  return x;
}

ll find(ll num[], ll rem[], ll k, ll prod) {
  // Compute product of all numbers
  ll result = 0;
  // Apply above formula
  for (ll i = 0; i < k; i++) {
    ll pp = prod / num[i];
    result += rem[i] * mulInv(pp, num[i]) * pp;
  }

  return result % prod;
}

bool cmp(pair<ll, ll> &p1, pair<ll, ll> &p2) {
  if (p1.ff == p2.ff) {
    return p1.ss > p2.ss;
  }
  return p1.ff < p2.ff;
}
bool isPalindrome(string s) {
  ll i, j;
  for (i = 0, j = s.length() - 1; i <= j; i++, j--) {
    if (s[i] != s[j]) {
      return 0;
    }
  }
  return 1;
}

bool isSubSeq(string str1, string str2, ll m, ll n) {
  // Base Cases
  if (m == 0)
    return true;
  if (n == 0)
    return false;
  // If last characters of two strings are matching
  if (str1[m - 1] == str2[n - 1])
    return isSubSeq(str1, str2, m - 1, n - 1);
  // If last characters are not matching
  return isSubSeq(str1, str2, m, n - 1);
}

void modBigNumber(string num, ll m) {
  // Store the modulus of big number
  vector<int> vec;
  ll mod = 0;

  // Do step by step division
  for (int i = 0; i < num.size(); i++) {

    int digit = num[i] - '0';

    // Update modulo by concatenating
    // current digit.
    mod = mod * 10 + digit;

    // Update quotient
    int quo = mod / m;
    if ((vec.size() != 0) || (quo != 0)) // to remove initiale zeros
      vec.push_back(quo);
    // Update mod for next iteration.
    mod = mod % m;
  }
  // cout << "\nRemainder : " << mod << "\n";
  // cout << "Quotient : ";rep(i,0,vec.size()-1,1)cout<<vec[i];cout<<"\n";
  return;
}

ll sumofdigits(ll a) {
  ll val = 0;
  while (a > 0) {
    val += a % 10;
    a /= 10;
  }
  return val;
}

ll stn(string s) // stn=string_to_number
{
  SS s1;
  s1 << s;
  ll num;
  s1 >> num;
  return num;
}

string nts(ll n) // nts=number_to_string
{
  SS s1;
  s1 << n;
  string s;
  s1 >> s;
  return s;
}

struct BIT {
  vll v;
  BIT(ll n) { v.resize(n + 1, 0); }
  void update_BIT(ll index, ll n) {
    index++;
    while (index < v.size()) {
      v[index] += n;
      index += index & (-index);
    }
  }
  ll get_sum(ll index) {
    ll sum = 0;
    index++;
    while (index > 0) {
      sum += v[index];
      index -= index & (-index);
    }
    return sum;
  }
};

class DSU {
public:
  vll v, size;

  DSU(ll n) {
    v.resize(n + 1);
    size.resize(n + 1, 1);
    rep(i, 1, n, 1) v[i] = i;
  }

  ll find_set(ll a) {
    if (v[a] == a)
      return a;
    else
      return v[a] = find_set(v[a]);
  }

  void union_sets(ll a, ll b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
      if (size[a] < size[b]) {
        v[a] = b;
        size[b] += size[a];
      } else {
        v[b] = a;
        size[a] += size[b];
      }
    }
  }
};

using namespace std;
int main() {
  fastIO ll n;
  cin >> n;
  ll a[n], val = 1;
  ld sum = 0;
  rep(i, 0, n - 1, 1) {
    cin >> a[i];
    val = a[i] * val / __gcd(a[i], val);
  }
  rep(i, 0, n - 1, 1) sum += val / a[i];
  cout << setprecision(8) << val / sum;
  return 0;
}