結果

問題 No.1036 Make One With GCD 2
ユーザー tomatoma
提出日時 2020-04-24 22:39:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,760 bytes
コンパイル時間 2,309 ms
コンパイル使用メモリ 198,856 KB
実行使用メモリ 22,300 KB
最終ジャッジ日時 2024-04-24 18:07:07
合計ジャッジ時間 40,400 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,907 ms
15,616 KB
testcase_01 AC 1,386 ms
15,304 KB
testcase_02 AC 395 ms
15,360 KB
testcase_03 AC 31 ms
8,960 KB
testcase_04 AC 54 ms
13,956 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 292 ms
8,964 KB
testcase_08 AC 230 ms
8,668 KB
testcase_09 AC 1,374 ms
15,232 KB
testcase_10 AC 1,286 ms
14,976 KB
testcase_11 AC 1,446 ms
15,280 KB
testcase_12 AC 1,309 ms
15,104 KB
testcase_13 AC 1,706 ms
15,124 KB
testcase_14 AC 1,777 ms
15,360 KB
testcase_15 AC 1,595 ms
14,924 KB
testcase_16 AC 1,645 ms
14,916 KB
testcase_17 AC 1,646 ms
15,064 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 1,605 ms
14,884 KB
testcase_23 AC 1,131 ms
14,004 KB
testcase_24 AC 1,678 ms
15,000 KB
testcase_25 AC 1,491 ms
14,816 KB
testcase_26 AC 1,567 ms
14,976 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 377 ms
15,360 KB
testcase_39 TLE -
testcase_40 AC 1,153 ms
14,080 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 AC 1,971 ms
15,616 KB
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:
    int n; // 横幅
    T e;   // モノイド単位元
    vector<T> data;

public:
    // init忘れに注意
    SegmentTree() {}
    SegmentTree(T e) :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] = gcd(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] = gcd(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 = gcd(vl, data[l++]); // unknown
            if (r & 1)vr = gcd(data[--r], vr); // unknown
        }
        return gcd(vl, vr);
    }
};
// ============ template finished ============



int main()
{
    ll N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N)cin >> A[i];
    SegmentTree<ll> seg(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