結果

問題 No.1036 Make One With GCD 2
ユーザー roarisroaris
提出日時 2020-04-24 22:32:59
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 441 ms / 2,000 ms
コード長 1,381 bytes
コンパイル時間 3,690 ms
コンパイル使用メモリ 134,132 KB
実行使用メモリ 15,204 KB
最終ジャッジ日時 2023-10-14 19:27:28
合計ジャッジ時間 16,145 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 367 ms
15,096 KB
testcase_01 AC 330 ms
15,204 KB
testcase_02 AC 332 ms
15,072 KB
testcase_03 AC 37 ms
8,480 KB
testcase_04 AC 60 ms
14,620 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 109 ms
8,844 KB
testcase_08 AC 90 ms
8,464 KB
testcase_09 AC 308 ms
15,136 KB
testcase_10 AC 292 ms
14,880 KB
testcase_11 AC 315 ms
15,140 KB
testcase_12 AC 290 ms
14,872 KB
testcase_13 AC 427 ms
14,904 KB
testcase_14 AC 424 ms
14,860 KB
testcase_15 AC 396 ms
14,924 KB
testcase_16 AC 399 ms
14,884 KB
testcase_17 AC 409 ms
14,868 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 4 ms
4,372 KB
testcase_21 AC 3 ms
4,372 KB
testcase_22 AC 400 ms
14,692 KB
testcase_23 AC 291 ms
14,416 KB
testcase_24 AC 407 ms
14,860 KB
testcase_25 AC 367 ms
14,344 KB
testcase_26 AC 389 ms
14,660 KB
testcase_27 AC 1 ms
4,372 KB
testcase_28 AC 1 ms
4,372 KB
testcase_29 AC 2 ms
4,368 KB
testcase_30 AC 1 ms
4,372 KB
testcase_31 AC 2 ms
4,368 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 2 ms
4,368 KB
testcase_35 AC 2 ms
4,368 KB
testcase_36 AC 1 ms
4,368 KB
testcase_37 AC 1 ms
4,372 KB
testcase_38 AC 331 ms
15,076 KB
testcase_39 AC 377 ms
15,184 KB
testcase_40 AC 290 ms
13,928 KB
testcase_41 AC 427 ms
15,068 KB
testcase_42 AC 422 ms
15,148 KB
testcase_43 AC 441 ms
15,128 KB
testcase_44 AC 434 ms
15,120 KB
権限があれば一括ダウンロードができます

ソースコード

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;
    int G = 0;
    int r = 0;
    rep(l, N) {
        while (r<N && gcd(G, A[r])>1) {
            G = gcd(G, A[r]);
            r++;
        }
        ans += N-r;
        if (l==r) r++;
        else G = st.get(l+1, r);
    }
    
    cout << ans << endl;
}
0