結果

問題 No.1036 Make One With GCD 2
ユーザー knshnbknshnb
提出日時 2020-04-26 13:36:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 229 ms / 2,000 ms
コード長 2,145 bytes
コンパイル時間 2,833 ms
コンパイル使用メモリ 207,516 KB
実行使用メモリ 17,256 KB
最終ジャッジ日時 2023-10-14 20:19:12
合計ジャッジ時間 10,338 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
11,500 KB
testcase_01 AC 112 ms
11,512 KB
testcase_02 AC 138 ms
16,376 KB
testcase_03 AC 15 ms
6,020 KB
testcase_04 AC 25 ms
8,276 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 40 ms
7,668 KB
testcase_08 AC 33 ms
5,648 KB
testcase_09 AC 139 ms
10,872 KB
testcase_10 AC 130 ms
11,504 KB
testcase_11 AC 142 ms
11,552 KB
testcase_12 AC 131 ms
10,320 KB
testcase_13 AC 227 ms
12,168 KB
testcase_14 AC 229 ms
11,008 KB
testcase_15 AC 215 ms
10,252 KB
testcase_16 AC 216 ms
10,760 KB
testcase_17 AC 223 ms
11,004 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 211 ms
10,604 KB
testcase_23 AC 155 ms
8,956 KB
testcase_24 AC 220 ms
11,176 KB
testcase_25 AC 200 ms
10,012 KB
testcase_26 AC 208 ms
10,776 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,356 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 2 ms
4,356 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 114 ms
17,256 KB
testcase_39 AC 135 ms
11,056 KB
testcase_40 AC 157 ms
9,060 KB
testcase_41 AC 155 ms
11,820 KB
testcase_42 AC 152 ms
11,544 KB
testcase_43 AC 151 ms
10,916 KB
testcase_44 AC 152 ms
12,924 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_); }

Int gcd(Int a, Int b) {
    if (a == 0) return b;
    if (b == 0) return a;
    const int s = __builtin_ctzll(a | b);
    a >>= __builtin_ctzll(a);
    while (b) {
        b >>= __builtin_ctzll(b);
        if (a > b) std::swap(a, b);
        b -= a;
    }
    return a << s;
}

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 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