結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 148 ms
7,796 KB
testcase_01 AC 119 ms
6,944 KB
testcase_02 AC 119 ms
15,296 KB
testcase_03 AC 13 ms
4,632 KB
testcase_04 AC 23 ms
5,584 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 40 ms
4,556 KB
testcase_08 AC 33 ms
4,448 KB
testcase_09 AC 175 ms
6,996 KB
testcase_10 AC 164 ms
6,400 KB
testcase_11 AC 178 ms
6,940 KB
testcase_12 AC 164 ms
6,680 KB
testcase_13 AC 287 ms
6,760 KB
testcase_14 AC 290 ms
6,868 KB
testcase_15 AC 274 ms
6,544 KB
testcase_16 AC 275 ms
6,704 KB
testcase_17 AC 282 ms
6,800 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,356 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 268 ms
6,372 KB
testcase_23 AC 199 ms
5,708 KB
testcase_24 AC 279 ms
6,716 KB
testcase_25 AC 256 ms
6,408 KB
testcase_26 AC 263 ms
6,448 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 1 ms
4,372 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,356 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 118 ms
15,272 KB
testcase_39 AC 158 ms
6,904 KB
testcase_40 AC 199 ms
5,728 KB
testcase_41 AC 187 ms
6,944 KB
testcase_42 AC 188 ms
6,980 KB
testcase_43 AC 181 ms
8,568 KB
testcase_44 AC 185 ms
7,716 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