結果

問題 No.2211 Frequency Table of GCD
ユーザー jianglyjiangly
提出日時 2023-02-10 21:33:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 2,781 bytes
コンパイル時間 2,304 ms
コンパイル使用メモリ 203,204 KB
実行使用メモリ 5,420 KB
最終ジャッジ日時 2023-09-22 00:59:57
合計ジャッジ時間 5,117 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 20 ms
4,380 KB
testcase_04 AC 25 ms
4,380 KB
testcase_05 AC 33 ms
4,836 KB
testcase_06 AC 27 ms
4,380 KB
testcase_07 AC 35 ms
4,820 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 12 ms
4,424 KB
testcase_11 AC 10 ms
4,376 KB
testcase_12 AC 14 ms
4,532 KB
testcase_13 AC 23 ms
4,376 KB
testcase_14 AC 27 ms
4,380 KB
testcase_15 AC 23 ms
4,376 KB
testcase_16 AC 26 ms
4,380 KB
testcase_17 AC 32 ms
4,544 KB
testcase_18 AC 45 ms
5,384 KB
testcase_19 AC 45 ms
5,332 KB
testcase_20 AC 45 ms
5,408 KB
testcase_21 AC 45 ms
5,332 KB
testcase_22 AC 45 ms
5,328 KB
testcase_23 AC 22 ms
4,376 KB
testcase_24 AC 43 ms
5,376 KB
testcase_25 AC 15 ms
4,628 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 43 ms
5,376 KB
testcase_28 AC 43 ms
5,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;
template<class T>
T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a) {
        if (b % 2) {
            res *= a;
        }
    }
    return res;
}

template<int P>
struct MInt {
    int x;
    MInt() : x{} {}
    MInt(i64 x) : x{norm(x % P)} {}
    
    int norm(int x) const {
        if (x < 0) {
            x += P;
        }
        if (x >= P) {
            x -= P;
        }
        return x;
    }
    int val() const {
        return x;
    }
    MInt operator-() const {
        MInt res;
        res.x = norm(P - x);
        return res;
    }
    MInt inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    MInt &operator*=(const MInt &rhs) {
        x = 1LL * x * rhs.x % P;
        return *this;
    }
    MInt &operator+=(const MInt &rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    MInt &operator-=(const MInt &rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    MInt &operator/=(const MInt &rhs) {
        return *this *= rhs.inv();
    }
    friend MInt operator*(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend MInt operator+(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend MInt operator-(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend MInt operator/(const MInt &lhs, const MInt &rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend std::ostream &operator<<(std::ostream &os, const MInt &a) {
        return os << a.val();
    }
};

constexpr int P = 998244353;
using Z = MInt<P>;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m;
    std::cin >> n >> m;
    
    std::vector<int> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    
    std::vector<Z> pw(n + 1);
    pw[0] = 1;
    for (int i = 1; i <= n; i++) {
        pw[i] = pw[i - 1] * 2;
    }
    
    std::vector<Z> f(m + 1);
    for (int i = 0; i < n; i++) {
        f[a[i]] += 1;
    }
    
    for (int i = 1; i <= m; i++) {
        for (int j = 2 * i; j <= m; j += i) {
            f[i] += f[j];
        }
    }
    for (int i = 1; i <= m; i++) {
        f[i] = pw[f[i].val()] - 1;
    }
    for (int i = m; i >= 1; i--) {
        for (int j = 2 * i; j <= m; j += i) {
            f[i] -= f[j];
        }
    }
    
    for (int i = 1; i <= m; i++) {
        std::cout << f[i] << "\n";
    }
    
    return 0;
}
0