結果

問題 No.1036 Make One With GCD 2
ユーザー potoooooooopotoooooooo
提出日時 2020-06-02 23:12:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 548 ms / 2,000 ms
コード長 1,639 bytes
コンパイル時間 2,684 ms
コンパイル使用メモリ 208,324 KB
実行使用メモリ 11,252 KB
最終ジャッジ日時 2023-10-14 20:31:44
合計ジャッジ時間 14,553 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
4,348 KB
testcase_01 AC 105 ms
4,380 KB
testcase_02 AC 122 ms
11,116 KB
testcase_03 AC 18 ms
4,352 KB
testcase_04 AC 29 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 38 ms
4,352 KB
testcase_08 AC 32 ms
4,352 KB
testcase_09 AC 273 ms
4,348 KB
testcase_10 AC 256 ms
4,352 KB
testcase_11 AC 280 ms
4,348 KB
testcase_12 AC 257 ms
4,352 KB
testcase_13 AC 543 ms
4,352 KB
testcase_14 AC 548 ms
4,348 KB
testcase_15 AC 516 ms
4,352 KB
testcase_16 AC 518 ms
4,352 KB
testcase_17 AC 536 ms
4,352 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 2 ms
4,356 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 510 ms
4,352 KB
testcase_23 AC 374 ms
4,352 KB
testcase_24 AC 527 ms
4,372 KB
testcase_25 AC 480 ms
4,352 KB
testcase_26 AC 499 ms
4,356 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 2 ms
4,356 KB
testcase_34 AC 2 ms
4,356 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 123 ms
11,252 KB
testcase_39 AC 135 ms
4,352 KB
testcase_40 AC 373 ms
4,348 KB
testcase_41 AC 377 ms
4,352 KB
testcase_42 AC 377 ms
4,380 KB
testcase_43 AC 326 ms
5,612 KB
testcase_44 AC 358 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// V = val, R = ret, F = function<V(R, V)>
template<class V, class R, class F>
struct SlidingWindow {
    const F f; const R id;
    stack<pair<V, R>> front, back;

    SlidingWindow(const F &f, const R &id) : f(f), id(id) {}
    
    size_t size() const { return front.size() + back.size(); }
    bool empty() const { return size() == 0; }

    void emplace(const V &val) { 
        if (back.empty()) back.emplace(val, val);
        else back.emplace(val, f(back.top().second, val));
    }

    void pop() {
        assert(!empty());
        if (front.empty()) {
            while (!back.empty()) {
                auto [v, w] = back.top(); back.pop();
                w = front.empty() ? v : f(front.top().second, v);
                front.emplace(v, w);
            }
        }
        front.pop();
    }

    template<class T>
    T fold(const function<T(R, R)> &q) {  
        auto rf = front.empty() ? id : front.top().second;
        auto rb = back.empty() ? id : back.top().second;
        return q(rf, rb); 
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    using D = long long;
    function<D(D, D)> g = [&](D a, D b) {
        if (b == 0LL) return a;
        if (a % b) return g(b, a % b);
        return b;
    };
    SlidingWindow<D, D, decltype(g)> SW(g, 0LL);
    long long Ans = 0;
    for (int i = 0; i < N; i++) {
        long long A; cin >> A;
        SW.emplace(A);
        while (!SW.empty() && SW.fold(g) == 1) {
            Ans += N - i;
            SW.pop();
        }
    }
    cout << Ans << endl;
    return 0;
}
0