結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 772 ms
15,180 KB
testcase_01 AC 366 ms
15,188 KB
testcase_02 AC 456 ms
15,168 KB
testcase_03 AC 56 ms
8,552 KB
testcase_04 AC 99 ms
13,632 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 132 ms
8,780 KB
testcase_08 AC 109 ms
8,476 KB
testcase_09 AC 481 ms
15,216 KB
testcase_10 AC 450 ms
14,676 KB
testcase_11 AC 491 ms
15,280 KB
testcase_12 AC 456 ms
14,864 KB
testcase_13 AC 712 ms
14,868 KB
testcase_14 AC 719 ms
14,916 KB
testcase_15 AC 676 ms
14,752 KB
testcase_16 AC 681 ms
14,664 KB
testcase_17 AC 702 ms
14,904 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 4 ms
4,352 KB
testcase_22 AC 666 ms
14,756 KB
testcase_23 AC 492 ms
13,940 KB
testcase_24 AC 695 ms
14,952 KB
testcase_25 AC 631 ms
14,608 KB
testcase_26 AC 655 ms
14,652 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 678 ms
15,172 KB
testcase_39 AC 1,198 ms
15,176 KB
testcase_40 AC 492 ms
13,972 KB
testcase_41 AC 1,012 ms
15,192 KB
testcase_42 AC 996 ms
15,128 KB
testcase_43 AC 966 ms
15,228 KB
testcase_44 AC 1,027 ms
15,172 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