結果

問題 No.2578 Jewelry Store
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-12-06 00:17:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,710 bytes
コンパイル時間 1,358 ms
コンパイル使用メモリ 129,388 KB
実行使用メモリ 404,524 KB
最終ジャッジ日時 2024-09-27 00:40:50
合計ジャッジ時間 7,654 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 151 ms
5,376 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
権限があれば一括ダウンロードができます

ソースコード

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) {
        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