結果

問題 No.1036 Make One With GCD 2
ユーザー noshi91noshi91
提出日時 2020-04-25 17:33:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,157 bytes
コンパイル時間 1,047 ms
コンパイル使用メモリ 84,176 KB
実行使用メモリ 86,656 KB
最終ジャッジ日時 2024-04-25 01:03:29
合計ジャッジ時間 25,035 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,833 ms
86,656 KB
testcase_01 AC 352 ms
81,280 KB
testcase_02 AC 231 ms
81,280 KB
testcase_03 AC 58 ms
30,080 KB
testcase_04 AC 105 ms
52,992 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 110 ms
34,432 KB
testcase_08 AC 91 ms
28,800 KB
testcase_09 AC 752 ms
79,360 KB
testcase_10 AC 691 ms
73,856 KB
testcase_11 AC 768 ms
81,152 KB
testcase_12 AC 718 ms
74,368 KB
testcase_13 AC 1,124 ms
77,568 KB
testcase_14 AC 1,128 ms
78,464 KB
testcase_15 AC 1,024 ms
73,344 KB
testcase_16 AC 1,040 ms
73,984 KB
testcase_17 AC 1,084 ms
76,544 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 1,024 ms
72,448 KB
testcase_23 AC 760 ms
53,376 KB
testcase_24 AC 1,088 ms
75,776 KB
testcase_25 AC 972 ms
68,992 KB
testcase_26 AC 1,014 ms
71,168 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 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 2 ms
5,376 KB
testcase_33 AC 2 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 222 ms
81,408 KB
testcase_39 TLE -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <utility>
#include <vector>

template <class Semigroup> class disjoint_sparse_table {
public:
  using value_structure = Semigroup;
  using value_type = typename value_structure::value_type;

private:
  using container_type = ::std::vector<::std::vector<value_type>>;

public:
  using const_reference = typename container_type::value_type::const_reference;
  using size_type = typename container_type::value_type::size_type;

protected:
  static size_type msb(size_type c) { return 31 - __builtin_clz(c); }

  container_type table;

public:
  disjoint_sparse_table() : table() {}
  template <class InputIterator>
  disjoint_sparse_table(InputIterator first, InputIterator last) : table() {
    table.emplace_back(first, last);
    const size_type size = table.front().size();
    for (size_type i = 2; i < size; i <<= 1) {
      typename container_type::value_type v;
      v.reserve(size);
      for (size_type j = i; j < size; j += i << 1) {
        v.emplace_back(table.front()[j - 1]);
        for (size_type k = 2; k <= i; ++k)
          v.emplace_back(
              value_structure::operation(table.front()[j - k], v.back()));
        v.emplace_back(table.front()[j]);
        for (size_type k = 1; k < i && j + k < size; ++k)
          v.emplace_back(
              value_structure::operation(v.back(), table.front()[j + k]));
      }
      table.emplace_back(::std::move(v));
    }
  }

  size_type size() const { return table.empty() ? 0 : table.front().size(); }
  bool empty() const { return size() == 0; }

  value_type fold_closed(const size_type first, const size_type last) const {
    assert(first <= last);
    assert(last < size());
    if (first == last) {
      return table.front()[first];
    } else {
      const size_type p = msb(first ^ last);
      return value_structure::operation(
          table[p][first ^ (static_cast<size_type>(1) << p) - 1],
          table[p][last]);
    }
  }
  const_reference operator[](const size_type index) const {
    assert(index < size());
    return table.front()[index];
  }
};

#include <iostream>
#include <numeric>
#include <utility>
#include <vector>

using u64 = unsigned long long;

class gcd_monoid {
public:
  using value_type = u64;
  static constexpr u64 operation(u64 a, u64 b) {
    while (b != 0) {
      a %= b;
      std::swap(a, b);
    }
    return a;
  }
  static constexpr u64 identity = 0;
};

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  int n;
  std::cin >> n;
  std::vector<u64> as(n);
  for (int i = 0; i < n; i++)
    std::cin >> as[i];

  const disjoint_sparse_table<gcd_monoid> dst(as.begin(), as.end());

  u64 ans = 0;
  for (int i = 0; i < n; i++) {
    int l = i;
    u64 pre = as[i], lst = dst.fold_closed(i, n - 1);
    while (lst != pre) {
      int r = n, pl = l;
      while (l + 1 < r) {
        int m = (l + r) >> 1;
        if (dst.fold_closed(i, m - 1) != pre)
          r = m;
        else
          l = m;
      }
      if (pre == 1)
        ans += l - pl;
      pre = dst.fold_closed(i, r - 1);
    }
    if (lst == 1)
      ans += n - l;
  }

  std::cout << ans << std::endl;
  return 0;
}
0