結果

問題 No.1036 Make One With GCD 2
ユーザー hedwig100hedwig100
提出日時 2020-04-25 22:31:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,142 ms / 2,000 ms
コード長 1,728 bytes
コンパイル時間 1,441 ms
コンパイル使用メモリ 170,616 KB
実行使用メモリ 19,240 KB
最終ジャッジ日時 2023-10-14 20:09:26
合計ジャッジ時間 22,128 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 746 ms
19,172 KB
testcase_01 AC 241 ms
19,052 KB
testcase_02 AC 461 ms
19,088 KB
testcase_03 AC 37 ms
10,052 KB
testcase_04 AC 61 ms
16,352 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 97 ms
10,404 KB
testcase_08 AC 80 ms
9,888 KB
testcase_09 AC 425 ms
18,848 KB
testcase_10 AC 398 ms
18,448 KB
testcase_11 AC 433 ms
19,176 KB
testcase_12 AC 402 ms
18,200 KB
testcase_13 AC 723 ms
18,536 KB
testcase_14 AC 731 ms
19,084 KB
testcase_15 AC 686 ms
18,304 KB
testcase_16 AC 689 ms
18,308 KB
testcase_17 AC 711 ms
18,588 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 678 ms
18,188 KB
testcase_23 AC 500 ms
16,284 KB
testcase_24 AC 703 ms
18,576 KB
testcase_25 AC 643 ms
17,868 KB
testcase_26 AC 667 ms
18,116 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,356 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 1 ms
4,356 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 678 ms
19,240 KB
testcase_39 AC 1,142 ms
19,048 KB
testcase_40 AC 502 ms
16,344 KB
testcase_41 AC 943 ms
19,240 KB
testcase_42 AC 923 ms
19,108 KB
testcase_43 AC 918 ms
19,152 KB
testcase_44 AC 970 ms
19,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i ++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PL;
typedef pair<int,int> P;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;

template <class T> class SegmentTree {
    int N = 1;
    vector<T> tree;
    function<T(T,T)> func;
    T unit;
    
    public:

    SegmentTree(vector<T> array,function<T(T,T)> _func,T _unit) :func(_func),unit(_unit) {
        int n = array.size();
        while (N < n) N *= 2;
        tree = vector<T>(2*N,unit);

        for (int i = 0;i < n; i++) tree[i + N - 1] = array[i];
        for (int i = N - 2; i >= 0; i--) tree[i] = func(tree[2*i + 1],tree[2*i + 2]);  
    }

    void update(int k,T x) {//k番目の要素をxに変更する
        k += N - 1;
        tree[k] = x;
        while (k) {
            k = (k - 1)/2;
            tree[k] = func(tree[2*k + 1],tree[2*k + 2]);
        }
    }

    T query(int l,int r) {//開区間[l,r)のクエリに答える
        if (r <= l) return unit;

        l += N - 1;
        r += N - 2;
        T ans = unit;

        while (r - l > 1) {
            if ((l&1) == 0) ans = func(ans,tree[l]);
            if ((r&1) == 1) {ans = func(ans,tree[r]); r--;}
            l /= 2;
            r = (r - 1)/2;
        }

        if (l == r) ans = func(ans,tree[l]);
        else ans = func(func(ans,tree[l]),tree[r]);
        return ans;
    }
};

int main() {
    int n; cin >> n;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    
    SegmentTree<ll> st(a,[](ll a, ll b){return __gcd(a,b);},0);

    int r = 0;
    ll ans = 0;
    for (int l = 0;l < n;l ++) {
        while (r < n && st.query(l,r + 1) != 1) r ++;
        ans += n - r;
    }
    cout << ans << endl;
}
0