結果

問題 No.2578 Jewelry Store
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-12-06 00:23:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 763 ms / 3,500 ms
コード長 7,712 bytes
コンパイル時間 1,492 ms
コンパイル使用メモリ 130,028 KB
実行使用メモリ 9,752 KB
最終ジャッジ日時 2024-09-27 00:42:13
合計ジャッジ時間 8,839 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 6 ms
6,940 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 17 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 7 ms
6,944 KB
testcase_07 AC 17 ms
6,944 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 19 ms
6,944 KB
testcase_10 AC 34 ms
6,944 KB
testcase_11 AC 3 ms
6,940 KB
testcase_12 AC 5 ms
6,944 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 33 ms
6,944 KB
testcase_17 AC 18 ms
6,940 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 10 ms
6,944 KB
testcase_20 AC 18 ms
6,944 KB
testcase_21 AC 17 ms
6,940 KB
testcase_22 AC 7 ms
6,940 KB
testcase_23 AC 11 ms
6,944 KB
testcase_24 AC 6 ms
6,940 KB
testcase_25 AC 8 ms
6,944 KB
testcase_26 AC 8 ms
6,944 KB
testcase_27 AC 148 ms
6,940 KB
testcase_28 AC 186 ms
6,940 KB
testcase_29 AC 182 ms
6,944 KB
testcase_30 AC 211 ms
6,940 KB
testcase_31 AC 142 ms
6,944 KB
testcase_32 AC 95 ms
6,940 KB
testcase_33 AC 115 ms
6,940 KB
testcase_34 AC 141 ms
6,940 KB
testcase_35 AC 36 ms
6,940 KB
testcase_36 AC 124 ms
6,940 KB
testcase_37 AC 91 ms
6,940 KB
testcase_38 AC 95 ms
6,944 KB
testcase_39 AC 92 ms
6,944 KB
testcase_40 AC 94 ms
6,940 KB
testcase_41 AC 94 ms
6,944 KB
testcase_42 AC 90 ms
6,940 KB
testcase_43 AC 103 ms
6,940 KB
testcase_44 AC 86 ms
6,940 KB
testcase_45 AC 103 ms
6,940 KB
testcase_46 AC 91 ms
6,944 KB
testcase_47 AC 351 ms
6,944 KB
testcase_48 AC 188 ms
9,752 KB
testcase_49 AC 763 ms
6,944 KB
testcase_50 AC 691 ms
6,940 KB
testcase_51 AC 94 ms
6,940 KB
testcase_52 AC 220 ms
6,940 KB
testcase_53 AC 59 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;


template<class T> vector<T> merge(const vector<T> &a, const vector<T> &b) {
  vector<T> c(a.size() + b.size());
  std::merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());
  return c;
}

template<class T> T power(T a, Int e, T m) {
  T b = 1;
  for (; e; e >>= 1) {
    if (e & 1) b = (b * a) % m;
    a = (a * a) % m;
  }
  return b;
}

Int gcd(Int a, Int b) {
  if (a < 0) a = -a;
  if (b < 0) b = -b;
  if (a == 0) return b;
  if (b == 0) return a;
  const int s = __builtin_ctzll(a | b);
  a >>= __builtin_ctzll(a);
  do {
    b >>= __builtin_ctzll(b);
    if (a > b) std::swap(a, b);
    b -= a;
  } while (b);
  return a << s;
}

// Checks if n is a prime using Miller-Rabin test
bool isPrime(Int n) {
  if (n <= 1 || n % 2 == 0) return (n == 2);
  const int s = __builtin_ctzll(n - 1);
  const Int d = (n - 1) >> s;
  // http://miller-rabin.appspot.com/
  for (const Int base : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
    __int128 a = base % n;
    if (a == 0) continue;
    a = power<__int128>(a, d, n);
    if (a == 1 || a == n - 1) continue;
    bool ok = false;
    for (int i = 0; i < s - 1; ++i) {
      a = (a * a) % n;
      if (a == n - 1) {
        ok = true;
        break;
      }
    }
    if (!ok) return false;
  }
  return true;
}

// Factorize n using Pollard's rho algorithm
vector<Int> factorize(Int n) {
  static constexpr int BLOCK = 256;
  if (n <= 1) return {};
  if (isPrime(n)) return {n};
  if (n % 2 == 0) return merge({2}, factorize(n / 2));
  for (Int c = 2; ; ++c) {
    Int x, y = 2, y0, z = 1, d = 1;
    for (int l = 1; d == 1; l <<= 1) {
      x = y;
      for (int i = 0; i < l; ++i) y = (static_cast<__int128>(y) * y + c) % n;
      for (int i = 0; i < l; i += BLOCK) {
        y0 = y;
        for (int j = 0; j < BLOCK && j < l - i; ++j) {
          y = (static_cast<__int128>(y) * y + c) % n;
          z = (static_cast<__int128>(z) * (y - x)) % n;
        }
        if ((d = gcd(z, n)) != 1) break;
      }
    }
    if (d == n) {
      for (y = y0; ; ) {
        y = (static_cast<__int128>(y) * y + c) % n;
        if ((d = gcd(y - x, n)) != 1) break;
      }
    }
    if (d != n) return merge(factorize(d), factorize(n / d));
  }
}


int T;
Int M;
int N;
Mint B, C, D;
vector<Int> A;

int main() {
  for (; ~scanf("%d%lld", &T, &M); ) {
    vector<Int> ps;
    vector<int> es;
    {
      const auto res = factorize(M);
      for (int i = 0, j = 0; i < (int)res.size(); i = j) {
        for (; j < (int)res.size() && res[i] == res[j]; ++j) {}
        ps.push_back(res[i]);
        es.push_back(j - i);
      }
    }
    const int len = ps.size();
// cerr<<"ps = "<<ps<<", es = "<<es<<endl;
    
    vector<Int> prods(1 << len);
    prods[0] = 1;
    for (int k = 0; k < len; ++k) {
      for (int h = 0; h < 1 << k; ++h) {
        prods[h | 1 << k] = prods[h] * ps[k];
      }
    }
    map<Int, int> toH;
    for (int h = 0; h < 1 << len; ++h) {
      toH[prods[h]] = h;
    }
    
    vector<Mint> fs(1 << len);
    for (int t = 0; t < T; ++t) {
      scanf("%d%u%u%u", &N, &B.x, &C.x, &D.x);
      A.resize(N);
      for (int i = 0; i < N; ++i) {
        scanf("%lld", &A[i]);
      }
      
      fill(fs.begin(), fs.end(), 1);
      Mint w = B;
      for (int i = 0; i < N; ++i) {
        if (M % A[i] == 0) {
          const int h = toH[__gcd(M / A[i], prods.back())];
          fs[h] *= (1 + w);
        }
        w = C * w + D;
      }
// cerr<<"fs = "<<fs<<endl;
      for (int k = 0; k < len; ++k) {
        // for (int h = 0; h < 1 << len; ++h) if (!(h & 1 << k)) {
        for (int g = 0; g < 1 << len; g += (1 << (k + 1))) for (int h = g; h < g + (1 << k); ++h) {
          fs[h] *= fs[h | 1 << k];
        }
      }
// cerr<<"fs = "<<fs<<endl;
      Mint ans = 0;
      for (int h = 0; h < 1 << len; ++h) {
        ans += (__builtin_parity(h)?-1:+1) * fs[h];
      }
      if (M == 1) {
        ans -= 1;
      }
      printf("%u\n", ans.x);
    }
  }
  return 0;
}
0