結果

問題 No.2075 GCD Subsequence
ユーザー tktk_snsntktk_snsn
提出日時 2022-09-17 12:34:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,214 ms / 4,000 ms
コード長 2,535 bytes
コンパイル時間 969 ms
コンパイル使用メモリ 104,468 KB
実行使用メモリ 21,960 KB
最終ジャッジ日時 2023-08-23 17:39:29
合計ジャッジ時間 55,118 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
7,600 KB
testcase_01 AC 9 ms
7,648 KB
testcase_02 AC 10 ms
7,740 KB
testcase_03 AC 10 ms
7,540 KB
testcase_04 AC 9 ms
7,852 KB
testcase_05 AC 9 ms
7,588 KB
testcase_06 AC 10 ms
7,596 KB
testcase_07 AC 10 ms
7,536 KB
testcase_08 AC 1,468 ms
17,892 KB
testcase_09 AC 2,459 ms
21,960 KB
testcase_10 AC 1,586 ms
18,192 KB
testcase_11 AC 2,089 ms
20,184 KB
testcase_12 AC 1,793 ms
19,204 KB
testcase_13 AC 1,257 ms
16,776 KB
testcase_14 AC 2,042 ms
20,232 KB
testcase_15 AC 1,368 ms
17,372 KB
testcase_16 AC 1,490 ms
18,040 KB
testcase_17 AC 2,465 ms
21,616 KB
testcase_18 AC 3,164 ms
9,500 KB
testcase_19 AC 3,183 ms
9,500 KB
testcase_20 AC 3,167 ms
9,708 KB
testcase_21 AC 3,203 ms
9,396 KB
testcase_22 AC 3,182 ms
9,616 KB
testcase_23 AC 3,133 ms
9,500 KB
testcase_24 AC 3,194 ms
9,508 KB
testcase_25 AC 3,155 ms
9,536 KB
testcase_26 AC 3,128 ms
9,500 KB
testcase_27 AC 3,214 ms
9,696 KB
testcase_28 AC 2,001 ms
8,524 KB
testcase_29 AC 10 ms
7,584 KB
testcase_30 AC 9 ms
7,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <iomanip>
#include <numeric>
#include <string>
#include <bitset>
#include <assert.h>

// #define _GLIBCXX_DEBUG
// #define _LIBCPP_DEBUG 0
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define revrep(i, n) for(int i = (int)(n -1); i >= 0; --i)
#define range(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrange(i, s, t) for(int i = (int)(t - 1); i >= (int)(s); --i)
#define srange(i, s, t, step) for(int i = (int)(s); i < (int)(t); i+=int(step))
#define popcnt(x) __builtin_popcountll(x)

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
template<class T> inline bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }
inline bool inside(int x, int y, int h, int w) { return (x >= 0 && x < h && y >= 0 && y < w); }
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};

const ll mod = 998244353;


vector<int> prime_sieve(int n){
    vector<int> sieve(n+1, 0), prime;
    for (int i=2; i<n+1; i++){
        if (sieve[i]==0){
            sieve[i] = i;
            prime.emplace_back(i);
        }
        for (int p: prime){
            if (p > sieve[i] || i * p > n) break;
            sieve[i * p] = p;
        }
    }
    return sieve;
}


int main() {
    int n; 
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];

    auto sieve = prime_sieve(1e6);
    map<int, ll> mp;
    ll ans = 0;
    for (auto x: a) {
        cerr << x << endl;
        vector<int> v;
        while (sieve[x] != 0) {
            if (v.empty() || v[v.size()-1] != sieve[x]) v.push_back(sieve[x]);
            x /= sieve[x];
        }

        ll cnt = 1;
        int m = v.size();
        range(bit, 1, 1 << m) {
            int prod = 1;
            int num = 0;
            rep(i, m) if((bit >> i) & 1) num ^= 1, prod *= v[i];
            if (num == 1) cnt += mp[prod];
            else cnt += mod - mp[prod];
            if (cnt >= mod) cnt -= mod;
        }
        rep(bit, 1 << m) {
            int prod = 1;
            rep(i, m) if((bit >> i) & 1) prod *= v[i];
            mp[prod] += cnt;
            if (mp[prod] >= mod) mp[prod] -= mod;
        }
        ans += cnt;
        if (ans >= mod) ans -= mod;
        cerr << cnt << endl << endl;;
    }
    cout << ans << endl;
    return 0;
}
0