結果
問題 |
No.1036 Make One With GCD 2
|
ユーザー |
![]() |
提出日時 | 2020-12-25 16:29:41 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 276 ms / 2,000 ms |
コード長 | 2,144 bytes |
コンパイル時間 | 2,095 ms |
コンパイル使用メモリ | 202,224 KB |
最終ジャッジ日時 | 2025-01-17 07:16:47 |
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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; int n = __builtin_ctzll(a); int m = __builtin_ctzll(b); a >>= n; b >>= m; while (a != b) { if (a < b) swap(a, b); int k = __builtin_ctzll(a - b); a = (a - b) >> k; } return a << min(n, m); } template<class S, S (*op)(S, S)> 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 { assert(!empty()); 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(); } }; int main() { int n; cin >> n; vector<long long> a(n); for (auto& ai : a) cin >> ai; swag<long long, binary_gcd> q; long long ans = 0; for (int l = 0, r = 0; l < n; ++l) { while (r < n && (q.empty() || q.fold() != 1)) q.push(a[r++]); if (q.fold() == 1) ans += n - r + 1; q.pop(); } cout << ans << '\n'; }