結果

問題 No.1036 Make One With GCD 2
ユーザー micheeeeell1001micheeeeell1001
提出日時 2020-04-25 17:26:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 311 ms / 2,000 ms
コード長 2,285 bytes
コンパイル時間 3,741 ms
コンパイル使用メモリ 236,388 KB
実行使用メモリ 15,416 KB
最終ジャッジ日時 2023-10-14 19:56:14
合計ジャッジ時間 11,381 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 167 ms
7,784 KB
testcase_01 AC 138 ms
6,956 KB
testcase_02 AC 137 ms
15,356 KB
testcase_03 AC 14 ms
4,668 KB
testcase_04 AC 22 ms
5,768 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 44 ms
4,592 KB
testcase_08 AC 36 ms
4,356 KB
testcase_09 AC 196 ms
6,980 KB
testcase_10 AC 184 ms
6,476 KB
testcase_11 AC 202 ms
6,992 KB
testcase_12 AC 186 ms
6,712 KB
testcase_13 AC 308 ms
6,688 KB
testcase_14 AC 311 ms
6,888 KB
testcase_15 AC 292 ms
6,516 KB
testcase_16 AC 294 ms
6,804 KB
testcase_17 AC 303 ms
6,752 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 288 ms
6,352 KB
testcase_23 AC 211 ms
5,744 KB
testcase_24 AC 298 ms
6,696 KB
testcase_25 AC 273 ms
6,424 KB
testcase_26 AC 283 ms
6,452 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 134 ms
15,416 KB
testcase_39 AC 178 ms
7,172 KB
testcase_40 AC 211 ms
5,844 KB
testcase_41 AC 205 ms
6,896 KB
testcase_42 AC 206 ms
6,976 KB
testcase_43 AC 198 ms
8,652 KB
testcase_44 AC 204 ms
7,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0