結果

問題 No.2578 Jewelry Store
ユーザー suisensuisen
提出日時 2023-01-11 20:27:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,755 bytes
コンパイル時間 1,553 ms
コンパイル使用メモリ 118,796 KB
実行使用メモリ 22,716 KB
最終ジャッジ日時 2024-09-27 00:34:13
合計ジャッジ時間 12,792 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,268 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 6 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 11 ms
5,376 KB
testcase_05 WA -
testcase_06 AC 7 ms
5,376 KB
testcase_07 AC 12 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 12 ms
5,376 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 11 ms
5,376 KB
testcase_21 AC 12 ms
5,376 KB
testcase_22 AC 7 ms
5,376 KB
testcase_23 WA -
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 8 ms
5,376 KB
testcase_26 AC 7 ms
5,376 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 306 ms
5,376 KB
testcase_31 WA -
testcase_32 AC 105 ms
13,936 KB
testcase_33 WA -
testcase_34 AC 169 ms
11,764 KB
testcase_35 AC 45 ms
7,820 KB
testcase_36 AC 136 ms
16,904 KB
testcase_37 AC 97 ms
5,376 KB
testcase_38 AC 112 ms
5,376 KB
testcase_39 WA -
testcase_40 AC 113 ms
5,376 KB
testcase_41 WA -
testcase_42 AC 96 ms
5,376 KB
testcase_43 AC 127 ms
5,376 KB
testcase_44 WA -
testcase_45 AC 111 ms
5,376 KB
testcase_46 WA -
testcase_47 AC 504 ms
5,376 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 TLE -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
権限があれば一括ダウンロードができます

ソースコード

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 ((k - __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;
}
0