結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
|
提出日時 | 2020-04-25 02:11:11 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 399 ms / 2,000 ms |
コード長 | 2,097 bytes |
コンパイル時間 | 1,828 ms |
コンパイル使用メモリ | 174,616 KB |
実行使用メモリ | 15,296 KB |
最終ジャッジ日時 | 2024-09-16 13:35:23 |
合計ジャッジ時間 | 11,271 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;// https://scrapbox.io/data-structures/Sliding_Window_Aggregationtemplate<class SemiGroup>struct SWAG {using T = typename SemiGroup::T;struct Node {T val, sum;Node(const T& v, const T& s) : val(v), sum(s) {}};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();}T fold_all() {assert(!empty());if (front_stack.empty()) return back_stack.top().sum;else if (back_stack.empty()) return front_stack.top().sum;else return SemiGroup::merge(front_stack.top().sum, back_stack.top().sum);}void push(const T& x) {if (back_stack.empty()) {back_stack.emplace(x, x);} else {T sum = SemiGroup::merge(back_stack.top().sum, x);back_stack.emplace(x, sum);}}void pop() {assert(!empty());if (front_stack.empty()) {front_stack.emplace(back_stack.top().val, back_stack.top().val);back_stack.pop();while (!back_stack.empty()) {T sum = SemiGroup::merge(front_stack.top().sum, back_stack.top().val);front_stack.emplace(back_stack.top().val, sum);back_stack.pop();}}front_stack.pop();}};// 以下、SemiGroupの例template <class U = ll>struct RMQSG {using T = U;static T merge(T a, T b) { return __gcd(a, b); }};int main() {cin.tie(nullptr);ios::sync_with_stdio(false);int n;cin >> n;vector<ll> a(n);for (int i = 0; i < n; ++i) {cin >> a[i];}SWAG< RMQSG<> > swag;ll ans = 0;for (int i = 0; i < n; ++i) {swag.push(a[i]);while (!swag.empty() && swag.fold_all() == 1) swag.pop();ans += i + 1 - swag.size();}cout << ans << "\n";return 0;}