結果

問題 No.1036 Make One With GCD 2
ユーザー ShibuyapShibuyap
提出日時 2020-05-29 17:55:38
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,480 ms / 2,000 ms
コード長 2,812 bytes
コンパイル時間 2,337 ms
コンパイル使用メモリ 200,164 KB
実行使用メモリ 17,940 KB
最終ジャッジ日時 2023-10-14 20:32:10
合計ジャッジ時間 24,690 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 852 ms
17,832 KB
testcase_01 AC 396 ms
17,660 KB
testcase_02 AC 373 ms
17,724 KB
testcase_03 AC 91 ms
11,008 KB
testcase_04 AC 166 ms
17,684 KB
testcase_05 AC 2 ms
5,520 KB
testcase_06 AC 2 ms
5,536 KB
testcase_07 AC 135 ms
11,204 KB
testcase_08 AC 111 ms
10,824 KB
testcase_09 AC 513 ms
17,752 KB
testcase_10 AC 477 ms
17,724 KB
testcase_11 AC 524 ms
17,660 KB
testcase_12 AC 482 ms
17,796 KB
testcase_13 AC 655 ms
17,696 KB
testcase_14 AC 661 ms
17,684 KB
testcase_15 AC 619 ms
17,680 KB
testcase_16 AC 628 ms
17,692 KB
testcase_17 AC 643 ms
17,696 KB
testcase_18 AC 3 ms
5,436 KB
testcase_19 AC 3 ms
5,492 KB
testcase_20 AC 4 ms
5,456 KB
testcase_21 AC 4 ms
5,548 KB
testcase_22 AC 613 ms
17,840 KB
testcase_23 AC 450 ms
17,756 KB
testcase_24 AC 635 ms
17,840 KB
testcase_25 AC 580 ms
17,656 KB
testcase_26 AC 601 ms
17,684 KB
testcase_27 AC 2 ms
5,444 KB
testcase_28 AC 2 ms
5,468 KB
testcase_29 AC 2 ms
5,632 KB
testcase_30 AC 2 ms
5,480 KB
testcase_31 AC 3 ms
5,380 KB
testcase_32 AC 2 ms
5,448 KB
testcase_33 AC 2 ms
5,380 KB
testcase_34 AC 2 ms
5,372 KB
testcase_35 AC 2 ms
5,404 KB
testcase_36 AC 2 ms
5,372 KB
testcase_37 AC 2 ms
5,404 KB
testcase_38 AC 546 ms
17,656 KB
testcase_39 AC 1,480 ms
17,696 KB
testcase_40 AC 449 ms
17,704 KB
testcase_41 AC 1,140 ms
17,708 KB
testcase_42 AC 1,120 ms
17,724 KB
testcase_43 AC 1,113 ms
17,652 KB
testcase_44 AC 1,174 ms
17,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef pair<int,int> P;
typedef long long int ll;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 1 << 20;

// セグメント木を持つグローバル配列
ll nn, dat[2 * MAX_N - 1];

// 関数
long long int GCD(long long int x, long long int y){
    x = abs(x);
    y = abs(y);
    if(x == 0) return abs(y);
    if(y == 0) return abs(x);
    if(x>y){
        long long int swap = x;
        x = y;
        y = swap;
    }
    while(true){
        if(y%x==0){
            return abs(x);
        }else{
            long long int amari = y%x;
            y = x;
            x = amari;
        }
    }
}

// function
ll func(ll x, ll y){
    return GCD(x, y);
}

// 単位元を入れる
ll init_val(){
    return 0;
}

// 初期化
void init(int n_){
    // n_が小さいときの変な挙動を防ぐ
    if(n_ < 10)n_ = 10;
    // 簡単のため要素数を2のべき乗に
    nn = 1;
    while(nn < n_)nn *= 2;

    // 全ての値を単位元に
    for(int i = 0; i < 2 * nn - 1; i++)dat[i] = init_val(); // 適宜変える
}

// k番目の値(0-indexed)をaに変更
void update(int k, ll a){
    // 葉の接点
    k += nn - 1;
    dat[k] = a;
    // 登りながら更新
    while(k > 0){
        k = (k - 1) / 2;
        dat[k] = func(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
}

// [a,b)の最小値を求める
// 後ろのほうの引数は、計算の簡単のための引数。
// kは節点の番号、l, rはその節点が[l, r)に対応づいていることを表す。
// したがって、外からはquery(a, b, 0, 0, nn)として呼ぶ。
ll query(int a, int b, int k, int l, int r){
    // [a, b)と[l, r)が交差しなければ、単位元
    if (r <= a || b <= l)return init_val();

    // [a, b)が[l, r)を完全に含んでいれば、この節点の値
    if (a <= l && r <= b)return dat[k];
    else {
        // そうでなければ、2つの子の最小値
        ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return func(vl, vr);
    }
}

ll a[600000];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin >> n;
    init(n);
    rep(i,n)cin >> a[i];

    rep(i,n){
        update(i,a[i]);
    }

    ll ans = 0;
    int now = 0;

    rep(i,n){
        now = max(now, i);
        while(now < n && query(i,now+1,0,0,nn) != 1){
            now++;
        }

        if(now == n){
            break;
        }

        ans += n - now;

    }

    cout << ans << endl;
    return 0;
}

0