結果

問題 No.1036 Make One With GCD 2
ユーザー minatominato
提出日時 2020-05-01 21:17:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,449 bytes
コンパイル時間 3,494 ms
コンパイル使用メモリ 203,796 KB
実行使用メモリ 15,700 KB
最終ジャッジ日時 2023-08-26 06:11:00
合計ジャッジ時間 17,393 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
    int 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);
    ll r = 0;
    ll ans = N*(N+1)/2;
    for (ll l = 0; l < N; l++) {
        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