結果

問題 No.1036 Make One With GCD 2
ユーザー IKyoproIKyopro
提出日時 2020-04-24 21:58:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,411 ms / 2,000 ms
コード長 2,947 bytes
コンパイル時間 2,292 ms
コンパイル使用メモリ 204,488 KB
実行使用メモリ 15,360 KB
最終ジャッジ日時 2023-08-06 23:10:54
合計ジャッジ時間 20,854 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,411 ms
15,196 KB
testcase_01 AC 260 ms
15,196 KB
testcase_02 AC 176 ms
15,256 KB
testcase_03 AC 45 ms
8,488 KB
testcase_04 AC 86 ms
13,800 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 96 ms
8,752 KB
testcase_08 AC 79 ms
8,600 KB
testcase_09 AC 321 ms
15,272 KB
testcase_10 AC 322 ms
14,888 KB
testcase_11 AC 330 ms
15,200 KB
testcase_12 AC 340 ms
14,876 KB
testcase_13 AC 489 ms
14,888 KB
testcase_14 AC 515 ms
15,064 KB
testcase_15 AC 458 ms
14,620 KB
testcase_16 AC 502 ms
14,992 KB
testcase_17 AC 473 ms
14,992 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 461 ms
14,756 KB
testcase_23 AC 339 ms
13,896 KB
testcase_24 AC 491 ms
14,992 KB
testcase_25 AC 431 ms
14,788 KB
testcase_26 AC 452 ms
14,732 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 172 ms
15,144 KB
testcase_39 AC 1,152 ms
15,260 KB
testcase_40 AC 350 ms
14,016 KB
testcase_41 AC 1,044 ms
15,360 KB
testcase_42 AC 993 ms
15,140 KB
testcase_43 AC 1,086 ms
15,200 KB
testcase_44 AC 1,189 ms
15,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;


template<typename Monoid,typename F>
class SegmentTree{
private:
    int sz;
    vector<Monoid> seg;
    const F op;
    const Monoid e;
public:
    SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){
        sz = 1;
        while(sz<=n) sz <<= 1;
        seg.assign(2*sz,e);
    }
    void set(int k, const Monoid &x){
        seg[k+sz] = x;
    }
    void build(){
        for(int i=sz-1;i>0;i--){
            seg[i] = op(seg[2*i],seg[2*i+1]);
        }
    }
    void update(int k,const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k>>=1){
            seg[k] = op(seg[2*k],seg[2*k+1]);
        }
    }
    Monoid query(int l,int r){
        Monoid L = e,R = e;
        for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
            if(l&1) L = op(L,seg[l++]);
            if(r&1) R = op(seg[--r],R);
        }
        return op(L,R);
    }
    Monoid operator[](const int &k)const{
        return seg[k+sz];
    }
    template<typename C>
    int find_subtree(int a,const C &check,Monoid &M,bool type) {
        while(a<sz) {
            Monoid nxt = type ? op(seg[2*a+type],M) : op(M,seg[2*a+type]);
            if(check(nxt)) a = 2*a+type;
            else M = nxt, a = 2*a+1-type;
        }
        return a - sz;
    }

    template<typename C>
    int find_first(int a,const C &check) {
        Monoid L = e;
        if(a <= 0) {
            if(check(op(L,seg[1]))) return find_subtree(1,check,L,false);
            return -1;
        }
        int b = sz;
        for(a+=sz,b+=sz;a<b;a>>=1,b>>=1) {
            if(a&1) {
                Monoid nxt = op(L,seg[a]);
                if(check(nxt)) return find_subtree(a,check,L,false);
                L = nxt;
                ++a;
            }
        }
        return -1;
    }

    template<typename C>
    int find_last(int b,const C &check) {
        Monoid R = e;
        if(b >= sz) {
            if(check(op(seg[1], R))) return find_subtree(1, check, R, true);
            return -1;
        }
        int a = sz;
        for(b+=sz; a<b; a>>=1,b>>=1) {
            if(b&1) {
                Monoid nxt = op(seg[--b],R);
                if(check(nxt)) return find_subtree(b,check,R,true);
                R = nxt;
            }
        }
        return -1;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<ll> A(N);
    for(int i=0;i<N;i++) cin >> A[i];
    auto op = [&](ll a,ll b){return gcd(a,b);};
    SegmentTree<ll,decltype(op)> seg(N,op,0);
    for(int i=0;i<N;i++) seg.set(i,A[i]);
    seg.build();
    ll ans = 0;
    for(int i=0;i<N;i++){
        if(seg.query(i,N)>1) continue;
        int r = seg.find_first(i,[&](ll b){return gcd(A[i],b)==1;});
        ans += N-r;
    }
    cout << ans << "\n";
}
0