結果

問題 No.1036 Make One With GCD 2
ユーザー minatominato
提出日時 2020-05-01 21:28:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 492 ms / 2,000 ms
コード長 2,431 bytes
コンパイル時間 4,504 ms
コンパイル使用メモリ 204,924 KB
実行使用メモリ 15,660 KB
最終ジャッジ日時 2023-10-14 20:26:39
合計ジャッジ時間 15,382 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
7,388 KB
testcase_01 AC 110 ms
6,900 KB
testcase_02 AC 131 ms
15,660 KB
testcase_03 AC 20 ms
4,348 KB
testcase_04 AC 34 ms
5,652 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 39 ms
4,648 KB
testcase_08 AC 33 ms
4,356 KB
testcase_09 AC 284 ms
6,948 KB
testcase_10 AC 266 ms
6,692 KB
testcase_11 AC 290 ms
6,968 KB
testcase_12 AC 268 ms
6,668 KB
testcase_13 AC 486 ms
6,692 KB
testcase_14 AC 492 ms
6,960 KB
testcase_15 AC 461 ms
6,848 KB
testcase_16 AC 467 ms
6,392 KB
testcase_17 AC 482 ms
6,760 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 457 ms
6,728 KB
testcase_23 AC 335 ms
5,660 KB
testcase_24 AC 473 ms
6,668 KB
testcase_25 AC 431 ms
6,532 KB
testcase_26 AC 449 ms
6,556 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 2 ms
4,480 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 131 ms
15,528 KB
testcase_39 AC 129 ms
6,968 KB
testcase_40 AC 335 ms
5,684 KB
testcase_41 AC 463 ms
6,956 KB
testcase_42 AC 464 ms
6,852 KB
testcase_43 AC 397 ms
9,136 KB
testcase_44 AC 438 ms
7,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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