結果

問題 No.1036 Make One With GCD 2
ユーザー betrue12betrue12
提出日時 2020-04-24 21:42:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 505 ms / 2,000 ms
コード長 1,387 bytes
コンパイル時間 2,963 ms
コンパイル使用メモリ 208,508 KB
実行使用メモリ 89,252 KB
最終ジャッジ日時 2023-10-14 19:14:00
合計ジャッジ時間 17,173 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 443 ms
89,104 KB
testcase_01 AC 333 ms
88,880 KB
testcase_02 AC 407 ms
89,072 KB
testcase_03 AC 75 ms
43,320 KB
testcase_04 AC 131 ms
87,372 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 2 ms
4,356 KB
testcase_07 AC 142 ms
43,584 KB
testcase_08 AC 127 ms
43,400 KB
testcase_09 AC 344 ms
88,708 KB
testcase_10 AC 308 ms
88,664 KB
testcase_11 AC 326 ms
88,964 KB
testcase_12 AC 305 ms
88,820 KB
testcase_13 AC 477 ms
88,800 KB
testcase_14 AC 475 ms
88,832 KB
testcase_15 AC 453 ms
88,660 KB
testcase_16 AC 462 ms
88,504 KB
testcase_17 AC 505 ms
88,728 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 4 ms
4,372 KB
testcase_21 AC 3 ms
4,356 KB
testcase_22 AC 458 ms
88,492 KB
testcase_23 AC 358 ms
87,452 KB
testcase_24 AC 464 ms
88,812 KB
testcase_25 AC 429 ms
88,572 KB
testcase_26 AC 445 ms
88,544 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,360 KB
testcase_30 AC 1 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,356 KB
testcase_34 AC 2 ms
4,356 KB
testcase_35 AC 1 ms
4,356 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,356 KB
testcase_38 AC 382 ms
89,092 KB
testcase_39 AC 465 ms
89,016 KB
testcase_40 AC 363 ms
87,408 KB
testcase_41 AC 450 ms
88,884 KB
testcase_42 AC 458 ms
88,936 KB
testcase_43 AC 443 ms
89,252 KB
testcase_44 AC 446 ms
89,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
struct SparseTable{
    vector<vector<T>> dat;
    vector<int> lookup;
    typedef function<T(T a, T b)> Func;
    Func f;
    T e;

    SparseTable(){}
    SparseTable(const vector<T> &v, Func f, T e){ initialize(v, f, e); }
    void initialize(const vector<T> &v, Func fin, T ein){
        f = fin;
        e = ein;
        int b = 0;
        while((1<<b) <= v.size()) b++;
        int N = 1<<b;
        dat.assign(b, vector<T>(N, e));
        for(int i=0; i<v.size(); i++) dat[0][i] = v[i];

        for(int i=1; i<b; i++){
            int d = 1<<i;
            for(int j=0; j+d<=N; j++){
                dat[i][j] = f(dat[i-1][j], dat[i-1][j+d/2]);
            }
        }
        lookup.assign(N+1, 0);
        for(int i=2; i<=N; i++){
            lookup[i] = lookup[i>>1] + 1;
        }
    }

    T between(int l, int r) {
        if(l > r) return e;
        r++;
        int b = lookup[r-l];
        return f(dat[b][l], dat[b][r-(1<<b)]);
    }
};

int main(){
    int N;
    cin >> N;
    vector<int64_t> A(N);
    for(int i=0; i<N; i++) cin >> A[i];

    SparseTable<int64_t> st(A, [](auto a, auto b){ return gcd(a, b); }, 0);
    int r = 0;
    int64_t ans = 0;
    for(int l=0; l<N; l++){
        while(r < N && st.between(l, r) != 1) r++;
        ans += N-r;
    }
    cout << ans << endl;
    return 0;
}
0