結果

問題 No.1036 Make One With GCD 2
ユーザー polylogKpolylogK
提出日時 2020-04-24 23:11:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,256 ms / 2,000 ms
コード長 3,586 bytes
コンパイル時間 2,071 ms
コンパイル使用メモリ 170,932 KB
実行使用メモリ 15,288 KB
最終ジャッジ日時 2023-10-14 19:33:32
合計ジャッジ時間 18,551 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 623 ms
15,208 KB
testcase_01 AC 184 ms
15,288 KB
testcase_02 AC 375 ms
15,284 KB
testcase_03 AC 50 ms
8,440 KB
testcase_04 AC 89 ms
13,684 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 72 ms
8,756 KB
testcase_08 AC 61 ms
8,480 KB
testcase_09 AC 315 ms
14,956 KB
testcase_10 AC 289 ms
14,936 KB
testcase_11 AC 319 ms
15,176 KB
testcase_12 AC 286 ms
14,756 KB
testcase_13 AC 451 ms
15,088 KB
testcase_14 AC 448 ms
14,936 KB
testcase_15 AC 421 ms
14,652 KB
testcase_16 AC 424 ms
14,984 KB
testcase_17 AC 440 ms
14,976 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 412 ms
14,580 KB
testcase_23 AC 310 ms
13,620 KB
testcase_24 AC 428 ms
14,916 KB
testcase_25 AC 396 ms
14,676 KB
testcase_26 AC 409 ms
14,740 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 555 ms
15,104 KB
testcase_39 AC 1,256 ms
15,244 KB
testcase_40 AC 301 ms
13,996 KB
testcase_41 AC 893 ms
15,232 KB
testcase_42 AC 890 ms
15,152 KB
testcase_43 AC 873 ms
15,236 KB
testcase_44 AC 895 ms
15,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cout;
using std::endl;
using std::cin;

template<typename T>
std::vector<T> make_v(size_t a){return std::vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return std::vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

#ifndef INCLUDED_SEGMENT_TREE_HPP
#define INCLUDED_SEGMENT_TREE_HPP

#include <cstdint>
#include <vector>
#include <functional>

template<class Monoid>
class segment_tree {
public:
	using T = typename Monoid::value_type;
	using size_type = std::uint_fast32_t;

	using updator = std::function<T(T)>;
	using checker = std::function<bool(T)>;
	
private:
	const size_type size_;
	size_type height_;

public:
	std::vector<T> data;
	
private:
	const size_type get_height(size_type size) const {
		size_type height = 1;
		while(1 << height <= size) height++;
		return height;
	}
	const size_type base_size() const { return 1 << height_; }

	void meld(size_type index) {
		data[index] = Monoid::operation(data[index << 1 ^ 0], data[index << 1 ^ 1]);
	}

public:
	segment_tree() = default;
	segment_tree(segment_tree&&) = default;
	segment_tree(const segment_tree&) = default;
	
	segment_tree(size_type size)
		: size_(size) {
		height_ = get_height(size);
		data.assign(base_size() << 1, T{});
	}
	T fold(size_type left, size_type right) {
		T l_value = T{};
		T r_value = T{};

		for(left += base_size(), right += base_size();
			left < right;
			left >>= 1, right >>= 1) {
			if(left & 1)  l_value = Monoid::operation(l_value, data[left++]);
			if(right & 1) r_value = Monoid::operation(data[--right], r_value); 
		}
		return Monoid::operation(std::move(l_value), std::move(r_value));
	}

	void update(size_type index, const updator& update) {
		index += base_size();
		data[index] = update(data[index]);

		while(index >>= 1) meld(index);
	}
	void update(size_type index, const T& value) { update(index, [&value](const T& x) { return Monoid::operation(x, value); }); }
	void change(size_type index, const T& value) { update(index, [&value](const T& x) { return value; }); }
	
	const size_type search(size_type left, const checker& check) {
		T val = T{};
		size_type k = left + base_size();
		while(true) {
			if(check(Monoid::operation(val, data[k]))) {
				val = Monoid::operation(val, data[k]);
				if(k & 1) {
					if((k + 1) & k) k = (k + 1) >> 1;
					else return size();
				} else {
					k = k + 1;
				}
			} else {
				if(k < base_size()) {
					k = k << 1 ^ 0;
				} else {
					return k - base_size();
				}
			}
		}
	}
	
	const T operator[](size_type index) const { return data[index + base_size()]; }
	const size_type size() const { return size_; }
	const bool empty() const { return data.empty(); }
};

template<class T>
struct monoid {
	using value_type = T;

	static value_type operation(const value_type& a, const value_type& b) { return a + b; };
};

// @docs docs/segment_tree.md

#endif

struct node {
	i64 val;
	
	node(i64 val = 0): val(val) {}
	node operator+(const node& r) const { return node(std::__gcd(val, r.val)); }
};

int main() {
	int n; scanf("%d", &n); std::vector<i64> a(n);
	for(int i = 0; i < n; i++) scanf("%lld", &a[i]);
	
	segment_tree<monoid<node>> seg(n);
	for(int i = 0; i < n; i++) seg.change(i, a[i]);
	
	i64 now = 0, ans = 0;
	for(int i = 0, left = 1; i < n; i++) {
		left = std::max(left, i + 1);
		now = seg.fold(i, left).val;
		while(left < n and now != 1) now = seg.fold(i, ++left).val;

		if(now == 1) ans += n - left + 1;
	}
	printf("%lld\n", ans);
	return 0;
}
0