結果

問題 No.1036 Make One With GCD 2
ユーザー veqccveqcc
提出日時 2020-04-24 22:08:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,104 ms / 2,000 ms
コード長 1,666 bytes
コンパイル時間 980 ms
コンパイル使用メモリ 109,244 KB
実行使用メモリ 15,368 KB
最終ジャッジ日時 2023-10-14 19:19:33
合計ジャッジ時間 19,095 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 693 ms
15,160 KB
testcase_01 AC 237 ms
15,228 KB
testcase_02 AC 446 ms
15,368 KB
testcase_03 AC 37 ms
8,620 KB
testcase_04 AC 64 ms
13,760 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 97 ms
8,748 KB
testcase_08 AC 80 ms
8,580 KB
testcase_09 AC 355 ms
14,848 KB
testcase_10 AC 332 ms
14,580 KB
testcase_11 AC 363 ms
15,108 KB
testcase_12 AC 336 ms
14,952 KB
testcase_13 AC 590 ms
14,888 KB
testcase_14 AC 596 ms
14,964 KB
testcase_15 AC 556 ms
14,572 KB
testcase_16 AC 559 ms
15,144 KB
testcase_17 AC 579 ms
14,792 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 548 ms
14,632 KB
testcase_23 AC 405 ms
14,064 KB
testcase_24 AC 570 ms
14,888 KB
testcase_25 AC 520 ms
14,796 KB
testcase_26 AC 541 ms
14,576 KB
testcase_27 AC 1 ms
4,356 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 2 ms
4,356 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 1 ms
4,372 KB
testcase_38 AC 637 ms
15,140 KB
testcase_39 AC 1,104 ms
15,136 KB
testcase_40 AC 406 ms
13,624 KB
testcase_41 AC 890 ms
15,236 KB
testcase_42 AC 881 ms
15,232 KB
testcase_43 AC 853 ms
15,164 KB
testcase_44 AC 906 ms
15,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <random>
#include <bitset>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;

template <typename T>
class SegmentTree {
    using F = function<T(T, T)>;
    int n;
    F f;
    T t;
    vector <T> dat;
public:
    SegmentTree(F f, T t) : f(f), t(t) {}
    void init(int n_) {
        n = 1;
        while (n < n_) n <<= 1;
        dat.assign(n << 1, t);
    }
    void build(const vector <T> &v) {
        auto n_ = (int)v.size();
        init(n_);
        for (int i = 0; i < n_; i++) dat[n + i] = v[i];
        for (int i = n - 1; i > 0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
    }
    void set_val(int k, T x) {
        dat[k += n] = x;
        while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
    }
    T query(int a, int b) {
        T vl = t, vr = t;
        for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) vl = f(vl, dat[l++]);
            if (r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }
};

ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); }
int main() {
    int n;
    cin >> n;

    vector <ll> a(n + 1, 1);
    for (int i = 0; i < n; i++) cin >> a[i];

    SegmentTree<ll> seg(gcd, 0);
    seg.build(a);
    ll ans = 0;
    int r = 1;
    for (int l = 0; l < n; l++) {
        while (r <= n && seg.query(l, r) != 1) r++;
        ans += n - r + 1;
    }

    cout << ans << "\n";
    return 0;
}

0