結果

問題 No.1036 Make One With GCD 2
ユーザー knshnbknshnb
提出日時 2020-04-25 17:17:02
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 1,884 bytes
コンパイル時間 2,136 ms
コンパイル使用メモリ 208,912 KB
実行使用メモリ 17,340 KB
最終ジャッジ日時 2023-10-14 19:56:01
合計ジャッジ時間 9,630 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
11,068 KB
testcase_01 AC 112 ms
11,792 KB
testcase_02 AC 139 ms
17,340 KB
testcase_03 AC 15 ms
6,084 KB
testcase_04 AC 25 ms
8,476 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,356 KB
testcase_07 AC 40 ms
6,700 KB
testcase_08 AC 33 ms
6,720 KB
testcase_09 AC 138 ms
11,636 KB
testcase_10 AC 129 ms
10,612 KB
testcase_11 AC 140 ms
11,500 KB
testcase_12 AC 131 ms
11,412 KB
testcase_13 AC 224 ms
11,772 KB
testcase_14 AC 227 ms
11,268 KB
testcase_15 AC 212 ms
10,708 KB
testcase_16 AC 215 ms
11,404 KB
testcase_17 AC 222 ms
10,960 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,356 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 210 ms
11,008 KB
testcase_23 AC 156 ms
9,348 KB
testcase_24 AC 219 ms
11,864 KB
testcase_25 AC 200 ms
10,040 KB
testcase_26 AC 207 ms
10,244 KB
testcase_27 AC 2 ms
4,356 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 2 ms
4,356 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,356 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 112 ms
16,388 KB
testcase_39 AC 136 ms
11,272 KB
testcase_40 AC 154 ms
8,448 KB
testcase_41 AC 152 ms
11,312 KB
testcase_42 AC 152 ms
10,756 KB
testcase_43 AC 150 ms
11,496 KB
testcase_44 AC 151 ms
11,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>  // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef _MY_DEBUG
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Sat Apr 25 15:38:40 JST 2020
 **/

template <class T, class F> struct SlidingWindowAggregation {
    const F op;
    const T e;
    std::stack<std::pair<T, T>> st1, st2;  // {val, acc}
    SlidingWindowAggregation(F op_, T e_) : op(op_), e(e_) { st1.emplace(e, e), st2.emplace(e, e); }
    int size() { return st1.size() + st2.size() - 2; }
    void push(T x) {
        T acc = op(st2.top().second, x);
        st2.emplace(x, acc);
    }
    void pop() {
        assert(st1.size() > 1 || st2.size() > 1);
        if (st1.size() > 1) {
            st1.pop();
        } else {
            while (st2.size() > 2) {
                T acc = op(st1.top().second, st2.top().first);
                st1.emplace(st2.top().first, acc);
                st2.pop();
            }
            st2.pop();
        }
    }
    T fold_all() {
        return op(st1.top().second, st2.top().second);
    }
};
template <class T, class F> auto make_swag(F op, T e_) { return SlidingWindowAggregation<T, F>(op, e_); }

signed main() {
    Int n;
    std::cin >> n;
    std::vector<Int> a(n);
    REP(i, n) std::cin >> a[i];
    a.push_back(1);
    auto swag = make_swag<Int>([](Int x, Int y) { return std::gcd(x, y); }, 0);
    Int ans = 0, j = -1;
    REP(i, n) {
        while (swag.fold_all() != 1) {
            swag.push(a[++j]);
        }
        ans += n - j;
        swag.pop();
    }
    std::cout << ans << std::endl;
}
0