alexjercan/bugnet huggingface dataset:
Problem p01558 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define MAX 300002

using namespace std;
typedef unsigned long long ull;

ull extgcd(ull a, ull b, ull &x, ull &y) {
  ull d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else
    x = 1, y = 0;
  return d;
}

ull mod_inv(ull a, ull m) {
  ull x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

int n, m, L, R, keta;
char s[MAX], o[5];
set<ull> ans;
const ull B = 9973ULL;
const ull inv = 9063375710536127837ULL;
// const ull B = 973;
ull H, Bmax[MAX];

int main() {

  // cout << ULLONG_MAX << endl;
  // cout << mod_inv(1000000007ULL,18446744073709551615ULL) << endl;

  Bmax[0] = 1ULL;
  REP(i, 1, MAX) Bmax[i] = Bmax[i - 1] * B;
  L = R = keta = 0;
  scanf("%d %d", &n, &m);
  scanf("%s", &s[0]);
  H = s[0];

  rep(_, m) {
    scanf("%s", &o[0]);

    if (o[0] == 'R') {
      if (o[1] == '+') {
        H = H * B + s[++R];
      } else {
        // H = (H - s[R--]) * invB;
        H = (H - s[R--]) * inv;
      }
    } else {
      if (o[1] == '+') {
        H = H - Bmax[R - L] * s[L++];
      } else {
        H = H + Bmax[R + 1 - L] * s[L - 1];
        L--;
      }
    }
    /*
      cout << "Bmax = " << Bmax << endl;
      cout << "[" << L << "," << R << "]" << endl;
      cout << "s : ";
      REP(i,L,R+1)cout << s[i];
      cout << endl;
      cout << "hash : " << H << endl;
    */
    ans.insert(H);
  }

  cout << ans.size() << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 29)
#define MAX 300001

using namespace std;
typedef unsigned long long ull;
int n, m, L, R;
char s[MAX], o[5];
set<ull> ans;
// const ull B = 1000000007;
const ull B = 973;
ull H, Bmax;

int main() {

  Bmax = 1;
  L = R = 0;
  scanf("%d %d", &n, &m);
  scanf("%s", &s[0]);
  H = s[0];

  rep(_, m) {
    scanf("%s", &o[0]);

    if (o[0] == 'R') {
      if (o[1] == '+') {
        R++;
        Bmax *= B;
        H = H + s[R] * Bmax;
      } else {
        H = H - s[R] * Bmax;
        assert(Bmax % B == 0);
        Bmax /= B;
        R--;
      }
    } else {
      if (o[1] == '+') {
        assert((H - s[L]) % B == 0);
        H = (H - s[L]) / B;
        Bmax /= B;
        L++;
      } else {
        L--;
        H = H * B + s[L];
        Bmax *= B;
      }
    }
    /*
    cout << "Bmax = " << Bmax << endl;
    cout << "[" << L << "," << R << "]" << endl;
    cout << "s : ";
    REP(i,L,R+1)cout << s[i];
    cout << endl;
    cout << "hash : " << H << endl;
    */
    ans.insert(H);
  }

  cout << ans.size() << endl;

  return 0;
}
Problem p01560 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

// 最大公約数
long long gcd(long long a, long long b) {
  while (b != 0) {
    long long tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}

int main() {
  int n;
  long long m;
  cin >> n >> m;

  vector<long long> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  vector<double> p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
    p[i] /= 100.0;
  }

  double ret = 0.0;
  for (int i = 1; i < (1 << n); ++i) {
    bitset<20> use(i);
    long long x = 1; // 選択された数字の最小公倍数
    for (int j = 0; j < n; ++j) {
      if (use[j]) {
        x /= gcd(x, a[j]);
        if (LLONG_MAX / x < a[j]) {
          x = -1;
          break;
        }
        x *= a[j];
      }
    }

    if (x == -1)
      continue;
    long long y = m / x; // xで割り切れる整数の個数

    double q = 1.0;
    for (int j = 0; j < n; ++j) {
      if (use[j])
        q *= p[j];
    }

    if (use.count() % 2 == 0)
      ret -= y * q;
    else
      ret += y * q;
  }

  printf("%.10f\\n", ret);

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

// 最大公約数
long long gcd(long long a, long long b) {
  while (b != 0) {
    long long tmp = a % b;
    a = b;
    b = tmp;
  }
  return a;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<long long> a(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  vector<double> p(n);
  for (int i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    p[i] = tmp / 100.0;
  }

  double ret = 0.0;
  for (int i = 1; i < (1 << n); ++i) {
    bitset<20> use(i);
    long long x = 1;
    for (int j = 0; j < n; ++j) {
      if (use[j]) {
        x /= gcd(x, a[j]);
        if (LLONG_MAX / x < a[j]) {
          x = -1;
          break;
        }
        x *= a[j];
      }
    }

    if (x == -1)
      continue;
    x = m / x;

    double q = 1.0;
    for (int j = 0; j < n; ++j) {
      if (use[j])
        q *= p[j];
    }

    if (use.count() % 2 == 0)
      ret -= x * q;
    else
      ret += x * q;
  }

  printf("%.10f\\n", ret);

  return 0;
}
Problem p01560 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

long long int gcd(long long int x, long long int y) {
  if (x < y)
    swap(x, y);
  while (y) {
    x %= y;
    swap(x, y);
  }
  return x;
}

int main() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> A(n);
  vector<double> P(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  for (int i = 0; i < n; ++i) {
    cin >> P[i];
    P[i] /= 100.0;
  }
  double ans = 0;
  vector<long long int> LCM;
  vector<double> P_;
  vector<int> C;
  LCM.push_back(1);
  P_.push_back(1.0);
  C.push_back(0);
  for (int i = 0; i < n; ++i) {
    long long int s = LCM.size();
    for (long long int j = 0; j < s; ++j) {
      if (LCM[j] / gcd(LCM[j], A[i]) > m / A[i])
        continue;
      long long int t = LCM[j] / gcd(LCM[j], A[i]) * A[i];
      if (t > m)
        continue;
      double p = P_[j] * P[i];
      int c = C[j] + 1;
      if (c % 2)
        ans += m / t * p;
      else
        ans -= m / t * p;
      LCM.push_back(t);
      P_.push_back(p);
      C.push_back(c);
      // cout << t << " " << p << " " << c << endl;
    }
  }
  printf("%.12f\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

long long int gcd(long long int x, long long int y) {
  while (y) {
    if (x < y)
      swap(x, y);
    if (!y)
      return x;
    x %= y;
  }
  return x;
}

int main() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> A(n);
  vector<int> P(n);
  for (int i = 0; i < n; ++i)
    cin >> A[i];
  for (int i = 0; i < n; ++i)
    cin >> P[i];
  double ans = 0;
  vector<long long int> LCM;
  vector<double> P_;
  vector<int> C;
  LCM.push_back(1);
  P_.push_back(1.0);
  C.push_back(0);
  for (int i = 0; i < n; ++i) {
    long long int s = LCM.size();
    for (long long int j = 0; j < s; ++j) {
      long long int t = LCM[j] / gcd(LCM[j], A[i]) * A[i];
      if (t > m)
        continue;
      double p = P_[j] * P[i] / 100.0;
      int c = C[j] + 1;
      if (c % 2)
        ans += m / t * p;
      else
        ans -= m / t * p;
      LCM.push_back(t);
      P_.push_back(p);
      C.push_back(c);
      // cout << t << " " << p << " " << c << endl;
    }
  }
  printf("%.12f\\n", ans);
  return 0;
}
Problem p01560 (C++) — buggy submission, verdict: Runtime Error

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

__int128 gcd(__int128 a, __int128 b) { return (a == 0 ? b : gcd(b % a, a)); }

int main() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];

  auto val = [&](int comb) {
    if (comb == 0)
      return 0LL;
    __int128 res = 1;
    for (int i = 0; i < n; i++) {
      if (comb & (1 << i)) {
        res = (res * a[i] / gcd(res, a[i]));
        if (res > 1e18 + 3) {
          return 0LL;
        }
      }
    }
    return static_cast<long long int>(m / res);
  };

  double res = 0;
  for (int i = 0; i < (1 << n); i++) {
    int cnt = 0;
    double pos = 1;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        pos *= double(p[j]) / 100;
        cnt++;
      }
    }
    res += pos * val(i) * ((cnt % 2) * 2 - 1);
  }
  cout << fixed << setprecision(10);
  cout << res << endl;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<long long int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> p(n);
  for (int i = 0; i < n; i++)
    cin >> p[i];

  auto val = [&](int comb) {
    if (comb == 0)
      return 0LL;
    long long int res = 1;
    for (int i = 0; i < n; i++) {
      if (comb & (1 << i)) {
        double buf = double(res) * a[i];
        if (buf > 1e18 + 3) {
          return 0LL;
        }
        res *= a[i];
      }
    }
    return m / res;
  };

  double res = 0;
  for (int i = 0; i < (1 << n); i++) {
    int cnt = 0;
    double pos = 1;
    for (int j = 0; j < n; j++) {
      if (i & (1 << j)) {
        pos *= double(p[j]) / 100;
        cnt++;
      }
    }
    res += pos * val(i) * ((cnt % 2) * 2 - 1);
  }
  cout << fixed << setprecision(10);
  cout << res << endl;
  return 0;
}
Problem p01560 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#include <queue>
typedef long long ll;

