結果

問題 No.1036 Make One With GCD 2
ユーザー potoooooooopotoooooooo
提出日時 2020-04-25 21:45:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,247 ms / 2,000 ms
コード長 1,658 bytes
コンパイル時間 2,415 ms
コンパイル使用メモリ 211,644 KB
実行使用メモリ 90,576 KB
最終ジャッジ日時 2023-10-14 20:07:24
合計ジャッジ時間 22,883 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 276 ms
89,304 KB
testcase_01 AC 261 ms
89,492 KB
testcase_02 AC 274 ms
89,740 KB
testcase_03 AC 90 ms
43,436 KB
testcase_04 AC 160 ms
87,708 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 112 ms
44,308 KB
testcase_08 AC 100 ms
43,288 KB
testcase_09 AC 513 ms
88,880 KB
testcase_10 AC 486 ms
89,708 KB
testcase_11 AC 521 ms
90,060 KB
testcase_12 AC 488 ms
88,748 KB
testcase_13 AC 881 ms
88,948 KB
testcase_14 AC 891 ms
89,768 KB
testcase_15 AC 842 ms
89,216 KB
testcase_16 AC 845 ms
89,240 KB
testcase_17 AC 870 ms
89,768 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 3 ms
4,372 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 830 ms
89,016 KB
testcase_23 AC 634 ms
87,940 KB
testcase_24 AC 863 ms
89,420 KB
testcase_25 AC 794 ms
88,488 KB
testcase_26 AC 818 ms
89,708 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 278 ms
89,948 KB
testcase_39 AC 304 ms
90,576 KB
testcase_40 AC 633 ms
88,900 KB
testcase_41 AC 1,245 ms
88,900 KB
testcase_42 AC 1,247 ms
90,444 KB
testcase_43 AC 1,017 ms
89,972 KB
testcase_44 AC 1,145 ms
89,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class T> struct SparseTable {
    vector<vector<T>> dat;
    vector<int> lookup;
    using Func = function<T(T a, T b)>;
    Func f; T e;
    SparseTable(Func f, T e) : f(f), e(e) {}
    SparseTable(const vector<T> &v, Func f, T e) : f(f), e(e) { build(v); }
    void build(const vector<T> &v) {
        int sz = v.size(), b = 0;
        while ((1 << b) <= sz) b++;
        int n = 1 << b;
        dat.assign(b, vector<T>(n, e));
        for (int i = 0; i < n; i++) dat[0][i] = v[i];
        for (int i = 1; i < b; i++) {
            int d = 1 << i;
            for (int j = 0; j + d <= n; j++) {
                dat[i][j] = f(dat[i-1][j], dat[i-1][j+d/2]);
            }
        }
        lookup.resize(n+1, 0);
        for (int i = 2; i <= n; i++) {
            lookup[i] = lookup[i/2] + 1; 
        }
    }
    // query : [l, r)
    T query(int l, int r) {
        int b = lookup[r - l];
        return f(dat[b][l], dat[b][r-(1<<b)]);
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    using D = long long;
    function<D(D, D)> g = [&](D a, D b) {
        if (b == 0LL) return a;
        if (a % b) return g(b, a % b);
        return b;
    };
    vector<D> a(n);
    for (auto &x: a) cin >> x;
    a.emplace_back(1);
    SparseTable<long long> st(a, g, 0);
    long long ans = 0;
    int rg = 1;
    for (int i = 0; i < n; i++) {
        long long curr = st.query(i, rg);
        while (curr != 1 && rg < n) curr = g(curr, a[rg++]);
        ans += n - rg + (curr == 1);
        if (i+1 == rg) rg++;
    }
    cout << ans << "\n";
    return 0;
}
0