結果

問題 No.2578 Jewelry Store
ユーザー suisensuisen
提出日時 2023-01-11 17:47:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,502 bytes
コンパイル時間 1,433 ms
コンパイル使用メモリ 117,704 KB
実行使用メモリ 814,176 KB
最終ジャッジ日時 2023-12-05 23:31:23
合計ジャッジ時間 3,795 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
6,548 KB
testcase_02 MLE -
testcase_03 -- -
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 <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 (__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;

        std::vector<uint64_t> a(n);
        for (auto& e : a) std::cin >> e;

        std::vector<uint32_t> w(n);
        for (auto& e : w) std::cin >> e;

        std::cout << solve(n, m, pf, a, w).val() << '\n';
    }

    return 0;
}
0