結果

問題 No.1036 Make One With GCD 2
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-02-29 19:33:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 434 ms / 2,000 ms
コード長 1,739 bytes
コンパイル時間 2,875 ms
コンパイル使用メモリ 211,764 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2024-02-29 19:33:45
合計ジャッジ時間 17,211 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 306 ms
15,488 KB
testcase_01 AC 240 ms
15,488 KB
testcase_02 AC 292 ms
15,488 KB
testcase_03 AC 38 ms
8,896 KB
testcase_04 AC 69 ms
14,032 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 77 ms
9,080 KB
testcase_08 AC 63 ms
8,856 KB
testcase_09 AC 274 ms
15,312 KB
testcase_10 AC 250 ms
15,160 KB
testcase_11 AC 268 ms
15,464 KB
testcase_12 AC 247 ms
15,096 KB
testcase_13 AC 346 ms
15,248 KB
testcase_14 AC 349 ms
15,336 KB
testcase_15 AC 327 ms
15,112 KB
testcase_16 AC 330 ms
15,168 KB
testcase_17 AC 342 ms
15,264 KB
testcase_18 AC 3 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 3 ms
6,676 KB
testcase_21 AC 3 ms
6,676 KB
testcase_22 AC 324 ms
15,024 KB
testcase_23 AC 239 ms
14,072 KB
testcase_24 AC 338 ms
15,184 KB
testcase_25 AC 327 ms
14,920 KB
testcase_26 AC 321 ms
15,032 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 1 ms
6,676 KB
testcase_37 AC 1 ms
6,676 KB
testcase_38 AC 365 ms
15,488 KB
testcase_39 AC 289 ms
15,488 KB
testcase_40 AC 254 ms
14,072 KB
testcase_41 AC 392 ms
15,488 KB
testcase_42 AC 379 ms
15,488 KB
testcase_43 AC 414 ms
15,488 KB
testcase_44 AC 434 ms
15,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

template<class S, S (*op)(S, S), S (*e)()>struct SegTree {
    private:
        vector<S> seg; int N = 1;
        S _prod (int l, int r, int idx, int bitl, int bitr) const{
            if (r < bitl || l > bitr) return e();
            if (l <= bitl && bitr <= r) return seg[idx];

            ll bitm = (bitl+bitr)/2;
            return op(_prod(l, r, idx*2+1, bitl, bitm), _prod(l, r, idx*2+2, bitm+1, bitr));
        }

    public:
        SegTree (ll n) : SegTree(vector<S>(n, e())) {}
        SegTree (const vector<S> &v){
            ll n = v.size(); while (N < n) N *= 2; seg.resize(N*2-1, e());
            for (ll i=0; i<n; i++) seg[i+N-1] = v[i];
            for (ll i=N-2; i>=0; i--) seg[i] = op(seg[i*2+1], seg[i*2+2]);
        }
        void set(int loc, S val){
            loc += N-1; seg[loc] = val;
            while (loc != 0) loc = (loc-1)/2, seg[loc] = op(seg[loc*2+1], seg[loc*2+2]);
        }
        S prod (int l, int r) const {return _prod(l, r, 0, 0, N-1);}
        S all_prod() const {return seg[0];}
        S get (int i) const{return seg[i+N-1];}
        void show() const{
            for (int i=N-1; i<N*2-1; i++) cout << seg[i] << " ";
            cout << endl;
        }
};

//Range GCD Query
using S = ll;
S op(S a, S b){return gcd(a, b);}
S e(){return 0;}
 
int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
 
    ll N, ans=0, r=0;
    cin >> N;
    vector<S> v(N);
    for (int i=0; i<N; i++) cin >> v[i];
    SegTree<S, op, e> tree(v);

    for (int l=0; l<N; l++){
        while(tree.prod(l, r) != 1 && r < N) r++;
        ans += N-r;
        if (l == r) r++;
    }

    cout << ans << endl;
 
    return 0;
}
0