結果

問題 No.1036 Make One With GCD 2
ユーザー kcvlexkcvlex
提出日時 2020-04-24 22:03:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 806 ms / 2,000 ms
コード長 3,804 bytes
コンパイル時間 1,611 ms
コンパイル使用メモリ 150,340 KB
実行使用メモリ 113,664 KB
最終ジャッジ日時 2024-04-25 10:06:03
合計ジャッジ時間 21,189 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 774 ms
113,664 KB
testcase_01 AC 554 ms
113,664 KB
testcase_02 AC 459 ms
113,664 KB
testcase_03 AC 182 ms
57,984 KB
testcase_04 AC 406 ms
112,256 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 232 ms
58,112 KB
testcase_08 AC 202 ms
57,984 KB
testcase_09 AC 569 ms
113,536 KB
testcase_10 AC 536 ms
113,152 KB
testcase_11 AC 578 ms
113,664 KB
testcase_12 AC 545 ms
113,280 KB
testcase_13 AC 641 ms
113,408 KB
testcase_14 AC 654 ms
113,408 KB
testcase_15 AC 614 ms
113,280 KB
testcase_16 AC 625 ms
113,152 KB
testcase_17 AC 656 ms
113,536 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 606 ms
113,280 KB
testcase_23 AC 511 ms
112,256 KB
testcase_24 AC 626 ms
113,408 KB
testcase_25 AC 589 ms
113,152 KB
testcase_26 AC 604 ms
113,152 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 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 451 ms
113,664 KB
testcase_39 AC 745 ms
113,536 KB
testcase_40 AC 518 ms
112,256 KB
testcase_41 AC 754 ms
113,536 KB
testcase_42 AC 741 ms
113,536 KB
testcase_43 AC 771 ms
113,664 KB
testcase_44 AC 806 ms
113,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <initializer_list>
#include <utility>
#include <bitset>
#include <tuple>
#include <type_traits>
#include <functional>
#include <string>
#include <array>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <algorithm>
#include <complex>
#include <random>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <regex>
#include <cassert>
#include <cstddef>
#include <variant>
#define endl codeforces
#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)
using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }
template <typename T> T make_v(T init) { return init; }
template <typename T, typename... Tail> auto make_v(T init, std::size_t s, Tail... tail) { auto v = std::move(make_v(init, tail...)); return vec<decltype(v)>(s, v); }
template <typename T, std::size_t Head, std::size_t... Tail> struct multi_dem_array { using type = std::array<typename multi_dem_array<T, Tail...>::type, Head>; };
template <typename T, std::size_t Head> struct multi_dem_array<T, Head> { using type = std::array<T, Head>; };
template <typename T, std::size_t... Args> using mdarray = typename multi_dem_array<T, Args...>::type;
namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; }
ssize_t ceil_pow2(ssize_t s) {
    ssize_t ret = 1;
    while (ret < s) ret *= 2;
    return ret;
}

struct SparseTable {
    vvec<ll> table;
    vec<ll> bsz;

    ll op(ll a, ll b) {
        return (a && b ? std::gcd(a, b) : std::max(a, b)); 
    }

    SparseTable(const vec<ll> &av) {
        ll sz = ceil_pow2(av.size());
        table = make_v<ll>(0, sz, 20);
        bsz.resize(sz + 1);
        for (ll i = 0, j = 1; j <= sz; i++, j *= 2) bsz[j] = i;
        for (ll i = 1; i <= sz; i++) chmax(bsz[i], bsz[i - 1]);
        for (ll i = 0; i < av.size(); i++) table[i][0] = av[i];
        for (ll i = 0; i + 1 < 20; i++) for (ll j = 0; j < sz; j++) {
            ll f = j;
            ll t = std::min<ll>(sz - 1, f + (1ll << i));
            table[j][i + 1] = op(table[j][i], table[t][i]);
        }
    }

    ll query(ll l, ll r) {
        ll len = r - l;
        ll idx = bsz[len];
        ll offset = 1ll << idx;
        return op(table[l][idx], table[r - offset][idx]);
    }
};

int main() {
    ll n;
    std::cin >> n;
    vec<ll> av(n);
    for (ll &e : av) std::cin >> e;
    SparseTable table(av);
    ll ans = 0;
    for (ll i = 0; i < n; i++) {
        if (1 < table.query(i, n)) break;
        ll ok = n, ng = i;
        while (1 < std::abs(ok - ng)) {
            ll mid = (ok + ng) / 2;
            (table.query(i, mid) == 1 ? ok : ng) = mid;
        }
        ans += n - ng;
    }
    std::cout << ans << '\n';
    return 0;
}
0