結果

問題 No.1036 Make One With GCD 2
ユーザー rodearodea
提出日時 2020-04-28 19:06:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,105 ms / 2,000 ms
コード長 2,394 bytes
コンパイル時間 1,837 ms
コンパイル使用メモリ 124,636 KB
実行使用メモリ 19,272 KB
最終ジャッジ日時 2023-10-14 20:24:39
合計ジャッジ時間 21,215 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 679 ms
19,108 KB
testcase_01 AC 380 ms
19,164 KB
testcase_02 AC 243 ms
19,096 KB
testcase_03 AC 67 ms
10,192 KB
testcase_04 AC 120 ms
16,280 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 119 ms
10,460 KB
testcase_08 AC 98 ms
9,992 KB
testcase_09 AC 504 ms
19,032 KB
testcase_10 AC 466 ms
18,320 KB
testcase_11 AC 513 ms
19,164 KB
testcase_12 AC 472 ms
18,440 KB
testcase_13 AC 643 ms
18,680 KB
testcase_14 AC 651 ms
18,944 KB
testcase_15 AC 609 ms
18,376 KB
testcase_16 AC 615 ms
18,344 KB
testcase_17 AC 632 ms
18,568 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 4 ms
4,372 KB
testcase_21 AC 3 ms
4,372 KB
testcase_22 AC 600 ms
18,348 KB
testcase_23 AC 442 ms
16,288 KB
testcase_24 AC 624 ms
18,776 KB
testcase_25 AC 570 ms
17,860 KB
testcase_26 AC 590 ms
18,108 KB
testcase_27 AC 2 ms
4,368 KB
testcase_28 AC 1 ms
4,368 KB
testcase_29 AC 2 ms
4,372 KB
testcase_30 AC 1 ms
4,368 KB
testcase_31 AC 2 ms
4,368 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 2 ms
4,368 KB
testcase_35 AC 2 ms
4,372 KB
testcase_36 AC 2 ms
4,368 KB
testcase_37 AC 1 ms
4,368 KB
testcase_38 AC 508 ms
19,096 KB
testcase_39 AC 1,105 ms
19,104 KB
testcase_40 AC 443 ms
16,228 KB
testcase_41 AC 934 ms
19,108 KB
testcase_42 AC 918 ms
19,132 KB
testcase_43 AC 858 ms
19,160 KB
testcase_44 AC 938 ms
19,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;

#define all(a) (a).begin(), (a).end()

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

template<typename T>
struct SegmentTree{
private:
    using Func = function<T(T, T)>;
    int n;
    vector<T> node;
    Func func;
    T init_v;

public:
    SegmentTree(vector<T> v, Func _func, T _init_v){
        func = _func, init_v = _init_v;
        int sz = v.size();
        n = 1;
        while(n < sz) n *= 2;
        node.resize(2 * n, init_v);
        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = func(node[i*2+1], node[i*2+2]);
    }

    void update(int idx, T val){
        idx += n - 1;
        node[idx] = val;
        while(idx > 0){
            idx = (idx - 1) / 2;
            node[idx] = func(node[idx*2+1], node[idx*2+2]);
        }
    }

    T query(int a, int b, int k = 0, int l = 0, int r = -1){
        if (r < 0) r = n;
        if (r <= a || b <= l) return init_v;
        if (a <= l && r <= b) return node[k];
        T lv = query(a, b, k * 2 + 1, l, (l + r) / 2);
        T rv = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return func(lv, rv);
    }
};

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

    int n; cin>>n;
    vector<ll> a(n);
    for(int i=0; i<n; i++) cin>>a[i];

    SegmentTree<ll> seg(a, [](const auto x, const auto y){return __gcd(x, y);}, 0);

    ll ans = 0;
    int r = 0;
    for(int l=0; l<n; l++){
        if(r < l) r = l;
        while(r < n && seg.query(l, r + 1) != 1) r++;
        ans += (n - r);
    }

    cout << ans << endl;

    return 0;
}
0