#include <cstdio>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}
inline ll getLL() {
  ll s;
  scanf("%lld", &s);
  return s;
}
#include <set>
using namespace std;

template <class T> T gcd(T a, T b) {
  if (a > b) {
    T tmp = b;
    b = a;
    a = tmp;
  }
  if (a == 0)
    return b;
  return gcd(a, b % a);
}

int main() {
  const int n = getInt();
  const ll m = getLL();
  vector<ll> a(n);
  REP(i, n) a[i] = getLL();
  vector<int> p(n);
  REP(i, n) p[i] = getInt();

  double ans = 0.0;

  REP(i, 1 << n) if (i) {
    double prob = 1.0;
    ll mul = 1;

    REP(j, n) if (i & (1 << j)) {
      prob *= p[j] / 100.0;

      if (mul > 0) {
        ll next = mul / gcd(mul, a[j]);
        if (next > m / a[j])
          mul = -1;
        else
          mul = next * a[j];
      }
    }

    if (mul > 0) {
      if (__builtin_popcount(i) % 2)
        ans += prob * (m / mul);
      else
        ans -= prob * (m / mul);
    }
  }

  printf("%.9f\\n", ans);

  return 0;
}

Error: TLE

Fixed submission:

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

#include <queue>
typedef long long ll;

#include <cstdio>
inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}
inline ll getLL() {
  ll s;
  scanf("%lld", &s);
  return s;
}
#include <set>
using namespace std;

