結果

問題 No.1036 Make One With GCD 2
ユーザー bond_cmprogbond_cmprog
提出日時 2020-04-24 22:16:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 331 ms / 2,000 ms
コード長 3,052 bytes
コンパイル時間 4,664 ms
コンパイル使用メモリ 202,884 KB
実行使用メモリ 15,312 KB
最終ジャッジ日時 2023-10-14 19:22:19
合計ジャッジ時間 12,629 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 251 ms
15,040 KB
testcase_01 AC 127 ms
15,152 KB
testcase_02 AC 172 ms
15,312 KB
testcase_03 AC 18 ms
8,492 KB
testcase_04 AC 29 ms
13,956 KB
testcase_05 AC 2 ms
4,372 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 43 ms
8,700 KB
testcase_08 AC 36 ms
8,428 KB
testcase_09 AC 152 ms
15,100 KB
testcase_10 AC 143 ms
15,040 KB
testcase_11 AC 156 ms
15,124 KB
testcase_12 AC 143 ms
14,864 KB
testcase_13 AC 241 ms
14,836 KB
testcase_14 AC 243 ms
14,864 KB
testcase_15 AC 228 ms
14,580 KB
testcase_16 AC 230 ms
14,864 KB
testcase_17 AC 238 ms
14,788 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 228 ms
14,664 KB
testcase_23 AC 167 ms
13,772 KB
testcase_24 AC 234 ms
14,868 KB
testcase_25 AC 214 ms
14,592 KB
testcase_26 AC 226 ms
14,576 KB
testcase_27 AC 1 ms
4,372 KB
testcase_28 AC 1 ms
4,368 KB
testcase_29 AC 1 ms
4,372 KB
testcase_30 AC 1 ms
4,372 KB
testcase_31 AC 1 ms
4,372 KB
testcase_32 AC 1 ms
4,372 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 1 ms
4,368 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,368 KB
testcase_37 AC 1 ms
4,372 KB
testcase_38 AC 210 ms
15,052 KB
testcase_39 AC 223 ms
15,088 KB
testcase_40 AC 166 ms
13,808 KB
testcase_41 AC 315 ms
15,216 KB
testcase_42 AC 305 ms
15,132 KB
testcase_43 AC 318 ms
15,108 KB
testcase_44 AC 331 ms
15,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define ll long long
using namespace std;
//typedef vector<unsigned int>vec;
//typedef vector<ll>vec;
//typedef vector<vec> mat;
typedef pair<int, int> P;
typedef pair<ll,ll> LP;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000;//1e18
const ll  MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-10;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
//template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template<class Monoid> struct SegTree {
    using Func = function<Monoid(Monoid, Monoid)>;
    const Func F;
    const Monoid UNITY;
    int SIZE_R;
    vector<Monoid> dat;
    
    SegTree(int n, const Func f, const Monoid &unity): F(f), UNITY(unity) { init(n); }
    void init(int n) {
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        dat.assign(SIZE_R * 2, UNITY);
    }
    
    /* set, a is 0-indexed */
    void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
    void build() {
        for (int k = SIZE_R - 1; k > 0; --k)
            dat[k] = F(dat[k*2], dat[k*2+1]);
    }
    
    /* update a, a is 0-indexed */
    void update(int a, const Monoid &v) {
        int k = a + SIZE_R;
        dat[k] = v;
        while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]);
    }
    
    /* get [a, b), a and b are 0-indexed */
    Monoid get(int a, int b) {
        Monoid vleft = UNITY, vright = UNITY;
        for (int left = a + SIZE_R, right = b + SIZE_R; left < right; left >>= 1, right >>= 1) {
            if (left & 1) vleft = F(vleft, dat[left++]);
            if (right & 1) vright = F(dat[--right], vright);
        }                                                                                                              
        return F(vleft, vright);
    }
    inline Monoid operator [] (int a) { return dat[a + SIZE_R]; }
    
    /* debug */
    void print() {
        for (int i = 0; i < SIZE_R; ++i) {
            cout << (*this)[i];
            if (i != SIZE_R-1) cout << ",";
        }
        cout << endl;
    }
};

void solve(){
    int N;
    cin >> N;
    SegTree<long long> seg(N, [](long long a, long long b) {
            return gcd(a, b);
        },
        0);
    vector<long long> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
        seg.set(i, A[i]);
    }
    seg.build();
    ll ans = 0;
    int right = 0;
    for(int left=0;left<N;left++){
        while(right < N && seg.get(left, right + 1) != 1){
            ++right;
        }

        ans += N - right;
        // cout << left << " " << right << endl;
        if(right == left) right++;
    }
    cout << ans << endl;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
    // int T; cin >> T; REP(t,T) solve();
}
0