結果
問題 |
No.1036 Make One With GCD 2
|
ユーザー |
![]() |
提出日時 | 2020-04-25 17:26:17 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 344 ms / 2,000 ms |
コード長 | 2,285 bytes |
コンパイル時間 | 2,973 ms |
コンパイル使用メモリ | 229,376 KB |
最終ジャッジ日時 | 2025-01-10 01:19:32 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#define _GLIBCXX_DEBUG #include<bits/stdc++.h> using namespace std; #define rep(i,x) for(ll i = 0; i < (ll)(x); i++) #define rrep(i,x) for(ll i = (ll)(x)-1;0 <= i; i--) #define reps(i,x) for(ll i = 1; i < (ll)(x)+1; i++) #define rreps(i,x) for(ll i = (ll)(x); 1 <= i; i--) #define debug(x) cerr << #x << ": " << (x) << "\n"; #define all(x) (x).begin(), (x).end() typedef long long ll; typedef long double ld; typedef pair<int,int> P; typedef pair<ll,ll> Pll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef vector<vector<vector<ll>>> vvvl; const ll INF = numeric_limits<ll>::max()/4; const int n_max = 1e5+10; #define int ll template<typename Monoid> struct SlidingWindowAggregation { using F = function< Monoid(Monoid, Monoid) >; stack< Monoid > front,back; stack< Monoid > sum_front; Monoid sum_back; const F f; const Monoid M1; SlidingWindowAggregation (const F f, const Monoid &M1) : f(f), M1(M1) { sum_front.push(M1); sum_back = M1; } void push(Monoid x) { back.push(x); sum_back = f(sum_back, x); } void pop() { if(front.empty()) { while(! back.empty()) { front.push(back.top()); sum_front.push(f(front.top(), sum_front.top())); back.pop(); } sum_back = M1; } assert(! front.empty()); front.pop(); sum_front.pop(); } Monoid accumulate() const { if(front.size() + back.size() == 0)return 0; return f(sum_front.top(), sum_back); } bool empty() const { return front.empty() && back.empty(); } size_t size() const { return front.size() + back.size(); } }; signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); ll n; cin >> n; vector<ll> a(n); rep(i,n) cin >> a[i]; auto f = [](ll a, ll b){return gcd(a, b);}; SlidingWindowAggregation<ll> swag(f, 0); ll left = 0, right = 0; ll ans = 0; for(;left < n; left++){ while(right < n && gcd(swag.accumulate(), a[right]) > 1){ swag.push(a[right++]); } ans += n - right; if(right == left) ++right; else swag.pop(); } cout << ans << endl; }