template <class T> T gcd(T a, T b) {
  if (a > b) {
    T tmp = b;
    b = a;
    a = tmp;
  }
  if (a == 0)
    return b;
  return gcd(a, b % a);
}

int main() {
  const int n = getInt();
  const ll m = getLL();
  vector<ll> a(n);
  REP(i, n) a[i] = getLL();
  vector<int> p(n);
  REP(i, n) p[i] = getInt();

  double ans = 0.0;

  REP(i, 1 << n) if (i) {
    double prob = 1.0;
    ll mul = 1;

    REP(j, n) if (i & (1 << j)) {
      prob *= p[j] / 100.0;

      ll next = mul / gcd(mul, a[j]);
      if (mul > 0) {
        if (next > m / a[j])
          mul = -1;
        else
          mul = next * a[j];
      }
    }

    if (mul > 0) {
      if (__builtin_popcount(i) % 2)
        ans += prob * (m / mul);
      else
        ans -= prob * (m / mul);
    }
  }

  printf("%.9f\\n", ans);

  return 0;
}
Problem p01560 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;

int main() {
  ll N, M;
  cin >> N >> M;

  vector<ll> A(N), P(N), MSB(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];

    for (int k = 0; k < 62; k++) {
      if (A[i] >> k & 1) {
        MSB[i] = k;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cin >> P[i];
  }

  vector<ll> divisor(1 << N), popcnt(1 << N);
  for (int bit = 0; bit < (1 << N); bit++) {
    popcnt[bit] = __builtin_popcountll(bit);

    bool overflow = false;
    ll msb_sum = 0, mul = 1;
    for (int i = 0; i < N; i++) {
      if (bit >> i & 1) {
        ll val = A[i] / __gcd(mul, A[i]);
        if (1.0 * val * mul > 1.2e18) {
          overflow = true;
          break;
        } else {
          mul = mul * val;
        }
      }
    }

    if (!overflow) {
      divisor[bit] = M / mul;
    }
  }

  divisor[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int bit = 0; bit < (1 << N); bit++) {
      if (bit >> i & 1)
        divisor[bit] -= divisor[bit ^ (1 << i)];
    }
  }

  long double ans = 0;
  for (int bit = 0; bit < (1 << N); bit++) {
    long double prob = 1.0;
    for (int i = 0; i < N; i++) {
      if (bit >> i & 1) {
        prob *= 1.0 * P[i];
      } else {
        prob *= 1.0 * (100 - P[i]);
      }
    }
    ll sum = llabs(divisor[bit]);
    ans += prob * sum;
  }
  printf("%.12Lf\\n", ans / pow(100, N));
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;

int main() {
  int N, M;
  cin >> N >> M;

  vector<ll> A(N), P(N), MSB(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];

    for (int k = 0; k < 62; k++) {
      if (A[i] >> k & 1) {
        MSB[i] = k;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cin >> P[i];
  }

  vector<ll> divisor(1 << N), popcnt(1 << N);
  for (int bit = 0; bit < (1 << N); bit++) {
    popcnt[bit] = __builtin_popcount(bit);

    bool overflow = false;
    ll msb_sum = 0, mul = 1;
    for (int i = 0; i < N; i++) {
      if (bit >> i & 1) {
        msb_sum += MSB[i];
        if (msb_sum >= 60) {
          overflow = true;
          break;
        } else {
          mul *= A[i];
        }
      }
    }

    if (!overflow) {
      divisor[bit] = M / mul;
    }
  }

  double ans = 0;
  for (int bit = 1; bit < (1 << N); bit++) {
    double prob = 1.0;
    for (int i = 0; i < N; i++) {
      if (bit >> i & 1) {
        prob *= 1.0 * P[i] / 100.0;
      } else {
        prob *= 1.0 * (100 - P[i]) / 100.0;
      }
    }

    double sum = 0.0;
    for (int mask = bit; mask > 0; mask = (mask - 1) & bit) {
      if (popcnt[mask] % 2)
        sum += divisor[mask];
      else
        sum -= divisor[mask];
    }

    // printf("bit = %d, prob = %.12f, sum = %.12f\\n", bit, prob, sum);
    ans += prob * sum;
  }
  printf("%.12f\\n", ans);
  return 0;
}
Problem p01560 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
using namespace std;

long double dp[2 << 20][2], prev_[2 << 20][2];
long long n, m, a[22];
long double p[22];

long long gcd(long long a1, long long b1) {
  if (b1 == 0)
    return a1;
  return gcd(b1, a1 % b1);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i] *= 0.01;
  }
  prev_[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      // パターン 1 : 何も取らない
      dp[j][0] += prev_[j][0] * (1.0L - p[i]);
      dp[j][1] += prev_[j][1] * (1.0L - p[i]);

      // パターン 2 : 取るが包除はしない
      dp[j][0] += prev_[j][0] * p[i];
      dp[j][1] += prev_[j][1] * p[i];

      // パターン 3 : 取って包除もする
      dp[j + (1 << i)][1] += prev_[j][0] * p[i];
      dp[j + (1 << i)][0] += prev_[j][1] * p[i];
    }
    for (int j = 0; j < (1 << n); j++) {
      prev_[j][0] = dp[j][0];
      dp[j][0] = 0;
      prev_[j][1] = dp[j][1];
      dp[j][1] = 0;
    }
  }
  long double ans = 0;
  for (int i = 1; i < (1 << n); i++) {
    long long F = 1;
    for (int j = 0; j < n; j++) {
      if ((i / (1 << j)) % 2 == 0)
        continue;
      long long G = gcd(F, a[j]);
      if (log2(1.0L * F) + log2(1.0L * a[j]) - log2(1.0L * G) >= 60) {
        F = (1LL << 60);
        break;
      }
      F = F / G * a[j];
    }
    long long A = m / F;
    ans -= 1.0L * A * prev_[i][0];
    ans += 1.0L * A * prev_[i][1];
  }
  printf("%.12Lf\\n", ans);
  return 0;
}


