結果

問題 No.2211 Frequency Table of GCD
ユーザー boatmusclesboatmuscles
提出日時 2023-02-10 23:16:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 4,083 bytes
コンパイル時間 1,137 ms
コンパイル使用メモリ 114,276 KB
実行使用メモリ 8,068 KB
最終ジャッジ日時 2023-09-22 01:17:03
合計ジャッジ時間 5,400 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
4,640 KB
testcase_01 AC 9 ms
4,644 KB
testcase_02 AC 8 ms
4,704 KB
testcase_03 AC 35 ms
6,256 KB
testcase_04 AC 68 ms
6,484 KB
testcase_05 AC 95 ms
7,220 KB
testcase_06 AC 63 ms
6,576 KB
testcase_07 AC 95 ms
7,052 KB
testcase_08 AC 26 ms
5,136 KB
testcase_09 AC 21 ms
4,816 KB
testcase_10 AC 47 ms
6,020 KB
testcase_11 AC 36 ms
5,404 KB
testcase_12 AC 51 ms
5,932 KB
testcase_13 AC 59 ms
6,432 KB
testcase_14 AC 64 ms
6,464 KB
testcase_15 AC 51 ms
6,312 KB
testcase_16 AC 63 ms
6,528 KB
testcase_17 AC 88 ms
7,008 KB
testcase_18 AC 133 ms
7,804 KB
testcase_19 AC 133 ms
7,900 KB
testcase_20 AC 132 ms
7,912 KB
testcase_21 AC 133 ms
8,068 KB
testcase_22 AC 134 ms
7,788 KB
testcase_23 AC 37 ms
6,288 KB
testcase_24 AC 148 ms
7,868 KB
testcase_25 AC 29 ms
6,312 KB
testcase_26 AC 8 ms
4,604 KB
testcase_27 AC 116 ms
7,808 KB
testcase_28 AC 146 ms
7,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cassert>
#include<vector>
#include<iostream>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<functional>
#include<utility>
#include<cstring>
#include<numeric>
#include<algorithm>
#include<atcoder/math>
#include<atcoder/modint>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using Mint = modint998244353;
using mint = modint;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define rep2(i, a, b) for (int i = (int)a; i < (int)(b); ++i)
#define rrep2(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); --i)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int dx[] = {-1,0,1,0};
constexpr int dy[] = {0,-1,0,1};
constexpr int MAX_N = 100000;
//ダイクストラ法:makedijkstra
//エラトステネスの篩:makesieve
//テストケースが複数の場合:multest
struct Eratosthenes {
    // テーブル
    vector<bool> isprime;

    // 整数 n を割り切る最小の素数
    vector<int> minfactor;

    // メビウス関数値
    vector<int> mobius;

    // コンストラクタで篩を回す
    Eratosthenes(int N) : isprime(N+1, true),
                          minfactor(N+1, -1),
                          mobius(N+1, 1) {
        // 1 は予めふるい落としておく
        isprime[1] = false;
        minfactor[1] = 1;

        // 篩
        for (int p = 2; p <= N; ++p) {
            // すでに合成数であるものはスキップする
            if (!isprime[p]) continue;

            // p についての情報更新
            minfactor[p] = p;
            mobius[p] = -1;
            // p 以外の p の倍数から素数ラベルを剥奪
            for (int q = p * 2; q <= N; q += p) {
                // q は合成数なのでふるい落とす
                isprime[q] = false;

                // q は p で割り切れる旨を更新
                if (minfactor[q] == -1) minfactor[q] = p;
                if ((q / p) % p == 0) mobius[q] = 0;
                else mobius[q] = -mobius[q];
            }
        }
    }

    // 高速素因数分解、高速約数列挙は省略
    vector<pair<int,int>> factorize(int n) {
        vector<pair<int,int>> res;
        while (n > 1) {
            int p = minfactor[n];
            int exp = 0;

            // n で割り切れる限り割る
            while (minfactor[n] == p) {
                n /= p;
                ++exp;
            }
            res.emplace_back(p, exp);
        }
        return res;
    }
    vector<int> divisors(int n) {
        vector<int> res({1});

        // n を素因数分解 (メンバ関数使用)
        auto pf = factorize(n);

        // 約数列挙
        for (auto p : pf) {
            int s = (int)res.size();
            for (int i = 0; i < s; ++i) {
                int v = 1;
                for (int j = 0; j < p.second; ++j) {
                    v *= p.first;
                    res.push_back(res[i] * v);
                }
            }
        }
        return res;
    }    
};
int main(){
    //cin.tie(nullptr);
    //std::ios_base::sync_with_stdio(false);
    int N, M;
    scanf("%d %d", &N, &M);
    int A[N];
    for(int i = 0; i < N; ++i) scanf("%d", A + i);
    Eratosthenes sieve(200000);
    int multiple[M+1];
    memset(multiple, 0, sizeof(multiple));
    rep(i, N){
        auto div = sieve.divisors(A[i]);
        for(auto d : div){
            if(d > M) continue;
            multiple[d]++;
        }
    }
    Mint two[N+1];
    two[0] = 1;
    rep2(i, 1, N+1) two[i] = two[i-1]*2;
    Mint answer[M+1];
    rep2(i, 1, M+1) rep2(j, 1, M/i + 1) answer[i] += (two[multiple[i*j]] - 1)*sieve.mobius[j];
    rep(i, M) printf("%u\n", answer[i+1].val());
    return 0;
}
0