結果
| 問題 | 
                            No.1036 Make One With GCD 2
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2021-01-23 20:01:05 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 264 ms / 2,000 ms | 
| コード長 | 1,617 bytes | 
| コンパイル時間 | 849 ms | 
| コンパイル使用メモリ | 78,916 KB | 
| 最終ジャッジ日時 | 2025-01-18 07:40:04 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 41 | 
ソースコード
#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;
}