結果

問題 No.2075 GCD Subsequence
ユーザー otoshigootoshigo
提出日時 2022-09-17 16:36:56
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 587 ms / 4,000 ms
コード長 2,727 bytes
コンパイル時間 2,091 ms
コンパイル使用メモリ 207,644 KB
実行使用メモリ 19,620 KB
最終ジャッジ日時 2023-08-23 17:49:30
合計ジャッジ時間 11,769 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
12,880 KB
testcase_01 AC 35 ms
13,296 KB
testcase_02 AC 35 ms
13,048 KB
testcase_03 AC 35 ms
13,056 KB
testcase_04 AC 36 ms
12,916 KB
testcase_05 AC 35 ms
12,864 KB
testcase_06 AC 35 ms
13,168 KB
testcase_07 AC 35 ms
13,076 KB
testcase_08 AC 128 ms
19,192 KB
testcase_09 AC 192 ms
19,516 KB
testcase_10 AC 138 ms
19,232 KB
testcase_11 AC 165 ms
19,456 KB
testcase_12 AC 147 ms
19,200 KB
testcase_13 AC 121 ms
19,188 KB
testcase_14 AC 167 ms
19,448 KB
testcase_15 AC 123 ms
19,256 KB
testcase_16 AC 129 ms
19,236 KB
testcase_17 AC 189 ms
19,512 KB
testcase_18 AC 567 ms
19,452 KB
testcase_19 AC 562 ms
19,620 KB
testcase_20 AC 564 ms
19,452 KB
testcase_21 AC 562 ms
19,500 KB
testcase_22 AC 562 ms
19,508 KB
testcase_23 AC 572 ms
19,496 KB
testcase_24 AC 561 ms
19,464 KB
testcase_25 AC 570 ms
19,544 KB
testcase_26 AC 568 ms
19,456 KB
testcase_27 AC 572 ms
19,456 KB
testcase_28 AC 587 ms
13,612 KB
testcase_29 AC 35 ms
12,920 KB
testcase_30 AC 34 ms
12,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for (int i = 0; i < n; i++)
#define rrep(i,n) for (int i = (n) - 1; i >= 0; i--)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

struct Eratosthenes{
    vector<bool> prime;
    vector<int> factor, mobius;
    Eratosthenes(int n) : prime(n + 1, true), factor(n + 1, -1), mobius(n + 1, 1){
        prime[1] = false;
        factor[1] = 1;
        for (int p = 2; p <= n; p++){
            if (!prime[p]){
                continue;
            }
            factor[p] = p;
            mobius[p] = -1;
            for (int q = 2 * p; q <= n; q += p){
                prime[q] = false;
                factor[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>> pf;
        while (n > 1){
            int p = factor[n], exp = 0;
            while (n % p == 0){
                n /= p;
                exp++;
            }
            pf.push_back(make_pair(p, exp));
        }
        return pf;
    }
    vector<int> divisors(int n){
        vector<int> div = {1};
        vector<pair<int, int>> pf = factorize(n);
        for (auto [p, e] : pf){
            int l = div.size();
            for (int i = 0; i < l; i++){
                int q = p;
                for (int j = 0; j < e; j++){
                    div.push_back(div[i] * q);
                    q *= p;
                }
            }
        }
        //sort(div.begin(), div.end());
        return div;
    }
    int euler(int n){
        int e = n;
        vector<pair<int, int>> pf = factorize(n);
        for (int i = 0; i < pf.size(); i++){
            e /= pf[i].first;
            e *= pf[i].first - 1;
        }
        return e;
    }
};

const int MOD = 998244353;
int n, A[200010];
ll S[1000010];

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cin >> n;
    ll s = 0;
    Eratosthenes er(1e6 + 1);
    rep(i, n){
        cin >> A[i];
        ll x = 1 + s;
        vector<int> div = er.divisors(A[i]);
        for (auto a : div){
            x -= er.mobius[a] * S[a];
            x %= MOD;
            if (x < 0){
                x += MOD;
            }
        }
        s += x;
        s %= MOD;
        for (auto a : div){
            S[a] += x;
            S[a] %= MOD;
        }
    }
    cout << s << "\n";
}
0