結果
問題 |
No.1036 Make One With GCD 2
|
ユーザー |
![]() |
提出日時 | 2020-12-25 16:49:40 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 242 ms / 2,000 ms |
コード長 | 2,150 bytes |
コンパイル時間 | 1,945 ms |
コンパイル使用メモリ | 203,384 KB |
最終ジャッジ日時 | 2025-01-17 07:17:20 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#include <bits/stdc++.h> using namespace std; __attribute__((constructor)) void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); } long long binary_gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; const int n = __builtin_ctzll(a | b); a >>= __builtin_ctzll(a); while (b > 0) { b >>= __builtin_ctzll(b); if (a > b) swap(a, b); b -= a; } return a << n; } template<class S, S (*op)(S, S), S (*e)()> struct swag { struct node { S value, sum; node(const S& value, const S& sum) : value(value), sum(sum) {} }; stack<node> front_stack, back_stack; swag() {} bool empty() const { return front_stack.empty() && back_stack.empty();} size_t size() const { return front_stack.size() + back_stack.size();} S fold() const { if (empty()) return e(); else if (front_stack.empty()) return back_stack.top().sum; else if (back_stack.empty()) return front_stack.top().sum; else return op(front_stack.top().sum, back_stack.top().sum); } void push(const S &x) { if (back_stack.empty()) back_stack.emplace(x, x); else { S s{op(back_stack.top().sum, x)}; back_stack.emplace(x, s); } } void pop() { assert(!empty()); if (front_stack.empty()) { front_stack.emplace(back_stack.top().value, back_stack.top().value); back_stack.pop(); while (!back_stack.empty()) { S s{op(back_stack.top().value, front_stack.top().sum)}; front_stack.emplace(back_stack.top().value, s); back_stack.pop(); } } front_stack.pop(); } }; long long e() { return 0; } int main() { int n; cin >> n; vector<long long> a(n); for (auto& ai : a) cin >> ai; a.push_back(1); swag<long long, binary_gcd, e> q; long long ans = 0; q.push(a.front()); for (int l = 0, r = 0; l < n; ++l) { while (q.fold() != 1) q.push(a[++r]); ans += n - r; q.pop(); } cout << ans << '\n'; }