結果

問題 No.1036 Make One With GCD 2
ユーザー StrorkisStrorkis
提出日時 2020-04-25 11:10:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,198 ms / 2,000 ms
コード長 1,258 bytes
コンパイル時間 879 ms
コンパイル使用メモリ 75,832 KB
実行使用メモリ 15,396 KB
最終ジャッジ日時 2023-10-14 19:46:36
合計ジャッジ時間 21,666 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 773 ms
15,288 KB
testcase_01 AC 367 ms
15,228 KB
testcase_02 AC 457 ms
15,292 KB
testcase_03 AC 57 ms
8,468 KB
testcase_04 AC 101 ms
13,708 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 132 ms
8,784 KB
testcase_08 AC 110 ms
8,480 KB
testcase_09 AC 483 ms
14,864 KB
testcase_10 AC 450 ms
14,892 KB
testcase_11 AC 491 ms
15,224 KB
testcase_12 AC 453 ms
14,912 KB
testcase_13 AC 714 ms
14,900 KB
testcase_14 AC 720 ms
14,996 KB
testcase_15 AC 676 ms
14,760 KB
testcase_16 AC 680 ms
14,600 KB
testcase_17 AC 701 ms
14,860 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 3 ms
4,372 KB
testcase_21 AC 4 ms
4,368 KB
testcase_22 AC 666 ms
14,756 KB
testcase_23 AC 492 ms
14,032 KB
testcase_24 AC 690 ms
14,984 KB
testcase_25 AC 632 ms
14,408 KB
testcase_26 AC 653 ms
14,656 KB
testcase_27 AC 2 ms
4,368 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 1 ms
4,368 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 2 ms
4,372 KB
testcase_35 AC 1 ms
4,368 KB
testcase_36 AC 2 ms
4,368 KB
testcase_37 AC 2 ms
4,368 KB
testcase_38 AC 678 ms
15,172 KB
testcase_39 AC 1,198 ms
15,176 KB
testcase_40 AC 494 ms
13,560 KB
testcase_41 AC 1,013 ms
15,396 KB
testcase_42 AC 1,001 ms
15,140 KB
testcase_43 AC 962 ms
15,124 KB
testcase_44 AC 1,027 ms
15,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

long long gcd(long long a, long long b) {
    if (a == 0) return b;
    if (b == 0) return a;
    long long r = a % b;
    return r ? gcd(b, r) : b;
}

struct SegmentTree {
    int n;
    std::vector<long long> node;

    SegmentTree(const std::vector<long long>& A) {
        n = 1;
        while (n < A.size()) n *= 2;
        node.resize(n * 2 - 1);

        for (int i = 0; i < A.size(); i++)
            node[i + n - 1] = A[i];
        for (int i = n - 2; i >= 0; i--)
            node[i] = gcd(node[i * 2 + 1], node[i * 2 + 2]);
    }

    long long get(int a, int b, int k=0, int l=0, int r=-1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return 0;
        if (a <= l && r <= b) return node[k];
        return gcd(
            get(a, b, k * 2 + 1, l, (l + r) / 2),
            get(a, b, k * 2 + 2, (l + r) / 2, r)
        );
    }
};

int main() {
    int N;
    std::cin >> N;

    std::vector<long long> A(N);
    for (int i = 0; i < N; i++)
        std::cin >> A[i];

    SegmentTree st(A);
    long long ans = 0;
    int i = 0, j = 1;
    while (i < N) {
        while (j <= N && st.get(i, j) != 1) j++;
        ans += N - j + 1;
        if (++i == j) j++;
    }
    std::cout << ans << "\n";
}
0