結果

問題 No.1036 Make One With GCD 2
ユーザー milanis48663220milanis48663220
提出日時 2020-07-04 01:58:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,326 ms / 2,000 ms
コード長 1,958 bytes
コンパイル時間 2,026 ms
コンパイル使用メモリ 91,648 KB
実行使用メモリ 15,308 KB
最終ジャッジ日時 2023-10-14 20:35:25
合計ジャッジ時間 21,160 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 707 ms
15,256 KB
testcase_01 AC 315 ms
15,192 KB
testcase_02 AC 478 ms
15,180 KB
testcase_03 AC 95 ms
8,632 KB
testcase_04 AC 169 ms
13,716 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 124 ms
8,712 KB
testcase_08 AC 102 ms
8,484 KB
testcase_09 AC 428 ms
15,036 KB
testcase_10 AC 399 ms
14,776 KB
testcase_11 AC 438 ms
15,192 KB
testcase_12 AC 394 ms
14,736 KB
testcase_13 AC 559 ms
14,952 KB
testcase_14 AC 556 ms
15,044 KB
testcase_15 AC 527 ms
14,836 KB
testcase_16 AC 533 ms
14,776 KB
testcase_17 AC 548 ms
15,048 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 524 ms
14,716 KB
testcase_23 AC 383 ms
13,896 KB
testcase_24 AC 543 ms
15,032 KB
testcase_25 AC 506 ms
14,392 KB
testcase_26 AC 520 ms
14,728 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 2 ms
4,372 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 619 ms
15,308 KB
testcase_39 AC 1,326 ms
15,308 KB
testcase_40 AC 382 ms
13,980 KB
testcase_41 AC 974 ms
15,300 KB
testcase_42 AC 958 ms
15,184 KB
testcase_43 AC 982 ms
15,236 KB
testcase_44 AC 1,016 ms
15,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

template <typename T>
T gcd(T a, T b) {
	if (a < b) swap(a, b);
		while (b != 0) {
			T tmp = b;
			b = a % b;
			a = tmp;
		}
	return a;
}

template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    segtree(int n_, T unit){
        UNIT = unit;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    T calc(T a, T b){
        T ans;
        ans = gcd<ll>(a, b);
        return ans;
    }
    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

int N;
ll A[500000];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N; i++) cin >> A[i];
    segtree<ll> sgt(N, 0);
    for(int i = 0; i < N; i++) sgt.update(i, A[i]);
    ll ans = 0;
    int r = 0;
    for(int i = 0; i < N; i++){
        while(sgt.query(i, r+1) != 1 && r < N) r++;
        // cout << ans << endl;
        ans += N-r;
    }
    cout << ans << endl;
}
0