結果

問題 No.2578 Jewelry Store
ユーザー noshi91noshi91
提出日時 2023-12-06 23:02:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 629 ms / 3,500 ms
コード長 8,192 bytes
コンパイル時間 2,559 ms
コンパイル使用メモリ 217,800 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-27 01:50:57
合計ジャッジ時間 9,529 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 6 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 15 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 6 ms
5,376 KB
testcase_07 AC 15 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 14 ms
5,376 KB
testcase_10 AC 26 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 25 ms
5,376 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 8 ms
5,376 KB
testcase_20 AC 14 ms
5,376 KB
testcase_21 AC 15 ms
5,376 KB
testcase_22 AC 7 ms
5,376 KB
testcase_23 AC 10 ms
5,376 KB
testcase_24 AC 6 ms
5,376 KB
testcase_25 AC 8 ms
5,376 KB
testcase_26 AC 6 ms
5,376 KB
testcase_27 AC 127 ms
5,376 KB
testcase_28 AC 187 ms
5,376 KB
testcase_29 AC 193 ms
5,376 KB
testcase_30 AC 220 ms
5,376 KB
testcase_31 AC 126 ms
5,376 KB
testcase_32 AC 90 ms
5,376 KB
testcase_33 AC 114 ms
5,376 KB
testcase_34 AC 149 ms
5,376 KB
testcase_35 AC 38 ms
5,376 KB
testcase_36 AC 112 ms
5,376 KB
testcase_37 AC 82 ms
5,376 KB
testcase_38 AC 94 ms
5,376 KB
testcase_39 AC 88 ms
5,376 KB
testcase_40 AC 90 ms
5,376 KB
testcase_41 AC 82 ms
5,376 KB
testcase_42 AC 78 ms
5,376 KB
testcase_43 AC 111 ms
5,376 KB
testcase_44 AC 75 ms
5,376 KB
testcase_45 AC 93 ms
5,376 KB
testcase_46 AC 84 ms
5,376 KB
testcase_47 AC 331 ms
5,376 KB
testcase_48 AC 201 ms
5,376 KB
testcase_49 AC 629 ms
5,376 KB
testcase_50 AC 563 ms
5,376 KB
testcase_51 AC 87 ms
5,376 KB
testcase_52 AC 198 ms
5,376 KB
testcase_53 AC 42 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define NDEBUG
#pragma warning(disable : 4146)

#include <bits/stdc++.h>

