結果

問題 No.1036 Make One With GCD 2
ユーザー KKT89KKT89
提出日時 2021-08-02 20:51:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 2,003 bytes
コンパイル時間 3,135 ms
コンパイル使用メモリ 220,172 KB
実行使用メモリ 17,112 KB
最終ジャッジ日時 2023-10-14 20:53:52
合計ジャッジ時間 10,647 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
11,380 KB
testcase_01 AC 102 ms
12,108 KB
testcase_02 AC 124 ms
17,112 KB
testcase_03 AC 15 ms
5,912 KB
testcase_04 AC 25 ms
8,748 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 39 ms
7,580 KB
testcase_08 AC 32 ms
6,948 KB
testcase_09 AC 138 ms
11,836 KB
testcase_10 AC 131 ms
10,412 KB
testcase_11 AC 138 ms
11,044 KB
testcase_12 AC 126 ms
10,412 KB
testcase_13 AC 223 ms
11,304 KB
testcase_14 AC 219 ms
11,388 KB
testcase_15 AC 207 ms
10,884 KB
testcase_16 AC 207 ms
12,176 KB
testcase_17 AC 218 ms
11,116 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 204 ms
10,672 KB
testcase_23 AC 149 ms
9,208 KB
testcase_24 AC 218 ms
10,916 KB
testcase_25 AC 206 ms
11,612 KB
testcase_26 AC 211 ms
10,724 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 105 ms
16,392 KB
testcase_39 AC 123 ms
11,976 KB
testcase_40 AC 150 ms
8,308 KB
testcase_41 AC 144 ms
11,328 KB
testcase_42 AC 141 ms
12,768 KB
testcase_43 AC 136 ms
11,300 KB
testcase_44 AC 133 ms
12,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}

long long binary_gcd(long long a, long long b) {
    if (a == 0) return b;
    if (b == 0) return a;
    const int n = __builtin_ctzll(a | b);
    a >>= __builtin_ctzll(a);
    while (b > 0) {
        b >>= __builtin_ctzll(b);
        if (a > b) swap(a, b);
        b -= a;
    }
    return a << n;
}

template<class S, S (*op)(S, S), S (*e)()> struct swag {
    std::stack<std::pair<S, S>> front_stack, back_stack;
    swag() {
        front_stack.emplace(e(), e());
        back_stack.emplace(e(), e());
    }
    bool empty() const { return front_stack.size() == 1 && back_stack.size() == 1; }
    size_t size() const { return front_stack.size() + back_stack.size() - 2; }
    S fold() const { return op(front_stack.top().second, back_stack.top().second); }
    void push(const S& x) { back_stack.emplace(x, op(back_stack.top().second, x)); }
    void pop() {
        assert(front_stack.size() > 1 || back_stack.size() > 1);
        if (front_stack.size() > 1) {
            front_stack.pop();
        } else {
            while (back_stack.size() > 2) {
                front_stack.emplace(back_stack.top().first, op(front_stack.top().second, back_stack.top().first));
                back_stack.pop();
            }
            back_stack.pop();
        }
    }
};

long long e() { return 0; }

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<ll> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    ll res=0;
    a.push_back(1);
    swag<ll, binary_gcd, e> q;
    for (int l = 0, r = 0; l < n; ++l) {
        while(r <= n and q.fold() != 1) {
            q.push(a[r]);
            r++;
        }
        if(r <= n) res += n-(r-1);
        q.pop();
    }
    printf("%lld\n",res);
}
0