結果

問題 No.1036 Make One With GCD 2
ユーザー potoooooooopotoooooooo
提出日時 2020-06-02 22:45:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,641 bytes
コンパイル時間 4,026 ms
コンパイル使用メモリ 205,732 KB
実行使用メモリ 11,188 KB
最終ジャッジ日時 2023-08-16 05:30:02
合計ジャッジ時間 19,391 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 121 ms
11,184 KB
testcase_03 AC 17 ms
4,380 KB
testcase_04 AC 28 ms
4,384 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 123 ms
11,188 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

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