結果

問題 No.1036 Make One With GCD 2
ユーザー merom686merom686
提出日時 2020-04-24 22:36:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 343 ms / 2,000 ms
コード長 1,433 bytes
コンパイル時間 885 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 7,144 KB
最終ジャッジ日時 2023-10-14 19:27:11
合計ジャッジ時間 10,047 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 112 ms
6,916 KB
testcase_01 AC 76 ms
6,912 KB
testcase_02 AC 111 ms
6,988 KB
testcase_03 AC 14 ms
4,372 KB
testcase_04 AC 22 ms
5,488 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 31 ms
4,572 KB
testcase_08 AC 25 ms
4,376 KB
testcase_09 AC 196 ms
6,980 KB
testcase_10 AC 183 ms
6,688 KB
testcase_11 AC 199 ms
7,144 KB
testcase_12 AC 184 ms
6,552 KB
testcase_13 AC 338 ms
6,656 KB
testcase_14 AC 343 ms
6,988 KB
testcase_15 AC 320 ms
6,632 KB
testcase_16 AC 325 ms
6,716 KB
testcase_17 AC 334 ms
6,660 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 317 ms
6,684 KB
testcase_23 AC 234 ms
5,568 KB
testcase_24 AC 329 ms
6,668 KB
testcase_25 AC 300 ms
6,408 KB
testcase_26 AC 313 ms
6,376 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 2 ms
4,356 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 107 ms
6,908 KB
testcase_39 AC 117 ms
6,884 KB
testcase_40 AC 234 ms
5,552 KB
testcase_41 AC 277 ms
6,868 KB
testcase_42 AC 277 ms
6,876 KB
testcase_43 AC 242 ms
6,820 KB
testcase_44 AC 262 ms
6,868 KB
権限があれば一括ダウンロードができます

ソースコード

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