結果

問題 No.1036 Make One With GCD 2
ユーザー tomatoma
提出日時 2020-04-24 22:00:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,893 bytes
コンパイル時間 2,125 ms
コンパイル使用メモリ 203,728 KB
実行使用メモリ 15,476 KB
最終ジャッジ日時 2023-08-06 23:13:08
合計ジャッジ時間 53,566 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 1,600 ms
15,188 KB
testcase_02 AC 412 ms
15,068 KB
testcase_03 AC 31 ms
8,588 KB
testcase_04 AC 54 ms
13,684 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 346 ms
8,804 KB
testcase_08 AC 277 ms
8,584 KB
testcase_09 AC 1,663 ms
14,884 KB
testcase_10 AC 1,556 ms
14,976 KB
testcase_11 AC 1,702 ms
15,408 KB
testcase_12 AC 1,585 ms
14,848 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 1,959 ms
14,656 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 5 ms
4,380 KB
testcase_21 AC 5 ms
4,384 KB
testcase_22 AC 1,933 ms
14,676 KB
testcase_23 AC 1,385 ms
13,880 KB
testcase_24 TLE -
testcase_25 AC 1,819 ms
14,524 KB
testcase_26 AC 1,897 ms
14,584 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 396 ms
15,320 KB
testcase_39 TLE -
testcase_40 AC 1,398 ms
13,592 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tp3 = tuple<int, int, int>;
using Mat = vector<vector<ll>>;
constexpr int INF = 1 << 28;
constexpr ll INFL = 1ll << 60;
constexpr int dh[4] = { 0,1,0,-1 };
constexpr int dw[4] = { -1,0,1,0 };
bool isin(const int H, const int W, const int h, const int w) {
    return 0 <= h && h < H && 0 <= w && w < W;
}
template<typename T>
T minch(T& l, T r) {
    return l = min(l, r);
}
template<typename T>
T maxch(T& l, T r) {
    return l = max(l, r);
}
template<typename T>
void output(const T& val) {
    cout << val << endl;
}
template<typename T>
void output(const vector<T>& vec, const bool newline = false) {
    for (const T& val : vec)cout << val << (newline ? '\n' : ' '); cout << endl;
}
template<typename T>
void output(const vector<vector<T>>& mat) {
    for (const auto& row : mat)output(row);
}
//ttp://beet-aizu.hatenablog.com/entry/2019/03/12/171221
template<typename T>
class SegmentTree {
private:
    using F = function<T(T, T)>; // モノイド型
    int n; // 横幅
    F f;   // モノイド
    T e;   // モノイド単位元
    vector<T> data;

public:
    // init忘れに注意
    SegmentTree() {}
    SegmentTree(F f, T e) :f(f), e(e) {}
    void init(int n_) {
        n = 1;
        while (n < n_)n <<= 1;
        data.assign(n << 1, e);
    }
    void build(const vector<T>& v) {
        int n_ = v.size();
        init(n_);
        rep(i, n_)data[n + i] = v[i];
        for (int i = n - 1; i >= 0; i--) {
            data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]);
        }
    }
    void set_val(int idx, T val) {
        idx += n;
        data[idx] = val;
        while (idx >>= 1) {
            data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
        }
    }
    T query(int a, int b) {
        // [a,b)
        T vl = e, vr = e;
        for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
            if (l & 1)vl = f(vl, data[l++]); // unknown
            if (r & 1)vr = f(data[--r], vr); // unknown
        }
        return f(vl, vr);
    }
};
// ============ template finished ============



int main()
{
    ll N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N)cin >> A[i];
    auto f = [](ll a, ll b) {return gcd(a, b); };
    SegmentTree<ll> seg(f, 0ll);
    seg.build(A);

    ll res = 0;
    rep(i, N) {
        if (A[i] == 1) {
            res += N - i;
            continue;
        }
        if (seg.query(i, N) != 1) {
            continue;
        }
        ll safe = N, out = i;
        while (abs(safe - out) > 1) {
            ll mid = (safe + out) / 2;
            ll tres = seg.query(i, mid);
            (tres == 1 ? safe : out) = mid;
        }
        res += N - out;
    }
    output(res);
    return 0;
}
0