結果

問題 No.1036 Make One With GCD 2
ユーザー 沙耶花沙耶花
提出日時 2020-04-26 20:10:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 335 ms / 2,000 ms
コード長 1,770 bytes
コンパイル時間 2,933 ms
コンパイル使用メモリ 205,120 KB
最終ジャッジ日時 2025-01-10 02:12:31
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:87:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   87 |         scanf("%d",&N);
      |         ~~~~~^~~~~~~~~
main.cpp:91:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   91 |                 scanf("%lld",&A[i]);
      |                 ~~~~~^~~~~~~~~~~~~~

ソースコード

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)break;
		ans += N+1-r;
		S.pop_front();
	}
	
	printf("%lld\n",ans);
		
	return 0;
}
0