結果

問題 No.1036 Make One With GCD 2
ユーザー yakkiyakki
提出日時 2020-04-24 22:37:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,170 bytes
コンパイル時間 1,640 ms
コンパイル使用メモリ 129,468 KB
実行使用メモリ 29,952 KB
最終ジャッジ日時 2024-04-24 18:03:22
合計ジャッジ時間 9,930 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
using namespace std;
 
#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
//#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592
 
const double EPS = 1e-10;
 
using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;

ll gcd(ll a,ll b){
    if(b == 0) return a;
    return gcd(b,a%b);
}

template<typename Monoid>
struct SegmentTree{
    int n;
    vector<Monoid> node;

    SegmentTree(vector<Monoid> v){
        int k = v.size();
        n = 1; while(n < k) n *= 2;
        node.resize(2*n-1,0); //単位元で初期化
        for(int i = 0; i < k; i++) node[i+n-1] = v[i];
        for(int i = n-2; i >= 0; i--) node[i] = gcd(node[2*i+1],node[2*i+2]); //書き換える
    }

    SegmentTree(int k){
        n = 1; while(n < k) n *= 2;
        node.resize(2*n-1,0); //書き換える
    }

    void update(int k, Monoid x){
        k += n-1;
        node[k] = x;
        while(k > 0){
            k = (k-1)/2;
            node[k] = gcd(node[2*k+1],node[2*k+2]); //書き換える
        }
    }

    //[a,b)のクエリに答える
    Monoid query(int a, int b, int k = 0, int l = 0, int r = -1){
        if(r < 0) r = n;
        if(b <= l || a >= r) return 0;
        if(a <= l && b >= r) return node[k];
        else{
            Monoid s = query(a,b,2*k+1,l,(l+r)/2);
            Monoid t = query(a,b,2*k+2,(l+r)/2,r);
            return gcd(s,t); //書き換える
        }
    }
};

int main(){
    int n; cin >> n;
    vector<ll> a(n);
    rep(i,n) cin >> a[i];
    SegmentTree<ll> seg(a);
    ll ans = 0;
    rep(i,n){
        int ng = -1,ok = i+1;
        while(ok-ng > 1){
            int mid = (ok+ng)/2;
            if(seg.query(mid,i+1) == 1) ng = mid;
            else ok = mid;
        }
        ans += ng+1;
    }
    cout << ans << endl;
}
0