Error: WA

Fixed submission:

#include <iostream>
using namespace std;

long double dp[1 << 20][2], prev_[1 << 20][2];
long long n, m, a[22];
long double p[22];

long long gcd(long long a1, long long b1) {
  if (b1 == 0)
    return a1;
  return gcd(b1, a1 % b1);
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i] *= 0.01;
  }
  prev_[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      // パターン 1 : 何も取らない
      dp[j][0] += prev_[j][0] * (1.0L - p[i]);
      dp[j][1] += prev_[j][1] * (1.0L - p[i]);

      // パターン 2 : 取るが包除はしない
      dp[j][0] += prev_[j][0] * p[i];
      dp[j][1] += prev_[j][1] * p[i];

      // パターン 3 : 取って包除もする
      dp[j + (1 << i)][1] += prev_[j][0] * p[i];
      dp[j + (1 << i)][0] += prev_[j][1] * p[i];
    }
    for (int j = 0; j < (1 << n); j++) {
      prev_[j][0] = dp[j][0];
      dp[j][0] = 0;
      prev_[j][1] = dp[j][1];
      dp[j][1] = 0;
    }
  }
  long double ans = 0;
  for (int i = 0; i < (1 << n); i++) {
    long long F = 1;
    for (int j = 0; j < n; j++) {
      if ((i / (1 << j)) % 2 == 0)
        continue;
      long long G = gcd(F, a[j]);
      if (m / F < G) {
        F = (1LL << 60);
        break;
      }
      F = F / G * a[j];
    }
    long long A = m / F;
    ans -= 1.0L * A * prev_[i][0];
    ans += 1.0L * A * prev_[i][1];
  }
  printf("%.12Lf\\n", 1.0L * m + ans);
  return 0;
}
Problem p01560 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n, m;
  cin >> n >> m;
  vector<Int> a(n), p(n);
  for (Int i = 0; i < n; i++)
    cin >> a[i];
  for (Int i = 0; i < n; i++)
    cin >> p[i];
  vector<Int> dp(1 << n, 0);
  for (Int b = 0; b < (1 << n); b++) {
    Int t = 1;
    for (Int i = 0; i < n; i++) {
      if ((b >> i) & 1) {
        Int k = a[i] / __gcd(t, a[i]);
        t = (t <= m / k ? t * k : m + 1);
      }
    }
    dp[b] = m / t;
  }

  for (Int i = 0; i < n; i++)
    for (Int b = 0; b < (1 << n); b++)
      if ((b >> i) & 1)
        dp[b] -= dp[b ^ (1 << i)];

  double ans = 0;
  for (Int b = 0; b < (1 << n); b++) {
    double po = 1;
    for (Int i = 0; i < n; i++)
      if ((b >> i) & 1)
        po *= p[i] / 100.0;
      else
        po *= 1.0 - (p[i] / 100.0);
    ans += po * (m - abs(dp[b]));
  }
  cout << fixed << setprecision(12) << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int n, m;
  cin >> n >> m;
  vector<Int> a(n), p(n);
  for (Int i = 0; i < n; i++)
    cin >> a[i];
  for (Int i = 0; i < n; i++)
    cin >> p[i];
  vector<Int> dp(1 << n, 0);
  for (Int b = 0; b < (1 << n); b++) {
    Int t = 1;
    for (Int i = 0; i < n; i++) {
      Int k = a[i] / __gcd(t, a[i]);
      if ((b >> i) & 1)
        t = (t <= m / k ? t * k : m + 1);
    }
    dp[b] = m / t;
  }

  for (Int i = 0; i < n; i++)
    for (Int b = 0; b < (1 << n); b++)
      if ((b >> i) & 1)
        dp[b] -= dp[b ^ (1 << i)];

  double ans = 0;
  for (Int b = 0; b < (1 << n); b++) {
    double po = 1;
    for (Int i = 0; i < n; i++)
      if ((b >> i) & 1)
        po *= p[i] / 100.0;
      else
        po *= 1.0 - (p[i] / 100.0);
    ans += po * (m - abs(dp[b]));
  }
  cout << fixed << setprecision(12) << ans << endl;
  return 0;
}
Problem p01560 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <stdio.h>
#include <string.h>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, (b))

