結果

問題 No.1036 Make One With GCD 2
ユーザー 👑 hitonanodehitonanode
提出日時 2020-04-25 21:55:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 281 ms / 2,000 ms
コード長 1,684 bytes
コンパイル時間 2,382 ms
コンパイル使用メモリ 208,684 KB
実行使用メモリ 87,068 KB
最終ジャッジ日時 2023-10-14 20:06:29
合計ジャッジ時間 11,560 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 228 ms
87,064 KB
testcase_01 AC 181 ms
87,044 KB
testcase_02 AC 166 ms
87,064 KB
testcase_03 AC 42 ms
32,560 KB
testcase_04 AC 73 ms
57,636 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 77 ms
37,056 KB
testcase_08 AC 63 ms
30,996 KB
testcase_09 AC 196 ms
85,308 KB
testcase_10 AC 181 ms
79,360 KB
testcase_11 AC 202 ms
86,812 KB
testcase_12 AC 184 ms
80,048 KB
testcase_13 AC 269 ms
83,172 KB
testcase_14 AC 281 ms
84,032 KB
testcase_15 AC 263 ms
78,844 KB
testcase_16 AC 261 ms
79,572 KB
testcase_17 AC 271 ms
81,948 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 3 ms
4,372 KB
testcase_21 AC 3 ms
4,368 KB
testcase_22 AC 255 ms
77,880 KB
testcase_23 AC 189 ms
58,104 KB
testcase_24 AC 267 ms
81,204 KB
testcase_25 AC 243 ms
73,928 KB
testcase_26 AC 253 ms
77,016 KB
testcase_27 AC 1 ms
4,372 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 2 ms
4,368 KB
testcase_30 AC 2 ms
4,368 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 1 ms
4,368 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 2 ms
4,372 KB
testcase_35 AC 1 ms
4,372 KB
testcase_36 AC 2 ms
4,368 KB
testcase_37 AC 1 ms
4,372 KB
testcase_38 AC 168 ms
86,904 KB
testcase_39 AC 247 ms
87,068 KB
testcase_40 AC 195 ms
58,160 KB
testcase_41 AC 233 ms
86,984 KB
testcase_42 AC 232 ms
86,988 KB
testcase_43 AC 225 ms
87,064 KB
testcase_44 AC 229 ms
86,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

// Static sequence sparse table
// Complexity: O(NlogN) for precalculation, O(1) per query
template<typename T, typename F>
struct SparseTable {
    int N, lgN;
    T defaultT;
    F func;
    std::vector<std::vector<T>> data;
    std::vector<int> lgx_table;
    SparseTable(F func) : func(func) {}
    SparseTable(const std::vector<T> &sequence, T defaultT, F func) : N(sequence.size()), defaultT(defaultT), func(func)
    {
        lgx_table.resize(N + 1);
        for (int i = 2; i < N + 1; i++) lgx_table[i] = lgx_table[i >> 1] + 1;
        lgN = lgx_table[N] + 1;
        data.assign(lgN, std::vector<T>(N, defaultT));
        data[0] = sequence;
        for (int d = 1; d < lgN; d++) {
            for (int i = 0; i + (1 << d) <= N; i++) {
                data[d][i] = func(data[d - 1][i], data[d - 1][i + (1 << (d - 1))]);
            }
        }
    }
    T get(int l, int r) { // [l, r), 0-indexed
        assert(l >= 0 and r <= N);
        if (l >= r) return defaultT;
        int d = lgx_table[r - l];
        return func(data[d][l], data[d][r - (1 << d)]);
    }
};

int main()
{
    int N;
    cin >> N;
    vector<lint> A(N);
    for (auto &a : A) cin >> a;
    auto f = [](lint l, lint r) -> lint { return gcd(l, r); };
    SparseTable<lint, decltype(f)> st(A, 0, f);
    lint ret = 0;
    int r = 1;
    for (int l = 0; l < N; l++)
    {
        if (l == r) r++;
        while (r <= N and st.get(l, r) > 1) r++;
        ret += N - r + 1;
    }
    cout << ret << '\n';
}
0