結果

問題 No.1036 Make One With GCD 2
ユーザー roarisroaris
提出日時 2020-04-24 22:25:35
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,359 bytes
コンパイル時間 2,491 ms
コンパイル使用メモリ 133,164 KB
実行使用メモリ 24,068 KB
最終ジャッジ日時 2023-08-20 14:51:37
合計ジャッジ時間 10,809 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define int long long

int N;
int A[500100];

struct segtree {
    int size;
    vector<int> node;
    
    segtree(int n) {
        size = 1;
        while (size<n) size *= 2;
        node.resize(2*size-1);
        fill(node.begin(), node.end(), 0);
    }

    void update(int k, int a) {
        k += size-1;
        node[k] = a;
        while (k>0) {
            k = (k-1)/2;
            node[k] = gcd(node[2*k+1], node[2*k+2]);
        }
    }
    
    int query(int a, int b, int k, int l, int r) {
        if (r<=a || b<=l) return 0;
        if (a<=l && r<=b) return node[k];
        else {
            int vl = query(a, b, 2*k+1, l, (l+r)/2);
            int vr = query(a, b, 2*k+2, (l+r)/2, r);
            return gcd(vl, vr);
        }
    }
    
    int get(int a, int b) {
        return query(a, b, 0, 0, size);
    }
};

signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N;
    rep(i, N) cin >> A[i];
    
    segtree st(N);
    rep(i, N) st.update(i, A[i]);
    
    int ans = 0;
    rep(i, N) {
        int l = i, r = N-1;
        while (l<=r) {
            int m = (l+r)/2;
            if (st.get(i, m+1)>1) l = m+1;
            else r = m-1;
        }
        ans += N-r-1;
    }
    
    cout << ans << endl;
}
0