結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
7,552 KB
testcase_01 AC 139 ms
6,956 KB
testcase_02 AC 103 ms
15,304 KB
testcase_03 AC 26 ms
4,368 KB
testcase_04 AC 43 ms
5,604 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 48 ms
4,732 KB
testcase_08 AC 39 ms
4,400 KB
testcase_09 AC 162 ms
6,856 KB
testcase_10 AC 157 ms
6,584 KB
testcase_11 AC 169 ms
6,880 KB
testcase_12 AC 155 ms
6,784 KB
testcase_13 AC 248 ms
6,692 KB
testcase_14 AC 260 ms
6,980 KB
testcase_15 AC 238 ms
6,560 KB
testcase_16 AC 238 ms
6,672 KB
testcase_17 AC 243 ms
6,672 KB
testcase_18 AC 2 ms
4,356 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 237 ms
6,420 KB
testcase_23 AC 177 ms
5,704 KB
testcase_24 AC 242 ms
6,728 KB
testcase_25 AC 229 ms
6,448 KB
testcase_26 AC 230 ms
6,464 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 2 ms
4,352 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 98 ms
15,256 KB
testcase_39 AC 130 ms
6,984 KB
testcase_40 AC 168 ms
5,616 KB
testcase_41 AC 172 ms
6,876 KB
testcase_42 AC 156 ms
6,952 KB
testcase_43 AC 152 ms
9,568 KB
testcase_44 AC 152 ms
8,316 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){
		if(a==0)return b;
		if(b==0)return a;
		int s = __builtin_ctzll(a|b);
		a >>= __builtin_ctzll(a);
		while(b){
			b>>=__builtin_ctzll(b);
			if(a>b)swap(a,b);
			b-=a;
		}
		return a<<s;
	};
	
	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)break;
		ans += N+1-r;
		S.pop_front();
	}
	
	printf("%lld\n",ans);
		
	return 0;
}
0