結果

問題 No.2578 Jewelry Store
ユーザー suisen
提出日時 2023-01-09 22:06:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,857 bytes
コンパイル時間 903 ms
コンパイル使用メモリ 83,408 KB
最終ジャッジ日時 2025-02-10 01:27:07
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 3 RE * 46 TLE * 4 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>

#include <atcoder/modint>

using mint = atcoder::modint998244353;

std::vector<int64_t> prime_factors(int64_t m) {
    std::vector<int64_t> pf;
    for (int64_t p = 2; p * p <= m; ++p) {
        if (m % p == 0) {
            do m /= p; while (m % p == 0);
            pf.push_back(p);
        }
    }
    if (m != 1) {
        pf.push_back(m);
    }
    return pf;
}

mint solve(int32_t n, int64_t m, std::vector<int64_t> pf, std::vector<int64_t> a, std::vector<int32_t> w) {
    {
        std::vector<int64_t> a2;
        std::vector<int32_t> w2;
        for (int32_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 int32_t k = pf.size();

    mint ans = 0;
    for (int32_t s = 0; s < 1 << k; ++s) {
        int64_t v = m;
        for (int32_t i = 0; i < k; ++i) if ((s >> i) & 1) {
            v /= pf[i];
        }
        mint add = 1;
        for (int32_t i = 0; i < n; ++i) {
            if (v % a[i] == 0) {
                add *= 1 + w[i];
            }
        }
        if ((k - __builtin_popcount(s)) & 1) {
            ans -= add;
        } else {
            ans += add;
        }
    }
    return ans;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int32_t t;
    int64_t m;
    std::cin >> t >> m;

    auto pf = prime_factors(m);

    for (int32_t testcase = 0; testcase < t; ++testcase) {
        int32_t n;
        std::cin >> n;

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

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

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

    return 0;
}
0