namespace n91 {

using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
using f64 = double;

struct rep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { ++i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr rep(const usize f, const usize l) noexcept
      : f(std::min(f, l)), l(l) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
struct revrep {
  struct itr {
    usize i;
    constexpr itr(const usize i) noexcept : i(i) {}
    void operator++() noexcept { --i; }
    constexpr usize operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  const itr f, l;
  constexpr revrep(const usize f, const usize l) noexcept
      : f(l - 1), l(std::min(f, l) - 1) {}
  constexpr auto begin() const noexcept { return f; }
  constexpr auto end() const noexcept { return l; }
};
template <class T> auto md_vec(const usize n, const T &value) {
  return std::vector<T>(n, value);
}
template <class... Args> auto md_vec(const usize n, Args... args) {
  return std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> constexpr T difference(const T &a, const T &b) noexcept {
  return a < b ? b - a : a - b;
}
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class F> class rec_lambda {
  F f;

public:
  rec_lambda(F &&f_) : f(std::forward<F>(f_)) {}
  template <class... Args> auto operator()(Args &&... args) const {
    return f(*this, std::forward<Args>(args)...);
  }
};
template <class T> T scan() {
  T ret;
  std::cin >> ret;
  return ret;
}
constexpr char eoln = '\n';

i64 floor_div(const i64 n, const i64 d) {
  assert(d != 0);
  return n / d - static_cast<i64>((n ^ d) < 0 && n % d != 0);
}

i64 ceil_div(const i64 n, const i64 d) {
  assert(d != 0);
  return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0);
}

#ifdef N91_LOCAL
#define OJ_LOCAL(a, b) b
#else
#define OJ_LOCAL(a, b) a
#endif

} // namespace n91

// https://ei1333.github.io/library/math/number-theory/fast-prime-factorization.hpp
namespace ei1333 {

using namespace std;

namespace FastPrimeFactorization {

template <typename word, typename dword, typename sword> struct UnsafeMod {
  UnsafeMod() : x(0) {}

  UnsafeMod(word _x) : x(init(_x)) {}

  bool operator==(const UnsafeMod &rhs) const { return x == rhs.x; }

  bool operator!=(const UnsafeMod &rhs) const { return x != rhs.x; }

  UnsafeMod &operator+=(const UnsafeMod &rhs) {
    if ((x += rhs.x) >= mod)
      x -= mod;
    return *this;
  }

  UnsafeMod &operator-=(const UnsafeMod &rhs) {
    if (sword(x -= rhs.x) < 0)
      x += mod;
    return *this;
  }

  UnsafeMod &operator*=(const UnsafeMod &rhs) {
    x = reduce(dword(x) * rhs.x);
    return *this;
  }

  UnsafeMod operator+(const UnsafeMod &rhs) const {
    return UnsafeMod(*this) += rhs;
  }

  UnsafeMod operator-(const UnsafeMod &rhs) const {
    return UnsafeMod(*this) -= rhs;
  }

  UnsafeMod operator*(const UnsafeMod &rhs) const {
    return UnsafeMod(*this) *= rhs;
  }

  UnsafeMod pow(uint64_t e) const {
    UnsafeMod ret(1);
    for (UnsafeMod base = *this; e; e >>= 1, base *= base) {
      if (e & 1)
        ret *= base;
    }
    return ret;
  }

  word get() const { return reduce(x); }

  static constexpr int word_bits = sizeof(word) * 8;

  static word modulus() { return mod; }

  static word init(word w) { return reduce(dword(w) * r2); }

  static void set_mod(word m) {
    mod = m;
    inv = mul_inv(mod);
    r2 = -dword(mod) % mod;
  }

  static word reduce(dword x) {
    word y =
        word(x >> word_bits) - word((dword(word(x) * inv) * mod) >> word_bits);
    return sword(y) < 0 ? y + mod : y;
  }

  static word mul_inv(word n, int e = 6, word x = 1) {
    return !e ? x : mul_inv(n, e - 1, x * (2 - x * n));
  }

  static word mod, inv, r2;

  word x;
};

using uint128_t = __uint128_t;

using Mod64 = UnsafeMod<uint64_t, uint128_t, int64_t>;
template <> uint64_t Mod64::mod = 0;
template <> uint64_t Mod64::inv = 0;
template <> uint64_t Mod64::r2 = 0;

using Mod32 = UnsafeMod<uint32_t, uint64_t, int32_t>;
template <> uint32_t Mod32::mod = 0;
template <> uint32_t Mod32::inv = 0;
template <> uint32_t Mod32::r2 = 0;

bool miller_rabin_primality_test_uint64(uint64_t n) {
  Mod64::set_mod(n);
  uint64_t d = n - 1;
  while (d % 2 == 0)
    d /= 2;
  Mod64 e{1}, rev{n - 1};
  // http://miller-rabin.appspot.com/  < 2^64
  for (uint64_t a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
    if (n <= a)
      break;
    uint64_t t = d;
    Mod64 y = Mod64(a).pow(t);
    while (t != n - 1 && y != e && y != rev) {
      y *= y;
      t *= 2;
    }
    if (y != rev && t % 2 == 0)
      return false;
  }
  return true;
}

bool miller_rabin_primality_test_uint32(uint32_t n) {
  Mod32::set_mod(n);
  uint32_t d = n - 1;
  while (d % 2 == 0)
    d /= 2;
  Mod32 e{1}, rev{n - 1};
  for (uint32_t a : {2, 7, 61}) {
    if (n <= a)
      break;
    uint32_t t = d;
    Mod32 y = Mod32(a).pow(t);
    while (t != n - 1 && y != e && y != rev) {
      y *= y;
      t *= 2;
    }
    if (y != rev && t % 2 == 0)
      return false;
  }
  return true;
}

bool is_prime(uint64_t n) {
  if (n == 2)
    return true;
  if (n == 1 || n % 2 == 0)
    return false;
  if (n < uint64_t(1) << 31)
    return miller_rabin_primality_test_uint32(n);
  return miller_rabin_primality_test_uint64(n);
}

uint64_t pollard_rho(uint64_t n) {
  if (is_prime(n))
    return n;
  if (n % 2 == 0)
    return 2;
  Mod64::set_mod(n);
  uint64_t d;
  Mod64 one{1};
  for (Mod64 c{one};; c += one) {
    Mod64 x{2}, y{2};
    do {
      x = x * x + c;
      y = y * y + c;
      y = y * y + c;
      d = gcd((x - y).get(), n);
    } while (d == 1);
    if (d < n)
      return d;
  }
  assert(0);
}

vector<uint64_t> prime_factor(uint64_t n) {
  if (n <= 1)
    return {};
  uint64_t p = pollard_rho(n);
  if (p == n)
    return {p};
  auto l = prime_factor(p);
  auto r = prime_factor(n / p);
  copy(begin(r), end(r), back_inserter(l));
  return l;
}
}; // namespace FastPrimeFactorization

} // namespace ei1333

#include <vector>

template <class F, class T>
void bitwise_transform(const F f, std::vector<T> &a) {
  const int n = a.size();
  for (int w = 1; w < n; w *= 2) {
    for (int k = 0; k < n; k += w * 2) {
      for (int i = 0; i < w; i++) {
        f(a[k + i], a[k + w + i]);
      }
    }
  }
}

#include <atcoder/modint>
using mint = atcoder::modint998244353;

namespace n91 {

void main_() {
  const usize T = scan<usize>();
  const u64 m = scan<u64>();
  std::vector<u64> ps;
  {
    auto f = ei1333::FastPrimeFactorization::prime_factor(m);
    std::set<u64> s;
    for (auto p : f)
      s.insert(p);
    ps.assign(s.begin(), s.end());
  }

  for (const usize loop : rep(0, T)) {
    const usize n = scan<usize>();
    mint B = scan<u32>();
    const mint C = scan<u32>();
    const mint D = scan<u32>();
    std::vector<mint> v(1 << ps.size(), 1);
    for (const usize i : rep(0, n)) {
      u64 A = scan<u64>();
      if (m % A == 0) {
        A = m / A;
        usize j = 0;
        for (const usize k : rep(0, ps.size())) {
          if (A % ps[k] == 0)
            j |= 1 << k;
        }
        v[j] *= B + 1;
      }
      B = C * B + D;
    }
    bitwise_transform([](auto &l, auto &r) { l *= r; }, v);
    mint ans = 0;
    for (const usize j : rep(0, v.size())) {
      ans += (__builtin_parity(j) ? -1 : 1) * v[j];
    }
    if (m == 1)
      ans -= 1;
    std::cout << ans.val() << eoln;
  }
}

} // namespace n91

int main() {
  //*
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  //*/
  std::cout << std::fixed << std::setprecision(20);
  n91::main_();
  return 0;
}
0