Problem p01560 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return (a % b ? gcd(a % b, b) : b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n, m;
  cin >> n >> m;
  vi a(n);
  REP(i, n) cin >> a[i];
  vector<ld> p(n);
  REP(i, n) {
    ld a;
    cin >> a;
    p[i] = a / 100.0;
  }
  vector<ll> cnt(1 << n);
  REP(i, 1 << n) {
    ll t = 1;
    bool f = false;
    REP(j, n) {
      if (!((i >> j) & 1))
        continue;
      ll tmp = a[j] / gcd(t, a[j]);
      if (t > LLONG_MAX / tmp)
        f = true;
      else
        t *= tmp;
    }
    if (f)
      cnt[i] = 0;
    else
      cnt[i] = m / t;
  }

  REP(i, n) REP(j, 1 << n) if (j & 1 << i) cnt[j] -= cnt[j ^ 1 << i];

  ld ans = 0;
  REP(i, 1 << n) {
    ld exp = 1.0;
    REP(j, n) {
      if ((i >> j) & 1)
        exp *= p[j];
      else
        exp *= 1.0 - p[j];
    }
    ans += exp * (m - abs(cnt[i]));
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
const ll INF = 114514810;
const ll MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

ll n, m;
ll a[20];
int p[20];
double ans;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return (a % b ? gcd(a % b, b) : b);
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void calc(vi v) {
  if (v.size() == 0)
    return;
  double exp = 1;
  REP(i, n) {
    if (find(ALL(v), i) != v.end()) {
      exp *= p[i] / 100.0;
    } else {
      exp *= (100 - p[i]) / 100.0;
    }
  }
  ll sum = 0;
  REP(i, 1 << v.size()) {
    vi w;
    REP(j, v.size()) {
      if ((i >> j) & 1 == 1)
        w.push_back(j);
    }
    if (w.size() == 0)
      continue;
    ll lc = 1;
    REP(j, w.size()) { lc = lcm(lc, a[v[w[j]]]); }
    sum += m / lc * (w.size() % 2 == 1 ? 1 : -1);
  }
  ans += sum * exp;
}

void solve(int i, vi v) {
  if (i == n) {
    calc(v);
  } else {
    solve(i + 1, v);
    v.push_back(i);
    solve(i + 1, v);
  }
}

int main() {
  cin >> n >> m;
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> p[i];
  ans = 0;
  vi v;
  solve(0, v);

  cout << D10 << ans << endl;
  return 0;
}