typedef unsigned long long ull;

double dp[1 << 20], b[1 << 20];
int c[1 << 20];

inline ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }

ull a[20];
int p[20];
int main() {
  int n;
  ull m;
  scanf("%d%llu", &n, &m);
  REP(i, 1 << n) b[i] = 0, c[i] = 0;
  REP(i, n) scanf("%llu", a + i);
  REP(i, n) scanf("%d", p + i);
  FOR(i, 1, 1 << n) REP(j, n) c[i] += (i >> j) & 1;
  int g = 1;
  FOR(k, 1, n + 1) {
    REP(i, 1 << n) dp[i] = 0;
    FOR(i, 1, 1 << n) {
      if (c[i] < k)
        continue;
      else {
        if (c[i] == k) {
          ull s = 1;
          REP(j, n)
          if ((1 << j) & i) {
            ull g = s > a[j] ? gcd(s, a[j]) : gcd(a[j], s);
            s /= g;
            if (s > m / a[j]) {
              s = 0;
              break;
            } else
              s *= a[j];
          }
          if (s)
            dp[i] = m / s;
        } else {
          REP(j, n)
          dp[i] += dp[i ^ (1 << j)];
          dp[i] /= c[i] - k;
        }
        b[i] += dp[i] * g;
      }
    }
    g = -g;
  }
  double s = 0;
  FOR(i, 1, 1 << n) {
    double e = 1;
    REP(j, n)
    if ((1 << j) & i)
      e *= 0.01 * p[j];
    else
      e *= 0.01 * (100 - p[j]);
    s += b[i] * e;
  }
  printf("%.8f\\n", s);
}

