結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
|
提出日時 | 2020-06-02 23:12:31 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 613 ms / 2,000 ms |
コード長 | 1,639 bytes |
コンパイル時間 | 3,665 ms |
コンパイル使用メモリ | 201,980 KB |
最終ジャッジ日時 | 2025-01-10 20:41:13 |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#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; }