結果

問題 No.1036 Make One With GCD 2
ユーザー merom686merom686
提出日時 2020-04-25 17:03:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 829 ms / 2,000 ms
コード長 1,751 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 87,700 KB
実行使用メモリ 11,168 KB
最終ジャッジ日時 2023-10-14 19:52:25
合計ジャッジ時間 14,143 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 387 ms
11,168 KB
testcase_01 AC 86 ms
11,108 KB
testcase_02 AC 112 ms
11,044 KB
testcase_03 AC 17 ms
6,688 KB
testcase_04 AC 28 ms
9,748 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 34 ms
6,584 KB
testcase_08 AC 28 ms
6,396 KB
testcase_09 AC 251 ms
11,048 KB
testcase_10 AC 235 ms
11,016 KB
testcase_11 AC 256 ms
10,944 KB
testcase_12 AC 235 ms
10,720 KB
testcase_13 AC 440 ms
10,840 KB
testcase_14 AC 442 ms
11,052 KB
testcase_15 AC 415 ms
10,724 KB
testcase_16 AC 417 ms
10,948 KB
testcase_17 AC 432 ms
10,740 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 411 ms
10,488 KB
testcase_23 AC 303 ms
9,740 KB
testcase_24 AC 426 ms
10,724 KB
testcase_25 AC 388 ms
10,476 KB
testcase_26 AC 402 ms
10,424 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 109 ms
10,952 KB
testcase_39 AC 829 ms
11,028 KB
testcase_40 AC 302 ms
9,680 KB
testcase_41 AC 523 ms
10,940 KB
testcase_42 AC 519 ms
11,040 KB
testcase_43 AC 451 ms
10,948 KB
testcase_44 AC 504 ms
11,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
using ll = long long;

ll gcd(ll n, ll m) {
    if (n < m) swap(n, m);
    while (m != 0) {
        ll r = n % m;
        n = m;
        m = r;
    }
    return n;
}

template <class T>
struct SegmentTree {
    SegmentTree(int n) : n(n) {
        for (m = 2; m < n; m *= 2);
        t = vector<T>(m + n + n % 2);
    }
    T &operator[](int i) {
        return t[m + i];
    }
    const T &root() const {
        return t[1];
    }
    void build() {
        for (int i = (n - 1 + m) / 2; i > 0; i--) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    void update(int i, const T &o) {
        for (t[i += m] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1];
    }
    T query(int l, int r) {
        T o0, o1;
        for (l += m, r += m; l < r; l /= 2, r /= 2) {
            if (l & 1) o0 = o0 * t[l++];
            if (r & 1) o1 = t[--r] * o1;
        }
        return o0 * o1;
    }
    vector<T> t;
    int n, m;
};

struct Node {
    constexpr Node() : v(0) {}
    Node(ll v) : v(v) {}
    Node operator*(const Node &o) const {
        return gcd(v, o.v);
    }
    ll v;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    SegmentTree<Node> st(n);
    for (int i = 0; i < n; i++) {
        cin >> st[i].v;
    }
    st.build();

    ll r = 0;
    for (int i0 = 0, i1 = 0; i0 < n; i0++) {
        if (i1 <= n) {
            Node t = st.query(i0, i1).v;
            while (t.v != 1) {
                if (++i1 > n) break;
                t = t * st[i1 - 1].v;
            }
        }
        r += n - i1 + 1;
    }

    cout << r << endl;

    return 0;
}
0