結果

問題 No.1036 Make One With GCD 2
ユーザー merom686
提出日時 2020-04-24 22:36:06
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 389 ms / 2,000 ms
コード長 1,433 bytes
コンパイル時間 1,209 ms
コンパイル使用メモリ 92,800 KB
最終ジャッジ日時 2025-01-10 00:10:34
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

ll gcd(ll n, ll m) {
    if (n < m) swap(n, m);
    while (m != 0) {
        ll r = n % m;
        n = m;
        m = r;
    }
    return n;
}

template <class T>
struct SWAG {
    SWAG(int n) : t(n), s(), i0(0), i1(0), i2(0) {}
    void push(const T &o) {
        t[i2++] = o;
        s = s * o;
    }
    void pop() {
        if (i0 == i1) {
            i1 = i2;
            for (int i = i1 - 2; i > i0; i--) {
                t[i] = t[i] * t[i + 1];
            }
            s = T();
        }
        i0++;
    }
    T fold_all() {
        return i0 == i1 ? s : t[i0] * s;
    }
    vector<T> t;
    T s;
    int i0, i1, i2;
};

struct Node {
    constexpr Node() : a(0) {}
    Node(ll a) : a(a) {}
    Node operator*(const Node &o) const {
        return Node(gcd(a, o.a));
    }
    ll a;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    SWAG<Node> sw(n);
    ll r = 0;

    for (int i0 = 0, i1 = 0; i0 < n; i0++) {
        while (i1 < n && sw.fold_all().a != 1) {
            ll a;
            cin >> a;
            sw.push(Node(a));
            i1++;
        }
        if (i1 == n && sw.fold_all().a != 1) i1++;
        r += n - i1 + 1;
        sw.pop();
    }

    cout << r << endl;

    return 0;
}
0