結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
7,736 KB
testcase_01 AC 87 ms
7,040 KB
testcase_02 AC 105 ms
15,196 KB
testcase_03 AC 17 ms
4,468 KB
testcase_04 AC 29 ms
5,716 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,356 KB
testcase_07 AC 34 ms
4,636 KB
testcase_08 AC 28 ms
4,372 KB
testcase_09 AC 229 ms
6,928 KB
testcase_10 AC 208 ms
6,672 KB
testcase_11 AC 225 ms
6,928 KB
testcase_12 AC 208 ms
6,656 KB
testcase_13 AC 385 ms
6,676 KB
testcase_14 AC 377 ms
7,036 KB
testcase_15 AC 365 ms
6,484 KB
testcase_16 AC 359 ms
6,572 KB
testcase_17 AC 381 ms
6,680 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,356 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 353 ms
6,508 KB
testcase_23 AC 267 ms
5,712 KB
testcase_24 AC 381 ms
6,700 KB
testcase_25 AC 355 ms
6,480 KB
testcase_26 AC 364 ms
6,444 KB
testcase_27 AC 1 ms
4,356 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 2 ms
4,356 KB
testcase_33 AC 2 ms
4,356 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 110 ms
15,536 KB
testcase_39 AC 123 ms
6,896 KB
testcase_40 AC 269 ms
5,652 KB
testcase_41 AC 276 ms
7,472 KB
testcase_42 AC 280 ms
7,028 KB
testcase_43 AC 241 ms
9,468 KB
testcase_44 AC 259 ms
8,100 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;
    stack<T> in, out, insum, outsum;
public:
    SWAG() {
        insum.emplace(G::e());
        outsum.emplace(G::e());
    }

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

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

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

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