結果

問題 No.1036 Make One With GCD 2
ユーザー roarisroaris
提出日時 2020-04-27 15:27:20
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 454 ms / 2,000 ms
コード長 1,377 bytes
コンパイル時間 3,337 ms
コンパイル使用メモリ 133,768 KB
実行使用メモリ 15,208 KB
最終ジャッジ日時 2023-10-14 20:23:59
合計ジャッジ時間 15,261 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 388 ms
15,164 KB
testcase_01 AC 370 ms
15,136 KB
testcase_02 AC 344 ms
15,132 KB
testcase_03 AC 41 ms
8,536 KB
testcase_04 AC 65 ms
13,852 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 117 ms
8,764 KB
testcase_08 AC 97 ms
8,476 KB
testcase_09 AC 339 ms
14,940 KB
testcase_10 AC 314 ms
14,864 KB
testcase_11 AC 344 ms
15,176 KB
testcase_12 AC 316 ms
14,924 KB
testcase_13 AC 448 ms
14,880 KB
testcase_14 AC 454 ms
14,972 KB
testcase_15 AC 426 ms
14,924 KB
testcase_16 AC 431 ms
14,740 KB
testcase_17 AC 444 ms
14,924 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 3 ms
4,372 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 421 ms
14,612 KB
testcase_23 AC 310 ms
13,876 KB
testcase_24 AC 436 ms
14,828 KB
testcase_25 AC 400 ms
14,412 KB
testcase_26 AC 413 ms
14,604 KB
testcase_27 AC 2 ms
4,372 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 1 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 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,372 KB
testcase_36 AC 2 ms
4,368 KB
testcase_37 AC 2 ms
4,372 KB
testcase_38 AC 340 ms
15,128 KB
testcase_39 AC 399 ms
15,192 KB
testcase_40 AC 311 ms
13,928 KB
testcase_41 AC 445 ms
15,200 KB
testcase_42 AC 442 ms
15,208 KB
testcase_43 AC 449 ms
15,124 KB
testcase_44 AC 446 ms
15,156 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

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);
    int N; cin >> N;
    int A[N];
    rep(i, N) cin >> A[i];
    
    segtree st(N);
    rep(i, N) st.update(i, A[i]);
    
    int ans = 0;
    int r = 0;
    int G = 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);
    }
    
    printf("%lld\n", ans);
}
0