結果

問題 No.1036 Make One With GCD 2
ユーザー potoooooooopotoooooooo
提出日時 2020-04-24 21:53:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,673 bytes
コンパイル時間 1,851 ms
コンパイル使用メモリ 177,152 KB
実行使用メモリ 90,612 KB
最終ジャッジ日時 2023-08-07 16:19:18
合計ジャッジ時間 21,861 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 404 ms
90,612 KB
testcase_01 AC 395 ms
89,696 KB
testcase_02 AC 403 ms
90,160 KB
testcase_03 AC 139 ms
43,328 KB
testcase_04 AC 253 ms
88,128 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 172 ms
44,152 KB
testcase_08 AC 146 ms
43,368 KB
testcase_09 AC 541 ms
88,812 KB
testcase_10 AC 507 ms
89,876 KB
testcase_11 AC 546 ms
89,360 KB
testcase_12 AC 502 ms
89,188 KB
testcase_13 AC 710 ms
89,868 KB
testcase_14 AC 716 ms
89,132 KB
testcase_15 AC 679 ms
88,896 KB
testcase_16 AC 677 ms
89,172 KB
testcase_17 AC 693 ms
89,860 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 665 ms
88,440 KB
testcase_23 AC 506 ms
87,780 KB
testcase_24 AC 687 ms
90,360 KB
testcase_25 AC 629 ms
88,596 KB
testcase_26 AC 657 ms
89,148 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 394 ms
89,204 KB
testcase_39 AC 428 ms
89,456 KB
testcase_40 AC 507 ms
87,784 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<class T> struct SparseTable {
    vector<vector<T>> dat;
    vector<int> lookup;
    using Func = function<T(T a, T b)>;
    Func f; T e;
    SparseTable(Func f, T e) : f(f), e(e) {}
    SparseTable(const vector<T> &v, Func f, T e) : f(f), e(e) { build(v); }
    void build(const vector<T> &v) {
        int sz = v.size(), b = 0;
        while ((1 << b) <= sz) b++;
        int n = 1 << b;
        dat.assign(b, vector<T>(n, e));
        for (int i = 0; i < n; 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.resize(n+1, 0);
        for (int i = 2; i <= n; i++) {
            lookup[i] = lookup[i/2] + 1; 
        }
    }
    // query : [l, r)
    T query(int l, int r) {
        int b = lookup[r - l];
        return f(dat[b][l], dat[b][r-(1<<b)]);
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    using D = long long;
    function<D(D, D)> g = [&](D a, D b) {
        if (b == 0LL) return a;
        if (a % b) return g(b, a % b);
        return b;
    };
    vector<D> a(n);
    for (auto &x: a) cin >> x;
    a.emplace_back(1);
    SparseTable<long long> st(a, g, 0);
    long long ans = 0;
    for (int i = 0; i < n; i++) {
        int lf = i, rg = n+1;
        while (rg - lf > 1) {
            int m = (lf + rg) / 2;
            if (st.query(i, m) == 1) rg = m;
            else lf = m;
        }
        ans += n+1 - rg;
    }
    cout << ans << "\n";
    return 0;
}
0