結果

問題 No.1036 Make One With GCD 2
ユーザー finefine
提出日時 2020-04-25 02:11:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 383 ms / 2,000 ms
コード長 2,097 bytes
コンパイル時間 1,508 ms
コンパイル使用メモリ 173,780 KB
実行使用メモリ 15,188 KB
最終ジャッジ日時 2023-10-14 19:40:16
合計ジャッジ時間 11,548 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
7,660 KB
testcase_01 AC 82 ms
6,944 KB
testcase_02 AC 104 ms
15,188 KB
testcase_03 AC 13 ms
4,636 KB
testcase_04 AC 22 ms
5,648 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 30 ms
4,640 KB
testcase_08 AC 27 ms
4,372 KB
testcase_09 AC 223 ms
6,948 KB
testcase_10 AC 207 ms
6,688 KB
testcase_11 AC 221 ms
6,948 KB
testcase_12 AC 201 ms
6,696 KB
testcase_13 AC 360 ms
6,696 KB
testcase_14 AC 383 ms
6,848 KB
testcase_15 AC 344 ms
6,404 KB
testcase_16 AC 361 ms
6,680 KB
testcase_17 AC 373 ms
6,748 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 359 ms
6,400 KB
testcase_23 AC 262 ms
5,700 KB
testcase_24 AC 360 ms
6,636 KB
testcase_25 AC 326 ms
6,420 KB
testcase_26 AC 344 ms
6,432 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,356 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 106 ms
15,128 KB
testcase_39 AC 117 ms
6,880 KB
testcase_40 AC 255 ms
5,696 KB
testcase_41 AC 280 ms
6,976 KB
testcase_42 AC 264 ms
7,032 KB
testcase_43 AC 242 ms
9,272 KB
testcase_44 AC 264 ms
7,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

// https://scrapbox.io/data-structures/Sliding_Window_Aggregation
template<class SemiGroup>
struct SWAG {
    using T = typename SemiGroup::T;

    struct Node {
        T val, sum;
        Node(const T& v, const T& s) : val(v), sum(s) {}
    };

    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();
    }

    T fold_all() {
        assert(!empty());
        if (front_stack.empty()) return back_stack.top().sum;
        else if (back_stack.empty()) return front_stack.top().sum;
        else return SemiGroup::merge(front_stack.top().sum, back_stack.top().sum);
    }

    void push(const T& x) {
        if (back_stack.empty()) {
            back_stack.emplace(x, x);
        } else {
            T sum = SemiGroup::merge(back_stack.top().sum, x);
            back_stack.emplace(x, sum);
        }
    }

    void pop() {
        assert(!empty());

        if (front_stack.empty()) {
            front_stack.emplace(back_stack.top().val, back_stack.top().val);
            back_stack.pop();

            while (!back_stack.empty()) {
                T sum = SemiGroup::merge(front_stack.top().sum, back_stack.top().val);
                front_stack.emplace(back_stack.top().val, sum);
                back_stack.pop();
            }
        }
        front_stack.pop();
    }
};

// 以下、SemiGroupの例
template <class U = ll>
struct RMQSG {
    using T = U;
    static T merge(T a, T b) { return __gcd(a, b); }
};

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

    int n;
    cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    SWAG< RMQSG<> > swag;
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        swag.push(a[i]);
        while (!swag.empty() && swag.fold_all() == 1) swag.pop();

        ans += i + 1 - swag.size();
    }
    cout << ans << "\n";
    return 0;
}
0