結果

問題 No.1036 Make One With GCD 2
ユーザー kappybarkappybar
提出日時 2020-04-25 17:03:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,184 ms / 2,000 ms
コード長 1,686 bytes
コンパイル時間 1,592 ms
コンパイル使用メモリ 172,660 KB
実行使用メモリ 19,176 KB
最終ジャッジ日時 2023-10-14 19:52:53
合計ジャッジ時間 23,438 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 752 ms
19,028 KB
testcase_01 AC 363 ms
19,116 KB
testcase_02 AC 598 ms
19,024 KB
testcase_03 AC 76 ms
10,208 KB
testcase_04 AC 136 ms
16,356 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 144 ms
10,356 KB
testcase_08 AC 118 ms
9,832 KB
testcase_09 AC 545 ms
18,916 KB
testcase_10 AC 506 ms
18,388 KB
testcase_11 AC 556 ms
19,072 KB
testcase_12 AC 513 ms
18,276 KB
testcase_13 AC 838 ms
18,532 KB
testcase_14 AC 842 ms
18,864 KB
testcase_15 AC 792 ms
18,428 KB
testcase_16 AC 795 ms
18,212 KB
testcase_17 AC 819 ms
18,588 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 4 ms
4,352 KB
testcase_22 AC 780 ms
18,284 KB
testcase_23 AC 576 ms
16,224 KB
testcase_24 AC 810 ms
18,564 KB
testcase_25 AC 742 ms
17,904 KB
testcase_26 AC 769 ms
17,956 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 2 ms
4,352 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,348 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,356 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 593 ms
19,020 KB
testcase_39 AC 1,184 ms
19,032 KB
testcase_40 AC 574 ms
16,268 KB
testcase_41 AC 890 ms
19,112 KB
testcase_42 AC 881 ms
19,116 KB
testcase_43 AC 848 ms
19,176 KB
testcase_44 AC 885 ms
19,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll =  long long ;
using P = pair<int,int> ;
const int INF = 1e9;
const int MOD = 1000000007;

ll gcd(ll i,ll j){
        if(j == 0) return i;
        return gcd(j,i%j);
}

struct SegmentTree{
        private:
        ll n;
        vector<ll> node;
        
        public:
        SegmentTree(vector<ll> v){
                ll sz = v.size();
                n = 1;
                while(n < sz) n *= 2;
                node.resize(2*n-1,0);
                for(ll i=0;i<sz;i++) node[i+n-1] = v[i];
                for(ll i=n-2;i>=0;i--) node[i] = gcd(node[2*i+1],node[2*i+2]);
        }
        
        void update(ll x,ll val){
                x += n-1;
                node[x] = val;
                while(x > 0){
                        x = (x-1)/2;
                        node[x] = gcd(node[2*x+1],node[2*x+2]);
                }
        }
        
        ll getgcd(ll a,ll b,ll k=0,ll l=0,ll r=-1){
                if(r < 0) r = n;
                if(r <= a || b <= l) return 0;
                if(a <= l && r <= b) return node[k];
                ll vl = getgcd(a,b,2*k+1,l,(l+r)/2);
                ll vr = getgcd(a,b,2*k+2,(l+r)/2,r);
                return gcd(vl,vr);
        }
};

int main(){
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    SegmentTree seg(a);
    ll ans = 0;
    int j = 0;
    ll res = 0;
    rep(i,n){
        while(j < n && gcd(res,a[j]) != 1){
            res = gcd(res,a[j]);
            ++j;
        }
        ans += n - j;
        if(i == j) ++j;
        res = seg.getgcd(i+1,j+1);
    }
    cout << ans << endl;
    return 0;
}
0