:heavy_check_mark: test/aoj_2446.test.cpp

Back to top page

Depends on

Code

#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2446"
#define ERROR 1e-7

#include <cstdint>
#include <cstdio>
#include <vector>

#include "algorithm/moebius_transform.cpp"
#include "utility/literals.cpp"
#include "integer/gcd.cpp"

bool lcm_overflow(intmax_t m, intmax_t n, intmax_t& res) {
  return __builtin_mul_overflow(m / gcd(m, n), n, &res);
}

intmax_t g(std::vector<intmax_t> const& a, size_t i, size_t m) {
  intmax_t d = 1;
  for (size_t j = 0; j < a.size(); ++j)
    if (i >> j & 1)
      if (lcm_overflow(d, a[j], d)) return 0;
  return m / d;
}

int main() {
  size_t n, m;
  scanf("%zu %zu", &n, &m);

  std::vector<intmax_t> a(n), p(n);
  for (auto& ai: a) scanf("%jd", &ai);
  for (auto& pi: p) scanf("%jd", &pi);

  std::vector<intmax_t> dp(1_zu << n, 0);
  for (size_t i = 1; i < dp.size(); ++i)
    dp[i] = g(a, i, m);

  moebius_transform(dp.begin(), dp.end());

  double res = 0;
  for (size_t i = 0; i < (1_zu << n); ++i) {
    double pi = 1;
    for (size_t j = 0; j < n; ++j)
      pi *= ((i >> j & 1)? p[j]: 100-p[j]) / 100.0;

    intmax_t vi = dp[i];
    if (!parity(i)) vi = -vi;
    res += pi * vi;
  }

  printf("%.12f\n", res);
}

#line 1 "test/aoj_2446.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2446"
#define ERROR 1e-7

#include <cstdint>
#include <cstdio>
#include <vector>

#line 1 "algorithm/moebius_transform.cpp"



/**
 * @brief 高速 Möbius 変換
 * @author えびちゃん
 */

#line 1 "integer/bit.cpp"



/** 
 * @brief ビット演算
 * @author えびちゃん
 */

// XXX integral promotion 関連の注意をあまりしていません

#include <climits>
#include <type_traits>

template <typename Tp>
constexpr auto countl_zero(Tp n)
  -> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
  using value_type = typename std::make_unsigned<Tp>::type;
  int bits = (sizeof(value_type) * CHAR_BIT);
  if (n == 0) return bits;
  int res = 0;
  for (int i = bits / 2; i > 0; i /= 2) {
    value_type mask = ((static_cast<value_type>(1) << i) - 1) << i;
    if (n & mask) n >>= i;
    else res += i;
  }
  return res;
}
template <typename Tp>
constexpr auto countl_one(Tp n)
  -> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
  using value_type = typename std::make_unsigned<Tp>::type;
  return countl_zero(static_cast<value_type>(~n));
}

template <typename Tp>
constexpr auto countr_zero(Tp n)
  -> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
  using value_type = typename std::make_unsigned<Tp>::type;
  int bits = (sizeof(value_type) * CHAR_BIT);
  if (n == 0) return bits;
  int res = 0;
  for (int i = bits / 2; i > 0; i /= 2) {
    value_type mask = ((static_cast<value_type>(1) << i) - 1);
    if (!(n & mask)) res += i, n >>= i;
  }
  return res;
}
template <typename Tp>
constexpr auto countr_one(Tp n)
  -> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
  using value_type = typename std::make_unsigned<Tp>::type;
  return countr_zero(static_cast<value_type>(~n));
}

constexpr unsigned long long half_mask[] = {
  0x5555555555555555uLL, 0x3333333333333333uLL, 0x0F0F0F0F0F0F0F0FuLL,
  0x00FF00FF00FF00FFuLL, 0x0000FFFF0000FFFFuLL, 0x00000000FFFFFFFFuLL
};

template <typename Tp>
constexpr auto popcount(Tp n)
  -> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{
  int bits = static_cast<int>((sizeof n) * CHAR_BIT);
  for (int i = 0, j = 1; j < bits; ++i, j *= 2) {
    if (j <= 8) n = (n & half_mask[i]) + ((n >> j) & half_mask[i]);
    else n += n >> j;
  }
  return n & 0xFF;
}

template <typename Tp>
constexpr auto parity(Tp n)
  -> typename std::enable_if<std::is_unsigned<Tp>::value, int>::type
{ return popcount(n) & 1; }


