結果

問題 No.1036 Make One With GCD 2
ユーザー 沙耶花沙耶花
提出日時 2020-04-26 20:17:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 274 ms / 2,000 ms
コード長 1,761 bytes
コンパイル時間 2,597 ms
コンパイル使用メモリ 210,812 KB
実行使用メモリ 15,552 KB
最終ジャッジ日時 2023-10-14 20:23:42
合計ジャッジ時間 10,213 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
7,512 KB
testcase_01 AC 143 ms
6,904 KB
testcase_02 AC 127 ms
15,544 KB
testcase_03 AC 26 ms
4,728 KB
testcase_04 AC 45 ms
5,724 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 51 ms
4,880 KB
testcase_08 AC 42 ms
4,348 KB
testcase_09 AC 179 ms
6,972 KB
testcase_10 AC 167 ms
7,072 KB
testcase_11 AC 182 ms
6,880 KB
testcase_12 AC 167 ms
6,704 KB
testcase_13 AC 271 ms
6,708 KB
testcase_14 AC 274 ms
6,988 KB
testcase_15 AC 257 ms
6,652 KB
testcase_16 AC 258 ms
6,768 KB
testcase_17 AC 267 ms
6,688 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 253 ms
6,372 KB
testcase_23 AC 186 ms
5,656 KB
testcase_24 AC 264 ms
6,704 KB
testcase_25 AC 241 ms
6,224 KB
testcase_26 AC 250 ms
6,396 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 1 ms
4,356 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 124 ms
15,552 KB
testcase_39 AC 147 ms
6,944 KB
testcase_40 AC 187 ms
5,664 KB
testcase_41 AC 178 ms
6,968 KB
testcase_42 AC 176 ms
6,976 KB
testcase_43 AC 175 ms
9,348 KB
testcase_44 AC 176 ms
8,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 998244353
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000000000001



template <typename T,typename F>
struct SWAG{
	F func;
	T init_value;
	stack<pair<T,T>> X,Y;
	SWAG(F f,T iv):func(f){
		init_value = iv;
	}
	
	void push_front(T x){
		if(X.empty())X.push({x,x});
		else X.push({x,func(x,X.top().second)});
	}
	
	void push_back(T x){
		if(Y.empty())Y.push({x,x});
		else Y.push({x,func(Y.top().second,x)});
	}
	
	void pop_front(){
		if(X.empty()){
			int n = Y.size();
			stack<T> t;
			for(int i=0;i<n/2;i++){
				t.push(Y.top().first);
				Y.pop();
			}
			while(!Y.empty()){
				push_front(Y.top().first);
				Y.pop();
			}
			while(!t.empty()){
				push_back(t.top());
				t.pop();
			}
			if(!X.empty())X.pop();
		}
		else{
			X.pop();
		}
	}
	
	void pop_back(){
		if(Y.empty()){
			int n = X.size();
			stack<T> t;
			for(int i=0;i<n/2;i++){
				t.push(X.top().first);
				X.pop();
			}
			while(!X.empty()){
				push_back(X.top().first);
				X.pop();
			}
			while(!t.empty()){
				push_front(t.top());
				t.pop();
			}
			if(!Y.empty())Y.pop();
		}
		else{
			Y.pop();
		}
	}
	
	T get(){
		T ret = init_value;
		if(!X.empty())ret = func(ret,X.top().second);
		if(!Y.empty())ret = func(ret,Y.top().second);
		return ret;
	}
	
	
};

int main(){
	
	int N;
	scanf("%d",&N);
	
	vector<long long> A(N);
	for(int i=0;i<N;i++){
		scanf("%lld",&A[i]);
	}
	
	auto f = [](long long a,long long b){
		return gcd(a,b);
	};
	
	SWAG<long long,decltype(f)> S(f,0);
	
	long long ans = 0;
	int r = 0;
	
	for(int i=0;i<N;i++){
		while(S.get()!=1&&r!=N){
			S.push_back(A[r]);
			r++;
		}
		if(S.get()==1)ans += N+1-r;
		S.pop_front();
	}
	
	printf("%lld\n",ans);
		
	return 0;
}
0