結果

問題 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,800 ms
コンパイル使用メモリ 174,464 KB
実行使用メモリ 87,424 KB
最終ジャッジ日時 2024-04-25 10:28:44
合計ジャッジ時間 25,875 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 375 ms
87,296 KB
testcase_01 AC 360 ms
87,168 KB
testcase_02 AC 381 ms
87,296 KB
testcase_03 AC 109 ms
32,896 KB
testcase_04 AC 202 ms
57,856 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 144 ms
37,248 KB
testcase_08 AC 115 ms
30,976 KB
testcase_09 AC 463 ms
85,376 KB
testcase_10 AC 443 ms
79,488 KB
testcase_11 AC 482 ms
87,040 KB
testcase_12 AC 451 ms
80,256 KB
testcase_13 AC 610 ms
83,328 KB
testcase_14 AC 631 ms
84,224 KB
testcase_15 AC 576 ms
78,976 KB
testcase_16 AC 582 ms
79,744 KB
testcase_17 AC 591 ms
82,048 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 567 ms
78,080 KB
testcase_23 AC 419 ms
58,112 KB
testcase_24 AC 595 ms
81,152 KB
testcase_25 AC 537 ms
74,240 KB
testcase_26 AC 569 ms
76,800 KB
testcase_27 AC 2 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 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 365 ms
87,296 KB
testcase_39 AC 392 ms
87,168 KB
testcase_40 AC 427 ms
58,240 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