結果
| 問題 |
No.1036 Make One With GCD 2
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-25 17:17:02 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 389 ms / 2,000 ms |
| コード長 | 1,884 bytes |
| コンパイル時間 | 2,473 ms |
| コンパイル使用メモリ | 203,056 KB |
| 最終ジャッジ日時 | 2025-01-10 01:18:47 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 41 |
ソースコード
#include <bits/stdc++.h> // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif // clang-format on
/**
* author: knshnb
* created: Sat Apr 25 15:38:40 JST 2020
**/
template <class T, class F> struct SlidingWindowAggregation {
const F op;
const T e;
std::stack<std::pair<T, T>> st1, st2; // {val, acc}
SlidingWindowAggregation(F op_, T e_) : op(op_), e(e_) { st1.emplace(e, e), st2.emplace(e, e); }
int size() { return st1.size() + st2.size() - 2; }
void push(T x) {
T acc = op(st2.top().second, x);
st2.emplace(x, acc);
}
void pop() {
assert(st1.size() > 1 || st2.size() > 1);
if (st1.size() > 1) {
st1.pop();
} else {
while (st2.size() > 2) {
T acc = op(st1.top().second, st2.top().first);
st1.emplace(st2.top().first, acc);
st2.pop();
}
st2.pop();
}
}
T fold_all() {
return op(st1.top().second, st2.top().second);
}
};
template <class T, class F> auto make_swag(F op, T e_) { return SlidingWindowAggregation<T, F>(op, e_); }
signed main() {
Int n;
std::cin >> n;
std::vector<Int> a(n);
REP(i, n) std::cin >> a[i];
a.push_back(1);
auto swag = make_swag<Int>([](Int x, Int y) { return std::gcd(x, y); }, 0);
Int ans = 0, j = -1;
REP(i, n) {
while (swag.fold_all() != 1) {
swag.push(a[++j]);
}
ans += n - j;
swag.pop();
}
std::cout << ans << std::endl;
}