結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-04-26 02:01:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 428 ms / 2,000 ms
コード長 2,629 bytes
コンパイル時間 1,968 ms
コンパイル使用メモリ 175,832 KB
実行使用メモリ 15,660 KB
最終ジャッジ日時 2023-10-14 20:13:33
合計ジャッジ時間 12,942 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 169 ms
7,412 KB
testcase_01 AC 177 ms
7,020 KB
testcase_02 AC 179 ms
15,616 KB
testcase_03 AC 18 ms
4,760 KB
testcase_04 AC 28 ms
5,720 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 60 ms
4,672 KB
testcase_08 AC 50 ms
4,508 KB
testcase_09 AC 267 ms
6,956 KB
testcase_10 AC 250 ms
6,504 KB
testcase_11 AC 275 ms
6,988 KB
testcase_12 AC 253 ms
6,752 KB
testcase_13 AC 424 ms
6,716 KB
testcase_14 AC 428 ms
6,884 KB
testcase_15 AC 401 ms
6,844 KB
testcase_16 AC 403 ms
6,884 KB
testcase_17 AC 418 ms
6,620 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 3 ms
4,368 KB
testcase_21 AC 3 ms
4,372 KB
testcase_22 AC 399 ms
6,476 KB
testcase_23 AC 292 ms
5,660 KB
testcase_24 AC 412 ms
6,804 KB
testcase_25 AC 375 ms
6,444 KB
testcase_26 AC 389 ms
6,416 KB
testcase_27 AC 2 ms
4,372 KB
testcase_28 AC 2 ms
4,368 KB
testcase_29 AC 1 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 1 ms
4,368 KB
testcase_33 AC 2 ms
4,368 KB
testcase_34 AC 2 ms
4,372 KB
testcase_35 AC 1 ms
4,372 KB
testcase_36 AC 2 ms
4,372 KB
testcase_37 AC 1 ms
4,368 KB
testcase_38 AC 259 ms
15,660 KB
testcase_39 AC 171 ms
6,884 KB
testcase_40 AC 291 ms
5,628 KB
testcase_41 AC 267 ms
6,984 KB
testcase_42 AC 267 ms
7,108 KB
testcase_43 AC 266 ms
9,284 KB
testcase_44 AC 267 ms
8,084 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: メンバ関数 ‘void Swag<Operator>::pop()’ 内:
main.cpp:72:38: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   72 |                                 auto [val,_] = suf.top();
      |                                      ^
main.cpp: 大域スコープ:
main.cpp:87:9: 警告: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   87 |         inline static constexpr TypeNode unit_node = 0;
      |         ^~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define SPEED cin.tie(0);ios::sync_with_stdio(false);

class Gcd{
public:
	inline static long long impl(long long n, long long m) {
		static constexpr long long K = 5;
		long long t,s;
		for(int i = 0; t = n - m, s = n - m * K, i < 80; ++i) {
			if(t<m){
				if(!t) return m;
				n = m, m = t;
			}
			else{
				if(!m) return t;
				n=t;
				if(t >= m * K) n = s;
			}
		}
		return impl(m, n % m);
	}
	inline static long long pre(long long n, long long m) {
		long long t;
		for(int i = 0; t = n - m, i < 4; ++i) {
			(t < m ? n=m,m=t : n=t);
			if(!m) return n;
		}
		return impl(n, m);
	}
	inline static long long gcd(long long n, long long m) {
		return (n>m ? pre(n,m) : pre(m,n));
	}
	inline static constexpr long long pureGcd(long long a, long long b) {
		return (b ? pureGcd(b, a % b):a);
	}
	inline static constexpr long long lcm(long long a, long long b) {
		return (a*b ? (a / gcd(a, b)*b): 0);
	}
	inline static constexpr long long extGCD(long long a, long long b, long long &x, long long &y) {
		if (b == 0) return x = 1, y = 0, a;
		long long d = extGCD(b, a%b, y, x);
		return y -= a / b * x, d;
	}
};

template<class Operator> class Swag{
public:
	using TypeNode = typename Operator::TypeNode;
	stack<pair<TypeNode,TypeNode>> pre,suf;

	Swag() {
		// do nothing
	}

	TypeNode fold() {
		TypeNode res = Operator::unit_node;
		if(pre.size()) res = Operator::funcNode(res,pre.top().second);
		if(suf.size()) res = Operator::funcNode(res,suf.top().second);
		return res;
	}
	void push(TypeNode val) {
		TypeNode acc = val;
		if(suf.size()) acc = Operator::funcNode(acc,suf.top().second);
		suf.emplace(val,acc);
	}
	void pop() {
		if(pre.empty()) {
			TypeNode acc = Operator::unit_node;
			while(suf.size()) {
				auto [val,_] = suf.top();
				suf.pop();
				acc = Operator::funcNode(acc,val);
				pre.emplace(val,acc);
			}
		}
		if(pre.size()) pre.pop();			
	}
	size_t size(){
		return pre.size()+suf.size();
	}
};

template<class T> struct nodeGCD {
	using TypeNode = T;
	inline static constexpr TypeNode unit_node = 0;
	inline static constexpr TypeNode funcNode(TypeNode l,TypeNode r){return Gcd::gcd(l,r);}
};

// solution by binary search in arbitary range on disjn sparse table 
int main() {
	SPEED
	ll N; cin >> N;
	vector<ll> A(N+1,1);
	for(int i = 0; i < N; ++i) cin >> A[i];
	Swag<nodeGCD<ll>> swag;
	ll ans=0;
	int r=0;
	swag.push(A[0]);
	for(int i=0; i<N; i++){
		while(r<i) {
			r++;
			swag.push(A[r]);
		}
		while(r<N){
			if(swag.fold()==1) break;
			r++;
			swag.push(A[r]);
		}
		ans+=N-r;
		swag.pop();
	}
	cout << ans << endl;
}
0