結果

問題 No.1036 Make One With GCD 2
ユーザー recososorecososo
提出日時 2020-04-25 13:43:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,293 ms / 2,000 ms
コード長 2,070 bytes
コンパイル時間 1,614 ms
コンパイル使用メモリ 169,968 KB
実行使用メモリ 15,412 KB
最終ジャッジ日時 2023-10-14 19:47:53
合計ジャッジ時間 18,089 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 640 ms
15,216 KB
testcase_01 AC 196 ms
15,236 KB
testcase_02 AC 284 ms
15,360 KB
testcase_03 AC 52 ms
8,520 KB
testcase_04 AC 91 ms
13,888 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 76 ms
8,812 KB
testcase_08 AC 62 ms
8,448 KB
testcase_09 AC 320 ms
15,412 KB
testcase_10 AC 298 ms
14,964 KB
testcase_11 AC 330 ms
15,112 KB
testcase_12 AC 302 ms
14,952 KB
testcase_13 AC 464 ms
14,956 KB
testcase_14 AC 470 ms
14,916 KB
testcase_15 AC 441 ms
14,968 KB
testcase_16 AC 444 ms
14,968 KB
testcase_17 AC 458 ms
15,044 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 435 ms
14,696 KB
testcase_23 AC 321 ms
14,108 KB
testcase_24 AC 453 ms
14,988 KB
testcase_25 AC 411 ms
14,680 KB
testcase_26 AC 427 ms
14,700 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 475 ms
15,256 KB
testcase_39 AC 1,293 ms
15,168 KB
testcase_40 AC 321 ms
13,972 KB
testcase_41 AC 912 ms
15,236 KB
testcase_42 AC 899 ms
15,208 KB
testcase_43 AC 863 ms
15,128 KB
testcase_44 AC 912 ms
15,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=1<<30;
const int mod=1e9+7;
const int MOD=998244353;
ll gcd(ll a,ll b){
    return (b?gcd(b,a%b):a);
}
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 n_=v.size();
        init(n_);
        for(int i=0;i<n_;i++) dat[n+i]=v[i];
        for(int i=n-1;i;i--)
            dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
    }
    void update(int k,const E &x){
        k += n;
        dat[k] = g(dat[k], x);
        while(k>>=1)
            dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);    
    }
    T operator [](int k) const { return dat[k+n]; }
    // [a,b)
    T query(int a,int b) const {
        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);
    ll n;cin >> n;
    using T = ll;  // type T
    using E = ll;  // type E
    auto f = [](T a, T b){ // return type T value
        return gcd(a,b);
    };
    auto g = [](T a, E b){ // return type T value
        return b;  // return b;
    };
    T ti = 0;  // identify element
    SegmentTree<T,E,decltype(f),decltype(g)> seg(f,g,ti);
    vector<ll> a(n);
    seg.build(a);
    for(int i=0;i<n;i++){
        ll t;cin >> t;
        seg.update(i,t);
    }
    int r=0;
    ll ans=0;
    for(int i=0;i<n;i++){
        while(r<n&&seg.query(i,r+1)!=1){
            r++;
        }
        ans+=n-r;
    }
    cout << ans << endl;
}
0