結果

問題 No.1036 Make One With GCD 2
ユーザー MisterMister
提出日時 2021-01-23 20:01:05
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 1,617 bytes
コンパイル時間 753 ms
コンパイル使用メモリ 81,804 KB
実行使用メモリ 11,808 KB
最終ジャッジ日時 2023-10-14 20:43:34
合計ジャッジ時間 8,646 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
4,372 KB
testcase_01 AC 127 ms
4,372 KB
testcase_02 AC 115 ms
11,632 KB
testcase_03 AC 14 ms
4,376 KB
testcase_04 AC 24 ms
4,372 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 47 ms
4,368 KB
testcase_08 AC 39 ms
4,372 KB
testcase_09 AC 157 ms
4,372 KB
testcase_10 AC 147 ms
4,372 KB
testcase_11 AC 161 ms
4,372 KB
testcase_12 AC 148 ms
4,372 KB
testcase_13 AC 258 ms
4,372 KB
testcase_14 AC 259 ms
4,372 KB
testcase_15 AC 245 ms
4,372 KB
testcase_16 AC 247 ms
4,372 KB
testcase_17 AC 254 ms
4,368 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 241 ms
4,368 KB
testcase_23 AC 178 ms
4,376 KB
testcase_24 AC 251 ms
4,372 KB
testcase_25 AC 228 ms
4,372 KB
testcase_26 AC 237 ms
4,372 KB
testcase_27 AC 2 ms
4,372 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 1 ms
4,372 KB
testcase_30 AC 1 ms
4,372 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 2 ms
4,372 KB
testcase_34 AC 2 ms
4,368 KB
testcase_35 AC 1 ms
4,500 KB
testcase_36 AC 1 ms
4,372 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 111 ms
11,808 KB
testcase_39 AC 134 ms
4,372 KB
testcase_40 AC 179 ms
4,376 KB
testcase_41 AC 147 ms
4,372 KB
testcase_42 AC 147 ms
4,372 KB
testcase_43 AC 146 ms
5,472 KB
testcase_44 AC 147 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/DataStructure/sliding_window_aggregation.hpp"

#include <stack>

template <class M>
struct SlidingWindowAggregation {
    using T = typename M::T;

    std::stack<std::pair<T, T>> front, back;

    explicit SlidingWindowAggregation() = default;

    void push(const T& x) {
        T acc = M::op(back.empty() ? M::id : back.top().second, x);
        back.emplace(x, acc);
    }

    void pop() {
        if (front.empty()) {
            T acc = M::id;
            while (!back.empty()) {
                acc = M::op(back.top().first, acc);
                front.emplace(back.top().first, acc);
                back.pop();
            }
        }
        front.pop();
    }

    T fold() {
        return M::op((front.empty() ? M::id : front.top().second),
                     (back.empty() ? M::id : back.top().second));
    }
};
#line 2 "main.cpp"
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;
using lint = long long;

struct GCD {
    using T = lint;
    static T op(T a, T b) { return gcd(a, b); }
    static const T id = 0;
};

void solve() {
    int n;
    cin >> n;

    lint ans = 0;
    SlidingWindowAggregation<GCD> swa;

    int r = 0;
    for (int l = 0; l < n; ++l) {
        while (swa.fold() != 1 && r < n) {
            lint x;
            cin >> x;
            swa.push(x);
            ++r;
        }

        if (swa.fold() == 1) ans += n - r + 1;
        swa.pop();
    }

    cout << ans << "\n";
}

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

    solve();

    return 0;
}
0