Error: MLE

Fixed submission:

#include <stdio.h>
#include <string.h>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, b) FOR(i, 0, (b))

typedef unsigned long long ull;

double dp[1 << 20][20];

inline ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }

ull a[20];
int p[20];
int main() {
  int n;
  ull m;
  scanf("%d%llu", &n, &m);
  REP(i, 1 << n)
  REP(j, n)
  dp[i][j] = 0;
  REP(i, n)
  scanf("%llu", a + i);
  REP(i, n)
  scanf("%d", p + i);
  FOR(i, 1, 1 << n) {
    int c = 0;
    REP(j, n) c += (i >> j) & 1;
    REP(j, n)
    REP(k, c - 1)
    dp[i][k] += dp[i ^ (1 << j)][k];
    REP(k, c - 1)
    dp[i][k] /= (c - 1 - k);
    ull s = 1;
    REP(j, n)
    if ((1 << j) & i) {
      ull g = s > a[j] ? gcd(s, a[j]) : gcd(a[j], s);
      s /= g;
      if (s > m / a[j]) {
        s = 0;
        break;
      } else
        s *= a[j];
    }
    if (s)
      dp[i][c - 1] = m / s;
  }
  double s = 0;
  FOR(i, 1, 1 << n) {
    double e = 1;
    REP(j, n)
    if ((1 << j) & i)
      e *= 0.01 * p[j];
    else
      e *= 0.01 * (100 - p[j]);
    double f = 0;
    int k = 1;
    REP(j, n) {
      f += dp[i][j] * k;
      k = -k;
    }
    s += f * e;
  }
  printf("%.8f\\n", s);
}