結果

問題 No.2075 GCD Subsequence
ユーザー hiro71687khiro71687k
提出日時 2023-03-14 12:16:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,845 ms / 4,000 ms
コード長 3,325 bytes
コンパイル時間 6,492 ms
コンパイル使用メモリ 266,144 KB
実行使用メモリ 28,416 KB
最終ジャッジ日時 2023-10-18 11:35:20
合計ジャッジ時間 33,066 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 135 ms
26,844 KB
testcase_01 AC 134 ms
26,844 KB
testcase_02 AC 136 ms
26,844 KB
testcase_03 AC 133 ms
26,844 KB
testcase_04 AC 127 ms
26,844 KB
testcase_05 AC 130 ms
26,844 KB
testcase_06 AC 127 ms
26,844 KB
testcase_07 AC 133 ms
26,844 KB
testcase_08 AC 344 ms
27,820 KB
testcase_09 AC 479 ms
28,416 KB
testcase_10 AC 351 ms
27,820 KB
testcase_11 AC 432 ms
28,348 KB
testcase_12 AC 387 ms
28,084 KB
testcase_13 AC 305 ms
27,820 KB
testcase_14 AC 428 ms
28,084 KB
testcase_15 AC 327 ms
27,820 KB
testcase_16 AC 353 ms
27,820 KB
testcase_17 AC 468 ms
28,416 KB
testcase_18 AC 1,781 ms
28,416 KB
testcase_19 AC 1,782 ms
28,416 KB
testcase_20 AC 1,788 ms
28,416 KB
testcase_21 AC 1,786 ms
28,416 KB
testcase_22 AC 1,790 ms
28,416 KB
testcase_23 AC 1,779 ms
28,416 KB
testcase_24 AC 1,780 ms
28,416 KB
testcase_25 AC 1,780 ms
28,416 KB
testcase_26 AC 1,779 ms
28,416 KB
testcase_27 AC 1,783 ms
28,416 KB
testcase_28 AC 1,845 ms
28,416 KB
testcase_29 AC 147 ms
26,844 KB
testcase_30 AC 144 ms
26,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll inf=15555055;
ll mod=998244353;
struct Eratosthenes {
    // テーブル
    vector<bool> isprime;

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

    vector<ll>mobius;

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

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

            // p についての情報更新
            minfactor[p] = p;
            mobius[p]=-1;

            // p 以外の p の倍数から素数ラベルを剥奪
            for (ll 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];
            }
        }
    }

    // 高速素因数分解
    // pair (素因子, 指数) の vector を返す
    vector<pair<ll,ll>> factorize(ll n) {
        vector<pair<ll,ll>> res;
        while (n > 1) {
            ll p = minfactor[n];
            ll exp = 0;

            // n で割り切れる限り割る
            while (minfactor[n] == p) {
                n /= p;
                ++exp;
            }
            res.emplace_back(p, exp);
        }
        return res;
    }
    vector<ll>divisors(ll n){
        vector<ll>res({1});
        auto pf=factorize(n);
        for (auto p : pf)
        {
            ll s=(ll)res.size();
            for (ll i = 0; i < s; i++)
            {
                ll v=1;
                for (ll j = 0; j < p.second; j++)
                {
                    v*=p.first;
                    res.push_back(res[i]*v);
                }
                
            }
            
        }
        return res;
    }  
};
int main(){
    ll n;
    cin >> n;
    vector<ll>a(n);
    for (ll i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    vector<ll>memo(1000001,0);
    Eratosthenes er(1000001);
    ll ans=0;
    memo[1]=1;
    for (ll i = 0; i < n; i++)
    {
        if (a[i]==1)
        {
            ans+=1;
            ans%=mod;
            continue;
        }
        vector<ll>x=er.divisors(a[i]);
        ll now=0;
        for (ll j = 0; j < x.size(); j++)
        {
            if (x[j]==1)
            {
                now+=1;
                now%=mod;
                continue;
            }
            
            now+=(-er.mobius[x[j]]*memo[x[j]])%mod;
            now%=mod;
        }
        now%=mod;
        now+=mod;
        now%=mod;
        ans+=now;
        ans%=mod;
        for (ll j = 0; j < x.size(); j++)
        {
            if (x[j]!=1&&er.mobius[x[j]]!=0)
            {
                memo[x[j]]+=now;
                memo[x[j]]%=mod;
            }
        }
    }
    cout << ans << endl;
}
0