結果

問題 No.2578 Jewelry Store
ユーザー suisensuisen
提出日時 2023-01-13 15:27:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 779 ms / 3,500 ms
コード長 5,652 bytes
コンパイル時間 1,681 ms
コンパイル使用メモリ 117,500 KB
実行使用メモリ 15,024 KB
最終ジャッジ日時 2024-09-27 00:35:15
合計ジャッジ時間 9,774 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 7 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 19 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 7 ms
5,376 KB
testcase_07 AC 19 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 18 ms
5,376 KB
testcase_10 AC 35 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
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 AC 4 ms
5,376 KB
testcase_16 AC 34 ms
5,376 KB
testcase_17 AC 19 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 11 ms
5,376 KB
testcase_20 AC 19 ms
5,376 KB
testcase_21 AC 19 ms
5,376 KB
testcase_22 AC 8 ms
5,376 KB
testcase_23 AC 12 ms
5,376 KB
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 AC 140 ms
5,376 KB
testcase_28 AC 208 ms
5,376 KB
testcase_29 AC 208 ms
5,376 KB
testcase_30 AC 249 ms
5,376 KB
testcase_31 AC 140 ms
5,376 KB
testcase_32 AC 105 ms
9,856 KB
testcase_33 AC 133 ms
6,912 KB
testcase_34 AC 169 ms
8,412 KB
testcase_35 AC 44 ms
6,016 KB
testcase_36 AC 134 ms
11,056 KB
testcase_37 AC 95 ms
5,376 KB
testcase_38 AC 111 ms
5,376 KB
testcase_39 AC 106 ms
5,376 KB
testcase_40 AC 114 ms
5,376 KB
testcase_41 AC 105 ms
5,376 KB
testcase_42 AC 95 ms
5,376 KB
testcase_43 AC 127 ms
5,376 KB
testcase_44 AC 87 ms
5,376 KB
testcase_45 AC 113 ms
5,376 KB
testcase_46 AC 102 ms
5,376 KB
testcase_47 AC 396 ms
5,376 KB
testcase_48 AC 215 ms
15,024 KB
testcase_49 AC 779 ms
5,376 KB
testcase_50 AC 756 ms
5,376 KB
testcase_51 AC 92 ms
5,376 KB
testcase_52 AC 229 ms
5,376 KB
testcase_53 AC 49 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

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) {
    const uint32_t k = pf.size();

    std::vector<mint> zeta(1 << k, 1);

    for (uint32_t i = 0; i < n; ++i) if (m % a[i] == 0) {
        uint32_t t = 0;
        for (uint32_t j = 0; j < k; ++j) {
            uint64_t p = pf[j];
            if ((m / p) % a[i] != 0) {
                t |= 1 << j;
            }
        }
        zeta[t] *= 1 + w[i];
    }

    // Zeta transform
    for (uint32_t block = 1; block < zeta.size(); block *= 2) {
        for (uint32_t offset = 0; offset < zeta.size(); offset += 2 * block) {
            for (uint32_t i = 0; i < block; ++i) {
                zeta[offset + block + i] *= zeta[offset + i];
            }
        }
    }

    mint ans = 0;
    for (uint32_t s = 0; s < zeta.size(); ++s) {
        if ((k - __builtin_popcount(s)) & 1) {
            ans -= zeta[s];
        } else {
            ans += zeta[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;

    std::vector<uint64_t> 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