結果
問題 |
No.1036 Make One With GCD 2
|
ユーザー |
![]() |
提出日時 | 2020-05-01 21:28:49 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 472 ms / 2,000 ms |
コード長 | 2,431 bytes |
コンパイル時間 | 3,738 ms |
コンパイル使用メモリ | 207,376 KB |
実行使用メモリ | 15,636 KB |
最終ジャッジ日時 | 2024-09-16 14:18:33 |
合計ジャッジ時間 | 14,716 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<long long, long long>; #define rep(i, n) for(int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() constexpr char ln = '\n'; constexpr long long MOD = 1000000007LL; //constexpr long long MOD = 998244353LL; template<class T, class U> inline bool chmax(T &a, U b) { if (a < b) { a = b; return true;} return false; } template<class T, class U> inline bool chmin(T &a, U b) { if (a > b) { a = b; return true;} return false; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// template<typename T, typename Monoid> struct SlidingWindowAggregation { T unity; Monoid merge; stack<pair<T, T>> front, back; SlidingWindowAggregation(Monoid f, T id) : merge(f), unity(id) {} bool empty() const {return front.empty() and back.empty();} int size() const {return front.size() + back.size();} T fold_all() const { T vf = front.empty() ? unity : front.top().second; T vb = back.empty() ? unity : back.top().second; return merge(vf,vb); } void emplace(const T &val) { if (back.empty()) { back.emplace(val,val); } else { back.emplace(val,merge(back.top().second,val)); } } void pop() { assert(!empty()); if (front.empty()) { front.emplace(back.top().first,back.top().first); back.pop(); while (!back.empty()) { front.emplace(back.top().first,merge(front.top().second,back.top().first)); back.pop(); } } front.pop(); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; vector<ll> A(N); rep(i,N) cin >> A[i]; auto f=[](ll a, ll b) {return __gcd(a,b);}; const ll id = 0; SlidingWindowAggregation<ll, decltype(f)> SWAG(f,id); int r = 0; ll ans = N*(N+1)/2; rep(l,N) { while (r < N and SWAG.fold_all() != 1) { SWAG.emplace(A[r]); r++; } ans -= SWAG.fold_all() == 1 ? r-l-1 : r-l; SWAG.pop(); } cout << ans << ln; }