結果

問題 No.1036 Make One With GCD 2
ユーザー finefine
提出日時 2020-04-25 01:48:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,705 bytes
コンパイル時間 1,712 ms
コンパイル使用メモリ 176,028 KB
実行使用メモリ 87,208 KB
最終ジャッジ日時 2023-08-07 16:49:41
合計ジャッジ時間 27,106 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 366 ms
87,088 KB
testcase_01 AC 353 ms
87,064 KB
testcase_02 AC 366 ms
87,152 KB
testcase_03 AC 112 ms
32,672 KB
testcase_04 AC 201 ms
57,644 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 155 ms
37,164 KB
testcase_08 AC 121 ms
30,972 KB
testcase_09 AC 481 ms
85,136 KB
testcase_10 AC 461 ms
79,404 KB
testcase_11 AC 487 ms
86,768 KB
testcase_12 AC 474 ms
80,064 KB
testcase_13 AC 633 ms
83,108 KB
testcase_14 AC 638 ms
83,988 KB
testcase_15 AC 589 ms
78,912 KB
testcase_16 AC 601 ms
79,332 KB
testcase_17 AC 618 ms
81,884 KB
testcase_18 AC 2 ms
4,360 KB
testcase_19 AC 2 ms
4,356 KB
testcase_20 AC 4 ms
4,356 KB
testcase_21 AC 3 ms
4,356 KB
testcase_22 AC 591 ms
77,832 KB
testcase_23 AC 449 ms
58,124 KB
testcase_24 AC 604 ms
80,868 KB
testcase_25 AC 556 ms
74,248 KB
testcase_26 AC 594 ms
76,596 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,356 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 2 ms
4,356 KB
testcase_31 AC 2 ms
4,356 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,360 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,356 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 361 ms
87,028 KB
testcase_39 AC 393 ms
87,052 KB
testcase_40 AC 449 ms
58,296 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

template <class SemiLattice>
struct SparseTable {
    using T = typename SemiLattice::T;

    int n;
    vector< vector<T> > table;
    vector<int> log_table;

    SparseTable() {}
    SparseTable(const vector<T>& v) : n(v.size()) {
        log_table.resize(n + 1, 0);
        for (int i = 2; i <= n; i++) {
            log_table[i] = log_table[i >> 1] + 1;
        }

        table.resize(log_table[n] + 1, vector<T>(n));
        for (int i = 0; i < n; i++) {
            table[0][i] = v[i];
        }

        size_t sz = table.size();
        for (int i = 1; i < sz; i++) {
            for (int j = 0; j + (1 << i) <= n; j++) {
                table[i][j] = SemiLattice::merge(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
            }
        }
    }

    //区間[l, r)に対するクエリに答える
    T query(int l, int r) {
        assert(l < r && r <= n);
        int k = log_table[r - l];
        return SemiLattice::merge(table[k][l], table[k][r - (1 << k)]);
    }
};

//以下、SemiLatticeの例
template <class U = ll>
struct RMQSL {
    using T = U;
    static T merge(T a, T b) { return __gcd(a, b); }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int n;
    cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    SparseTable< RMQSL<> > st(a);
    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        int ok = n + 1, ng = i;
        while (ok - ng > 1) {
            int mid = (ok + ng) / 2;
            (st.query(i, mid) == 1 ? ok : ng) = mid;
        }
        ans += n + 1 - ok;
    }
    cout << ans << "\n";
    return 0;
}
0