結果
| 問題 | No.1036 Make One With GCD 2 |
| コンテスト | |
| ユーザー |
lorent_kyopro
|
| 提出日時 | 2020-12-25 16:46:02 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 244 ms / 2,000 ms |
| コード長 | 2,109 bytes |
| 記録 | |
| コンパイル時間 | 2,823 ms |
| コンパイル使用メモリ | 202,916 KB |
| 最終ジャッジ日時 | 2025-01-17 07:17:03 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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)>
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;
a.push_back(1);
swag<long long, binary_gcd> q;
long long ans = 0;
q.push(a.front());
for (int l = 0, r = 0; l < n; ++l) {
while (q.empty() || q.fold() != 1) q.push(a[++r]);
ans += n - r;
q.pop();
}
cout << ans << '\n';
}
lorent_kyopro