template <typename Tp>
int clz(Tp n) { return countl_zero(static_cast<typename std::make_unsigned<Tp>::type>(n)); }
template <typename Tp>
int ctz(Tp n) { return countr_zero(static_cast<typename std::make_unsigned<Tp>::type>(n)); }

template <typename Tp>
int ilog2(Tp n) {
  return (CHAR_BIT * sizeof(Tp) - 1) - clz(static_cast<typename std::make_unsigned<Tp>::type>(n));
}
template <typename Tp>
bool is_pow2(Tp n) { return (n > 0) && ((n & (n-1)) == 0); }
template <typename Tp>
Tp floor2(Tp n) { return is_pow2(n)? n: static_cast<Tp>(1) << ilog2(n); }
template <typename Tp>
Tp ceil2(Tp n) { return is_pow2(n)? n: static_cast<Tp>(2) << ilog2(n); }

template <typename Tp>
constexpr auto reverse(Tp n)
  -> typename std::enable_if<std::is_unsigned<Tp>::value, Tp>::type
{
  int bits = static_cast<int>((sizeof n) * CHAR_BIT);
  for (int i = 0, j = 1; j < bits; ++i, j *= 2) {
    n = ((n & half_mask[i]) << j) | ((n >> j) & half_mask[i]);
  }
  return n;
}


#line 1 "utility/literals.cpp"



/**
 * @brief ユーザ定義リテラル
 * @author えびちゃん
 */

#include <cstddef>
#line 11 "utility/literals.cpp"

constexpr intmax_t  operator ""_jd(unsigned long long n) { return n; }
constexpr uintmax_t operator ""_ju(unsigned long long n) { return n; }
constexpr size_t    operator ""_zu(unsigned long long n) { return n; }
constexpr ptrdiff_t operator ""_td(unsigned long long n) { return n; }

constexpr int8_t   operator ""_i8(unsigned long long n)  { return n; }
constexpr int16_t  operator ""_i16(unsigned long long n) { return n; }
constexpr int32_t  operator ""_i32(unsigned long long n) { return n; }
constexpr int64_t  operator ""_i64(unsigned long long n) { return n; }
constexpr uint8_t  operator ""_u8(unsigned long long n)  { return n; }
constexpr uint16_t operator ""_u16(unsigned long long n) { return n; }
constexpr uint32_t operator ""_u32(unsigned long long n) { return n; }
constexpr uint64_t operator ""_u64(unsigned long long n) { return n; }


#line 11 "algorithm/moebius_transform.cpp"

template <typename RandomIt>
void moebius_transform(RandomIt first, RandomIt last) {
  size_t n = ctz(last-first);
  for (size_t j = 0; j < n; ++j)
    for (size_t i = 0; i < (1_zu << n); ++i)
      if (i >> j & 1) first[i] -= first[i ^ (1_zu << j)];
}


#line 1 "integer/gcd.cpp"



/** 
 * @brief 最大公約数
 * @author えびちゃん
 */

#include <utility>

template <typename Tp>
Tp gcd(Tp m, Tp n) {
  while (n) std::swap(m %= n, n);
  return m;
}


#line 11 "test/aoj_2446.test.cpp"

bool lcm_overflow(intmax_t m, intmax_t n, intmax_t& res) {
  return __builtin_mul_overflow(m / gcd(m, n), n, &res);
}

intmax_t g(std::vector<intmax_t> const& a, size_t i, size_t m) {
  intmax_t d = 1;
  for (size_t j = 0; j < a.size(); ++j)
    if (i >> j & 1)
      if (lcm_overflow(d, a[j], d)) return 0;
  return m / d;
}

int main() {
  size_t n, m;
  scanf("%zu %zu", &n, &m);

  std::vector<intmax_t> a(n), p(n);
  for (auto& ai: a) scanf("%jd", &ai);
  for (auto& pi: p) scanf("%jd", &pi);

  std::vector<intmax_t> dp(1_zu << n, 0);
  for (size_t i = 1; i < dp.size(); ++i)
    dp[i] = g(a, i, m);

  moebius_transform(dp.begin(), dp.end());

  double res = 0;
  for (size_t i = 0; i < (1_zu << n); ++i) {
    double pi = 1;
    for (size_t j = 0; j < n; ++j)
      pi *= ((i >> j & 1)? p[j]: 100-p[j]) / 100.0;

    intmax_t vi = dp[i];
    if (!parity(i)) vi = -vi;
    res += pi * vi;
  }

  printf("%.12f\n", res);
}

Back to top page