結果

問題 No.1036 Make One With GCD 2
ユーザー firiexp
提出日時 2020-04-25 17:53:46
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 425 ms / 2,000 ms
コード長 1,545 bytes
コンパイル時間 3,988 ms
コンパイル使用メモリ 142,772 KB
最終ジャッジ日時 2025-01-10 01:24:16
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

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