結果

問題 No.1036 Make One With GCD 2
ユーザー FF256grhyFF256grhy
提出日時 2020-04-25 17:08:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,064 ms / 2,000 ms
コード長 3,394 bytes
コンパイル時間 2,104 ms
コンパイル使用メモリ 170,308 KB
実行使用メモリ 11,424 KB
最終ジャッジ日時 2023-10-14 19:54:04
合計ジャッジ時間 19,362 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 623 ms
11,348 KB
testcase_01 AC 230 ms
11,312 KB
testcase_02 AC 342 ms
11,424 KB
testcase_03 AC 34 ms
7,204 KB
testcase_04 AC 59 ms
11,272 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 93 ms
7,276 KB
testcase_08 AC 77 ms
7,336 KB
testcase_09 AC 388 ms
11,392 KB
testcase_10 AC 362 ms
11,260 KB
testcase_11 AC 398 ms
11,244 KB
testcase_12 AC 366 ms
11,284 KB
testcase_13 AC 661 ms
11,312 KB
testcase_14 AC 673 ms
11,392 KB
testcase_15 AC 629 ms
11,240 KB
testcase_16 AC 635 ms
11,284 KB
testcase_17 AC 654 ms
11,356 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 620 ms
11,232 KB
testcase_23 AC 459 ms
11,264 KB
testcase_24 AC 646 ms
11,276 KB
testcase_25 AC 589 ms
11,368 KB
testcase_26 AC 610 ms
11,388 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,356 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,356 KB
testcase_37 AC 2 ms
4,356 KB
testcase_38 AC 334 ms
10,236 KB
testcase_39 AC 1,064 ms
11,284 KB
testcase_40 AC 461 ms
11,316 KB
testcase_41 AC 770 ms
11,424 KB
testcase_42 AC 769 ms
11,396 KB
testcase_43 AC 695 ms
11,260 KB
testcase_44 AC 753 ms
11,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define dec(i, n)  decID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define FR front()
#define BA back()
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)
#define IN(T, ...) T __VA_ARGS__; IN_(__VA_ARGS__);
void IN_() { };
template<typename T, typename ... U> void IN_(T & a, U & ... b) { cin >> a; IN_(b ...); };
template<typename T> void OUT(T && a) { cout << a << endl; }
template<typename T, typename ... U> void OUT(T && a, U && ... b) { cout << a << " "; OUT(b ...); }

// ---- ----

template<typename T> class SegmentTree {
private:
	int n, s;
	vector<T> a;
	function<T(T &, T &)> f;
	T e;
	bool ok;
public:
	SegmentTree() { n = 0; }
	SegmentTree(int nn, function<T(T &, T &)> ff, T ee) { init(nn, ff, ee); }
	void init(int nn, function<T(T &, T &)> ff, T ee) {
		n = nn;
		f = ff;
		e = ee;
		s = 1;
		while(s < n) { s *= 2; }
		a = vector<T>(2 * s, e);
		ok = true;
	}
	void shift(int & p) {
		assert(inID(p, 0, n));
		p += s;
	}
	void apply(int p, function<void(T &)> g) {
		shift(p);
		g(a[p]);
		while(p > 1) {
			p /= 2;
			a[p] = f(a[2 * p], a[2 * p + 1]);
		}
	}
	T fold_ID(int l, int r) {
		assert(ok);
		assert(inII(l, 0, n)); l += s;
		assert(inII(r, 0, n)); r += s; r--;
		T x = e, y = e;
		while(l <= r) {
			if(l % 2 == 1) { x = f(x, a[l]); l++; }
			if(r % 2 == 0) { y = f(a[r], y); r--; }
			l /= 2;
			r /= 2;
		}
		return f(x, y);
	}
	T fold_II(int l, int r) { return fold_ID(l + 0, r + 1); }
	T fold_CI(int l, int r) { return fold_ID(l + 1, r + 1); }
	T fold_CD(int l, int r) { return fold_ID(l + 1, r + 0); }
	const T & operator[](int p) {
		shift(p);
		return a[p];
	}
	T & ref(int p) {
		shift(p);
		ok = false;
		return a[p];
	}
	void update() {
		dec(i, s) { a[i] = f(a[2 * i], a[2 * i + 1]); }
		ok = true;
	}
};
#define OP(s) [&](auto & A, auto & B) { return s; }
#define AP(s) [&](auto & A) { s; }

// ----

template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int main() {
	IN(int, n);
	SegmentTree<LL> st(n, gcd<LL>, 0);
	inc(i, n) { cin >> st.ref(i); }
	st.update();
	
	LL ans = 0;
	int p = 0;
	inc(i, n) {
		LL g = st.fold_II(i, p);
		until(g == 1) {
			p++;
			if(p == n) { break; }
			g = gcd(g, st[p]);
		}
		if(p == n) { break; }
		ans += n - p;
	}
	OUT(ans);
}
0