結果

問題 No.1036 Make One With GCD 2
ユーザー lorent_kyoprolorent_kyopro
提出日時 2020-12-25 16:29:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 286 ms / 2,000 ms
コード長 2,144 bytes
コンパイル時間 2,495 ms
コンパイル使用メモリ 208,492 KB
実行使用メモリ 15,696 KB
最終ジャッジ日時 2023-10-14 20:42:47
合計ジャッジ時間 10,173 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
7,480 KB
testcase_01 AC 146 ms
7,004 KB
testcase_02 AC 108 ms
15,588 KB
testcase_03 AC 14 ms
4,384 KB
testcase_04 AC 24 ms
5,732 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 42 ms
4,640 KB
testcase_08 AC 37 ms
4,380 KB
testcase_09 AC 166 ms
6,900 KB
testcase_10 AC 152 ms
6,852 KB
testcase_11 AC 161 ms
7,008 KB
testcase_12 AC 159 ms
6,708 KB
testcase_13 AC 286 ms
6,704 KB
testcase_14 AC 280 ms
6,960 KB
testcase_15 AC 250 ms
6,800 KB
testcase_16 AC 260 ms
6,436 KB
testcase_17 AC 273 ms
6,708 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 258 ms
6,784 KB
testcase_23 AC 193 ms
5,704 KB
testcase_24 AC 266 ms
6,692 KB
testcase_25 AC 235 ms
6,476 KB
testcase_26 AC 259 ms
6,528 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,356 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 105 ms
15,696 KB
testcase_39 AC 126 ms
7,004 KB
testcase_40 AC 182 ms
5,636 KB
testcase_41 AC 159 ms
6,896 KB
testcase_42 AC 154 ms
6,988 KB
testcase_43 AC 150 ms
9,324 KB
testcase_44 AC 151 ms
8,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

__attribute__((constructor))
void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

long long binary_gcd(long long a, long long b) {
    if (a == 0) return b;
    if (b == 0) return a;
    int n = __builtin_ctzll(a);
    int m = __builtin_ctzll(b);
    a >>= n;
    b >>= m;
    while (a != b) {
        if (a < b) swap(a, b);
        int k = __builtin_ctzll(a - b);
        a = (a - b) >> k;
    }
    return a << min(n, m);
}

template<class S, S (*op)(S, S)>
struct swag {
    struct node {
        S value, sum;
        node(const S& value, const S& sum) : value(value), sum(sum) {}
    };
    stack<node> front_stack, back_stack;
    swag() {}
    bool empty() const { return front_stack.empty() && back_stack.empty();}
    size_t size() const { return front_stack.size() + back_stack.size();}
    S fold() const {
        assert(!empty());
        if (front_stack.empty()) return back_stack.top().sum;
        else if (back_stack.empty()) return front_stack.top().sum;
        else return op(front_stack.top().sum, back_stack.top().sum);
    }
    void push(const S &x) {
        if (back_stack.empty()) back_stack.emplace(x, x);
        else {
            S s{op(back_stack.top().sum, x)};
            back_stack.emplace(x, s);
        }
    }
    void pop() {
        assert(!empty());
        if (front_stack.empty()) {
            front_stack.emplace(back_stack.top().value, back_stack.top().value);
            back_stack.pop();
            while (!back_stack.empty()) {
                S s{op(back_stack.top().value, front_stack.top().sum)};
                front_stack.emplace(back_stack.top().value, s);
                back_stack.pop();
            }
        }
        front_stack.pop();
    }
};

int main() {
    int n;
    cin >> n;
    vector<long long> a(n);
    for (auto& ai : a) cin >> ai;

    swag<long long, binary_gcd> q;
    long long ans = 0;
    for (int l = 0, r = 0; l < n; ++l) {
        while (r < n && (q.empty() || q.fold() != 1)) q.push(a[r++]);
        if (q.fold() == 1) ans += n - r + 1;
        q.pop();
    }
    cout << ans << '\n';
}
0