結果

問題 No.1036 Make One With GCD 2
ユーザー rodearodea
提出日時 2020-04-28 18:46:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,301 ms / 2,000 ms
コード長 2,600 bytes
コンパイル時間 1,474 ms
コンパイル使用メモリ 110,908 KB
実行使用メモリ 19,292 KB
最終ジャッジ日時 2023-10-14 20:24:17
合計ジャッジ時間 18,139 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 641 ms
19,088 KB
testcase_01 AC 195 ms
19,040 KB
testcase_02 AC 284 ms
19,176 KB
testcase_03 AC 54 ms
10,032 KB
testcase_04 AC 94 ms
16,360 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 78 ms
10,428 KB
testcase_08 AC 64 ms
9,760 KB
testcase_09 AC 325 ms
18,916 KB
testcase_10 AC 303 ms
18,312 KB
testcase_11 AC 332 ms
19,188 KB
testcase_12 AC 305 ms
18,368 KB
testcase_13 AC 469 ms
18,832 KB
testcase_14 AC 475 ms
18,888 KB
testcase_15 AC 444 ms
18,304 KB
testcase_16 AC 450 ms
18,276 KB
testcase_17 AC 461 ms
18,684 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 439 ms
18,392 KB
testcase_23 AC 324 ms
16,160 KB
testcase_24 AC 457 ms
18,672 KB
testcase_25 AC 417 ms
17,720 KB
testcase_26 AC 432 ms
18,188 KB
testcase_27 AC 1 ms
4,356 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 474 ms
19,160 KB
testcase_39 AC 1,301 ms
19,292 KB
testcase_40 AC 325 ms
16,384 KB
testcase_41 AC 926 ms
19,212 KB
testcase_42 AC 912 ms
18,988 KB
testcase_43 AC 867 ms
19,032 KB
testcase_44 AC 923 ms
19,116 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};

// refer to http://beet-aizu.hatenablog.com/entry/2019/03/12/171221
template <typename T, typename E, typename F, typename G>
struct SegmentTree{
    int n;
    F f;
    G g;
    T ti;
    vector<T> dat;
    SegmentTree(){};
    SegmentTree(F f, G g, T ti) : f(f), g(g), ti(ti){}

    void init(int n_){    
        n = 1;
        while(n < n_) n <<= 1;
        dat.assign(n << 1, ti);
    }

    void build(const vector<T> &v){
        int sz = v.size();
        init(sz);
        for(int i=0; i<sz; i++) dat[n + i] = v[i];
        for(int i=n-1; i>=0; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
    }

    void set_val(int k, T x){
        k += n;
        dat[k] = g(dat[k], x);
        while(k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);    
    }

    T query(int a,int b){
        T vl = ti, vr = ti;
        for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
            if(l & 1) vl = f(vl, dat[l++]);
            if(r & 1) vr = f(dat[--r], vr);
        }
        return f(vl, vr);
    }
};

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

    using T = ll;
    using E = ll;
    auto f = [](T a, T b){
        return __gcd(a, b);
    };
    auto g = [](T a, E b){
        return b;
    };
    T ti = 0;
    SegmentTree<T, E, decltype(f), decltype(g)> seg(f, g, ti);

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

    seg.build(vector<ll>(n, 0));
    for(int i=0; i<n; i++) seg.set_val(i, a[i]);

    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