結果

問題 No.2578 Jewelry Store
ユーザー suisen
提出日時 2023-01-13 17:57:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 5,749 bytes
コンパイル時間 1,581 ms
コンパイル使用メモリ 117,944 KB
最終ジャッジ日時 2025-02-10 01:54:39
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 53 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
#include <atcoder/modint>
using mint = atcoder::modint998244353;
namespace details {
template <typename T>
constexpr int floor_log2(T n) {
int i = 0;
while (n) n >>= 1, ++i;
return i - 1;
}
constexpr bool miller_rabin(uint64_t n) {
if (n <= 1) return false;
uint64_t d = (n - 1) >> __builtin_ctzll(n - 1);
if (n == 2 or n == 3 or n == 5 or n == 7) {
return true;
}
if (n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n % 7 == 0) {
return false;
}
for (uint64_t p : { 2U, 325U, 9375U, 28178U, 450775U, 9780504U, 1795265022U }) {
p %= n;
if (p == 0) {
continue;
}
uint64_t y = 1;
for (uint64_t d2 = d; d2; d2 >>= 1) {
if (d2 & 1) {
y = __uint128_t(y) * p % n;
}
p = __uint128_t(p) * p % n;
}
if (y == 1) {
continue;
}
for (uint64_t t = d; y != n - 1; t <<= 1) {
y = __uint128_t(y) * y % n;
if (y == 1 or t == n - 1) {
return false;
}
}
}
return true;
}
uint64_t pollard_rho(uint64_t n) {
const uint64_t m = uint64_t(1) << (floor_log2(n) / 5);
static std::mt19937_64 rng{ std::random_device{}() };
std::uniform_int_distribution<uint64_t> dist(0, n - 1);
while (true) {
uint64_t c = dist(rng);
auto f = [&](uint64_t x) -> uint64_t {
return (__uint128_t(x) * x + c) % n;
};
uint64_t x, y = 2, ys, q = 1, g = 1;
for (uint64_t r = 1; g == 1; r <<= 1) {
x = y;
for (uint64_t i = 0; i < r; ++i) {
y = f(y);
}
for (uint64_t k = 0; k < r and g == 1; k += m) {
ys = y;
for (uint64_t i = 0; i < std::min(m, r - k); ++i) {
y = f(y), q = __uint128_t(q) * (x > y ? x - y : y - x) % n;
}
g = std::gcd(q, n);
}
}
if (g == n) {
g = 1;
while (g == 1) {
ys = f(ys), g = std::gcd(x > ys ? x - ys : ys - x, n);
}
}
if (g < n) {
if (miller_rabin(g)) {
return g;
}
if (uint64_t d = n / g; miller_rabin(d)) {
return d;
}
return pollard_rho(g);
}
}
}
}
std::vector<std::pair<uint64_t, int>> factorize(uint64_t n) {
std::vector<std::pair<uint64_t, int>> res;
if ((n & 1) == 0) {
int q = 0;
do ++q, n >>= 1; while ((n & 1) == 0);
res.emplace_back(2, q);
}
for (uint64_t p = 3; p * p <= n; p += 2) {
if (p >= 101 and n >= 1 << 20) {
while (n > 1) {
if (details::miller_rabin(n)) {
res.emplace_back(std::exchange(n, 1), 1);
} else {
p = details::pollard_rho(n);
int q = 0;
do ++q, n /= p; while (n % p == 0);
res.emplace_back(p, q);
}
}
break;
}
if (n % p == 0) {
int q = 0;
do ++q, n /= p; while (n % p == 0);
res.emplace_back(p, q);
}
}
if (n > 1) {
res.emplace_back(n, 1);
}
return res;
}
std::vector<uint64_t> prime_factors(uint64_t m) {
std::vector<uint64_t> res;
for (const auto &prime_power : factorize(m)) {
res.push_back(prime_power.first);
}
return res;
}
mint solve(uint32_t n, uint64_t m, std::vector<uint64_t> pf, std::vector<uint64_t> a, std::vector<uint32_t> w) {
{
std::vector<uint64_t> a2;
std::vector<uint32_t> w2;
for (uint32_t i = 0; i < n; ++i) if (m % a[i] == 0) {
a2.push_back(a[i]);
w2.push_back(w[i]);
}
a.swap(a2);
w.swap(w2);
n = a.size();
}
// A _ i | m for all i
const uint32_t k = pf.size();
std::vector<mint> prd(1 << k, 1);
for (uint32_t i = 0; i < n; ++i) {
uint32_t t = 0;
for (uint32_t j = 0; j < k; ++j) {
if ((m / pf[j]) % a[i] == 0) t |= 1 << j;
}
prd[t] *= 1 + w[i];
}
for (uint32_t t = 0; t < prd.size(); ++t) {
mint pw = prd[t];
if (pw == 1) continue;
for (int s = t; s;) {
s = (s - 1) & t;
prd[s] *= pw;
}
}
mint ans = 0;
for (uint32_t s = 0; s < prd.size(); ++s) {
if (__builtin_popcount(s) & 1) {
ans -= prd[s];
} else {
ans += prd[s];
}
}
return ans - (m == 1);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
uint32_t t;
uint64_t m;
std::cin >> t >> m;
auto pf = prime_factors(m);
for (uint32_t testcase = 0; testcase < t; ++testcase) {
uint32_t n;
std::cin >> n;
uint32_t x0, c, d;
std::cin >> x0 >> c >> d;
std::vector<uint32_t> w(n);
w[0] = x0;
for (uint32_t i = 1; i < n; ++i) {
w[i] = (c * mint(w[i - 1]) + d).val();
}
std::vector<uint64_t> a(n);
for (auto &e : a) std::cin >> e;
std::cout << solve(n, m, pf, a, w).val() << '\n';
}
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0