結果

問題 No.1036 Make One With GCD 2
ユーザー firiexp
提出日時 2020-04-25 17:48:16
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 478 ms / 2,000 ms
コード長 1,574 bytes
コンパイル時間 4,559 ms
コンパイル使用メモリ 139,996 KB
最終ジャッジ日時 2025-01-10 01:23:16
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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;
    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