結果

問題 No.1036 Make One With GCD 2
ユーザー firiexpfiriexp
提出日時 2020-04-25 17:48:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 373 ms / 2,000 ms
コード長 1,574 bytes
コンパイル時間 1,104 ms
コンパイル使用メモリ 112,396 KB
実行使用メモリ 15,368 KB
最終ジャッジ日時 2023-10-14 19:57:28
合計ジャッジ時間 10,520 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
8,636 KB
testcase_01 AC 79 ms
7,040 KB
testcase_02 AC 103 ms
15,368 KB
testcase_03 AC 17 ms
4,372 KB
testcase_04 AC 29 ms
5,792 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 33 ms
4,612 KB
testcase_08 AC 26 ms
4,404 KB
testcase_09 AC 220 ms
6,892 KB
testcase_10 AC 205 ms
6,640 KB
testcase_11 AC 224 ms
7,012 KB
testcase_12 AC 211 ms
6,752 KB
testcase_13 AC 370 ms
6,624 KB
testcase_14 AC 370 ms
7,040 KB
testcase_15 AC 357 ms
6,628 KB
testcase_16 AC 350 ms
6,776 KB
testcase_17 AC 373 ms
6,724 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 1 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,368 KB
testcase_22 AC 347 ms
6,416 KB
testcase_23 AC 255 ms
5,720 KB
testcase_24 AC 373 ms
6,720 KB
testcase_25 AC 330 ms
6,396 KB
testcase_26 AC 348 ms
6,396 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 1 ms
4,368 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 1 ms
4,368 KB
testcase_31 AC 1 ms
4,368 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 1 ms
4,372 KB
testcase_34 AC 1 ms
4,368 KB
testcase_35 AC 1 ms
4,372 KB
testcase_36 AC 1 ms
4,368 KB
testcase_37 AC 2 ms
4,368 KB
testcase_38 AC 108 ms
15,084 KB
testcase_39 AC 109 ms
6,884 KB
testcase_40 AC 258 ms
5,716 KB
testcase_41 AC 273 ms
7,468 KB
testcase_42 AC 264 ms
7,256 KB
testcase_43 AC 237 ms
12,144 KB
testcase_44 AC 255 ms
9,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

ll gcd_(ll x, ll y){
    if(x < y) swap(x, y);
    while(y){
        x %= y;
        swap(x, y);
    }
    return x;
}

template<class G>
class SWAG {
    using T = typename G::T;
    vector<T> in, out, insum, outsum;
public:
    SWAG() : in(0), out(0), insum(1, G::e()), outsum(1, G::e()) {}

    void push(const T& v){
        insum.push_back(G::f(insum.back(), v));
        in.push_back(v);
    }

    void pop(){
        if(out.empty()){
            do {
                out.emplace_back(in.back());
                outsum.emplace_back(G::f(in.back(), outsum.back()));
                in.pop_back(); insum.pop_back();
            }while(!in.empty());
        }
        out.pop_back(); outsum.pop_back();
    }

    T fold(){
        return G::f(outsum.back(), insum.back());
    }
};

struct Monoid {
    using T = ll;
    static T f(T a, T b) { return gcd_(a, b); }
    static T e() { return 0; }
};

int main() {
    int n;
    cin >> n;
    vector<ll> v(n);
    for (auto &&k : v) scanf("%lld", &k);
    SWAG<Monoid> Q;
    ll ans = 0, val = 0;
    for (int i = 0; i < n; ++i) {
        Q.push(v[i]);
        while(Q.fold() == 1) Q.pop(), val++;
        ans += val;
    }
    cout << ans << "\n";
    return 0;
}
0