結果

問題 No.1036 Make One With GCD 2
ユーザー hedwig100hedwig100
提出日時 2020-04-25 22:29:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,729 bytes
コンパイル時間 1,716 ms
コンパイル使用メモリ 168,612 KB
実行使用メモリ 19,328 KB
最終ジャッジ日時 2024-04-25 12:17:59
合計ジャッジ時間 20,904 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
    int 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