結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
![]() |
提出日時 | 2020-06-09 17:20:36 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 597 ms / 2,000 ms |
コード長 | 1,865 bytes |
コンパイル時間 | 2,614 ms |
コンパイル使用メモリ | 203,464 KB |
最終ジャッジ日時 | 2025-01-11 00:31:24 |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#include <bits/stdc++.h> using namespace std; template<class T> struct queue_aggregation { using F = function<T(T, T)>; using value_type = T; private : struct node { value_type val, sum; node (const value_type &val, const value_type &sum) : val(val), sum(sum) { } }; const F f; stack<node> front_stack, back_stack; public : queue_aggregation (const F &f) : f(f), front_stack(), back_stack() { } constexpr bool empty () const noexcept { return (front_stack.empty() and back_stack.empty()); } constexpr size_t size () const noexcept { return (front_stack.size() + back_stack.size()); } void push (const value_type &x) { if (back_stack.empty()) { back_stack.emplace(x, x); } else { value_type s = f(back_stack.top().sum, x); back_stack.emplace(x, s); } } void pop () { assert(not empty()); if (front_stack.empty()) { front_stack.emplace(back_stack.top().val, back_stack.top().val); back_stack.pop(); while (not back_stack.empty()) { value_type s = f(back_stack.top().val, front_stack.top().sum); front_stack.emplace(back_stack.top().val, s); back_stack.pop(); } } front_stack.pop(); } value_type fold () { assert(not empty()); if (front_stack.empty()) { return (back_stack.top().sum); } else if (back_stack.empty()) { return (front_stack.top().sum); } else { return (f(front_stack.top().sum, back_stack.top().sum)); } } }; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } queue_aggregation<long long> que([](long long a, long long b) { return gcd(a, b); }); long long ans = 0; for (int l = 0, r = 0; l < n; l++) { while ((que.empty() or que.fold() > 1) and r < n) que.push(a[r++]); if (not que.empty() and que.fold() == 1) ans += (n - r + 1); que.pop(); } cout << ans << '\n'; return 0; }