結果

問題 No.1036 Make One With GCD 2
ユーザー StrorkisStrorkis
提出日時 2020-04-25 10:26:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,252 bytes
コンパイル時間 1,139 ms
コンパイル使用メモリ 76,180 KB
実行使用メモリ 15,592 KB
最終ジャッジ日時 2024-04-24 19:12:19
合計ジャッジ時間 20,426 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 454 ms
15,304 KB
testcase_03 AC 55 ms
8,832 KB
testcase_04 AC 102 ms
14,080 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 130 ms
8,960 KB
testcase_08 AC 104 ms
8,704 KB
testcase_09 AC 456 ms
15,244 KB
testcase_10 AC 448 ms
14,912 KB
testcase_11 AC 501 ms
15,560 KB
testcase_12 AC 467 ms
15,032 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 490 ms
15,300 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 992 ms
15,284 KB
testcase_42 AC 985 ms
15,488 KB
testcase_43 AC 964 ms
15,592 KB
testcase_44 AC 1,014 ms
15,576 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]);
    }

    int 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