結果

問題 No.2075 GCD Subsequence
ユーザー 👑 NachiaNachia
提出日時 2022-09-16 22:16:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 658 ms / 4,000 ms
コード長 4,906 bytes
コンパイル時間 1,286 ms
コンパイル使用メモリ 97,628 KB
実行使用メモリ 95,804 KB
最終ジャッジ日時 2023-08-23 15:55:33
合計ジャッジ時間 15,087 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 583 ms
92,052 KB
testcase_09 AC 658 ms
95,748 KB
testcase_10 AC 584 ms
92,324 KB
testcase_11 AC 625 ms
94,424 KB
testcase_12 AC 610 ms
93,440 KB
testcase_13 AC 576 ms
91,316 KB
testcase_14 AC 623 ms
94,224 KB
testcase_15 AC 577 ms
91,772 KB
testcase_16 AC 583 ms
92,108 KB
testcase_17 AC 649 ms
95,804 KB
testcase_18 AC 566 ms
87,048 KB
testcase_19 AC 568 ms
87,212 KB
testcase_20 AC 565 ms
87,340 KB
testcase_21 AC 571 ms
87,180 KB
testcase_22 AC 567 ms
87,040 KB
testcase_23 AC 573 ms
87,088 KB
testcase_24 AC 579 ms
87,240 KB
testcase_25 AC 573 ms
87,040 KB
testcase_26 AC 578 ms
87,088 KB
testcase_27 AC 578 ms
87,108 KB
testcase_28 AC 420 ms
63,472 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "Main.cpp"

#line 2 "nachia\\math\\prime-sieve-explicit.hpp"

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


namespace nachia{

namespace prime_sieve_explicit_internal{
    std::vector<bool> isprime = { false }; // a[x] := isprime(2x+1)

    void CalcIsPrime(int z){
        if((int)isprime.size() *2+1 < z+1){
            int new_z = isprime.size();
            while(new_z*2+1 < z+1) new_z *= 2;
            z = new_z-1;
            isprime.resize(z+1, true);
            for(int i=1; i*(i+1)*2<=z; i++) if(isprime[i]){
                for(int j=i*(i+1)*2; j<=z; j+=i*2+1) isprime[j] = false;
            }
        }
    }
    
    std::vector<int> prime_list = {2};
    int prime_list_max = 0;

    void CalcPrimeList(int z){
        while((int)prime_list.size() < z){
            if((int)isprime.size() <= prime_list_max + 1) CalcIsPrime(prime_list_max + 1);
            for(int p=prime_list_max+1; p<(int)isprime.size(); p++){
                if(isprime[p]) prime_list.push_back(p*2+1);
            }
            prime_list_max = isprime.size() - 1;
        }
    }
    
    void CalcPrimeListUntil(int z){
        if(prime_list_max < z){
            CalcIsPrime(z);
            for(int p=prime_list_max+1; p<(int)isprime.size(); p++){
                if(isprime[p]) prime_list.push_back(p*2+1);
            }
            prime_list_max = isprime.size() - 1;
        }
    }

}


bool IsprimeExplicit(int n){
    using namespace prime_sieve_explicit_internal;
    if(n == 2) return true;
    if(n % 2 == 0) return false;
    CalcIsPrime(n);
    return isprime[(n-1)/2];
}

int NthPrimeExplicit(int n){
    using namespace prime_sieve_explicit_internal;
    CalcPrimeList(n);
    return prime_list[n];
}

int PrimeCountingExplicit(int n){
    using namespace prime_sieve_explicit_internal;
    if(n < 2) return 0;
    CalcPrimeListUntil(n);
    auto res = ::std::upper_bound(prime_list.begin(), prime_list.end(), n) - prime_list.begin();
    return (int)res;
}

// [l, r)
::std::vector<bool> SegmentedSieveExplicit(long long l, long long r){
    assert(0 <= l); assert(l <= r);
    long long d = r - l;
    if(d == 0) return {};
    ::std::vector<bool> res(d, true);
    for(long long p=2; p*p<=r; p++) if(IsprimeExplicit(p)){
        long long il = (l+p-1)/p, ir = (r+p-1)/p;
        if(il <= p) il = p;
        for(long long i=il; i<ir; i++) res[i*p-l] = false;
    }
    if(l < 2) for(long long p=l; p<2 && p<r; p++) res[l-p] = false;
    return res;
}


}
#line 1 "nachia\\misc\\bit-operations.hpp"

#line 4 "nachia\\misc\\bit-operations.hpp"


namespace nachia{

    int Popcount(unsigned long long c) noexcept {
    #ifdef __GNUC__
        return __builtin_popcountll(c);
    #else
        c = (c & (~0ull/3)) + ((c >> 1) & (~0ull/3));
        c = (c & (~0ull/5)) + ((c >> 2) & (~0ull/5));
        c = (c & (~0ull/17)) + ((c >> 4) & (~0ull/17));
        c = (c * (~0ull/257)) >> 56;
        return c;
    #endif
    }

    // please ensure x != 0
    int MsbIndex(unsigned long long x) noexcept {
    #ifdef __GNUC__
        return 63 - __builtin_clzll(x);
    #else
        int res = 0;
        for(int d=32; d>=0; d>>=1) if(x >> d){ res |= d; x >>= d; }
        return res;
    #endif
    }

    // please ensure x != 0
    int LsbIndex(unsigned long long x) noexcept {
    #ifdef __GNUC__
        return __builtin_ctzll(x);
    #else
        return msb_idx(x & -x);
    #endif
    }

}

#line 4 "Main.cpp"

#line 6 "Main.cpp"
#include <string>
#line 9 "Main.cpp"
#include <queue>
#include <atcoder/modint>

using namespace std;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

using Modint = atcoder::static_modint<998244353>;


int main(){
    int N; cin >> N;
    vector<int> A(N);
    rep(i,N) cin >> A[i];
    int maxA = *max_element(A.begin(), A.end());

    vector<vector<int>> P(maxA+1);
    for(int p=1; p<=maxA; p++) if(nachia::IsprimeExplicit(p)) for(int q=p; q<=maxA; q+=p) P[q].push_back(p);
    vector<vector<int>> divs(maxA+1);
    auto QueryDivs = [&](int a) -> vector<int> {
        if(divs[a].size()) return divs[a];
        divs[a] = {1};
        for(int p : P[a]){
            int z = divs[a].size();
            rep(i,z) divs[a].push_back(divs[a][i] * p);
        }
        return divs[a];
    };
    
    Modint ans = 0;

    vector<Modint> dp(maxA+1, 0);
    for(int a : A){
        Modint c = 0;
        auto D = QueryDivs(a);
        for(int d : D) if(d != 1) c += dp[d];
        c += 1;
        rep(i,D.size()) if(i != 0) dp[D[i]] += ((nachia::Popcount(i)%2) ? c : -c);
        if(a == 1) ans += 1;
    }

    for(auto a : dp) ans += a;
    cout << ans.val() << '\n';
    return 0;
}


struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;


0