結果

問題 No.1036 Make One With GCD 2
ユーザー betrue12betrue12
提出日時 2020-04-24 23:32:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 652 ms / 2,000 ms
コード長 1,625 bytes
コンパイル時間 3,889 ms
コンパイル使用メモリ 207,384 KB
実行使用メモリ 15,224 KB
最終ジャッジ日時 2023-10-14 19:33:53
合計ジャッジ時間 20,237 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 547 ms
15,144 KB
testcase_01 AC 400 ms
15,056 KB
testcase_02 AC 407 ms
15,116 KB
testcase_03 AC 85 ms
8,448 KB
testcase_04 AC 151 ms
13,856 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 158 ms
8,776 KB
testcase_08 AC 130 ms
8,512 KB
testcase_09 AC 423 ms
14,736 KB
testcase_10 AC 393 ms
14,568 KB
testcase_11 AC 430 ms
15,148 KB
testcase_12 AC 396 ms
14,948 KB
testcase_13 AC 585 ms
14,948 KB
testcase_14 AC 605 ms
14,892 KB
testcase_15 AC 562 ms
14,648 KB
testcase_16 AC 567 ms
14,876 KB
testcase_17 AC 586 ms
14,848 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 3 ms
4,372 KB
testcase_20 AC 4 ms
4,368 KB
testcase_21 AC 4 ms
4,368 KB
testcase_22 AC 557 ms
14,648 KB
testcase_23 AC 406 ms
13,624 KB
testcase_24 AC 570 ms
14,740 KB
testcase_25 AC 513 ms
14,636 KB
testcase_26 AC 544 ms
14,524 KB
testcase_27 AC 2 ms
4,368 KB
testcase_28 AC 1 ms
4,368 KB
testcase_29 AC 2 ms
4,372 KB
testcase_30 AC 1 ms
4,368 KB
testcase_31 AC 2 ms
4,368 KB
testcase_32 AC 1 ms
4,368 KB
testcase_33 AC 2 ms
4,368 KB
testcase_34 AC 2 ms
4,372 KB
testcase_35 AC 1 ms
4,368 KB
testcase_36 AC 2 ms
4,372 KB
testcase_37 AC 1 ms
4,368 KB
testcase_38 AC 471 ms
15,176 KB
testcase_39 AC 551 ms
15,076 KB
testcase_40 AC 401 ms
14,068 KB
testcase_41 AC 640 ms
15,148 KB
testcase_42 AC 627 ms
15,012 KB
testcase_43 AC 610 ms
15,224 KB
testcase_44 AC 652 ms
15,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T>
struct Segtree {
    int n, n_org;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        n_org = n_input;
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.assign(2*n-1, e);
    }

    void build(vector<T>& A){
        for(int k=0; k<int(A.size()); k++) dat[k+n-1] = A[k];
        for(int k=n-2; k>=0; k--) dat[k] = f(dat[2*k+1], dat[2*k+2]);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

int main(){
    int N;
    cin >> N;
    vector<int64_t> A(N);
    for(int i=0; i<N; i++) cin >> A[i];

    Segtree<int64_t> st(N, [](auto a, auto b){ return gcd(a, b); }, 0);
    st.build(A);
    int r = 0;
    int64_t ans = 0;
    for(int l=0; l<N; l++){
        while(r < N && st.between(l, r) != 1) r++;
        ans += N-r;
    }
    cout << ans << endl;
    return 0;
}
0