結果

問題 No.1036 Make One With GCD 2
ユーザー 沙耶花沙耶花
提出日時 2020-04-24 21:52:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 1,913 bytes
コンパイル時間 4,049 ms
コンパイル使用メモリ 203,456 KB
実行使用メモリ 15,432 KB
最終ジャッジ日時 2023-10-14 19:16:47
合計ジャッジ時間 11,131 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 190 ms
15,116 KB
testcase_01 AC 139 ms
15,432 KB
testcase_02 AC 174 ms
15,236 KB
testcase_03 AC 19 ms
8,520 KB
testcase_04 AC 32 ms
13,772 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 1 ms
4,372 KB
testcase_07 AC 51 ms
8,780 KB
testcase_08 AC 42 ms
8,528 KB
testcase_09 AC 166 ms
15,216 KB
testcase_10 AC 156 ms
14,988 KB
testcase_11 AC 171 ms
15,188 KB
testcase_12 AC 157 ms
14,744 KB
testcase_13 AC 268 ms
14,856 KB
testcase_14 AC 270 ms
14,856 KB
testcase_15 AC 254 ms
14,660 KB
testcase_16 AC 255 ms
14,988 KB
testcase_17 AC 263 ms
15,104 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 2 ms
4,372 KB
testcase_21 AC 2 ms
4,368 KB
testcase_22 AC 252 ms
14,652 KB
testcase_23 AC 185 ms
13,908 KB
testcase_24 AC 260 ms
14,804 KB
testcase_25 AC 238 ms
14,588 KB
testcase_26 AC 247 ms
14,700 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 2 ms
4,372 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,372 KB
testcase_33 AC 1 ms
4,372 KB
testcase_34 AC 1 ms
4,368 KB
testcase_35 AC 2 ms
4,368 KB
testcase_36 AC 2 ms
4,372 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 171 ms
15,168 KB
testcase_39 AC 182 ms
15,212 KB
testcase_40 AC 186 ms
13,656 KB
testcase_41 AC 215 ms
15,200 KB
testcase_42 AC 211 ms
15,200 KB
testcase_43 AC 222 ms
15,172 KB
testcase_44 AC 223 ms
15,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T,typename F>
struct segtree{
	//元データx[i]はv[n+i]
	//v[i]の親はv[i/2],子はv[i*2]とv[i*2+1]
	F func;
	vector<T> v;
	int n;
	
	T init_value;
	
	segtree(int sz,F f,T iv):func(f){
		init_value = iv;
		n=1;
		while(true){
			if(n>=sz)break;
			n*=2;
		}
		v.resize(2*n,init_value);

		for(int i=n-1;i>=0;i--){
			v[i]=func(v[i<<1],v[(i<<1)+1]);
		}
	}
	
	segtree(vector<T> &x,F f,T iv):func(f){
		init_value = iv;
		n=1;
		while(true){
			if(n>=x.size())break;
			n*=2;
		}
		v.resize(2*n,init_value);
		
		for(int i=0;i<x.size();i++){
			v[n+i]=x[i];
		}
		for(int i=n-1;i>=0;i--){
			v[i]=func(v[i<<1],v[(i<<1)+1]);
		}
	}

	void update(int x,T val){
		x+=n;
		v[x]=val;
		while(x>0){
			x>>=1;
			v[x]=func(v[x<<1],v[(x<<1)+1]);
		}
	}
	
	//区間[l,r)におけるクエリ処理
	T query(int l,int r){
		if(l>=r)return init_value;
		l+=n;
		r+=n;
		T res1 = init_value;
		T res2 = init_value;
		while(true){
			if(l&1){
				res1=func(res1,v[l++]);
			}
			if(r&1){
				res2=func(v[--r],res2);
			}
			if(l>=r)break;
			l>>=1;r>>=1;
		}
		return func(res1,res2);
	}
	
	void show(){
		int n = 1;
		for(int i=1;i<v.size();i++){
			for(int j=0;j<n;j++){
				if(j!=0)cout<<' ';
				cout<<v[i+j];
			}
			cout<<endl;
			i+=n-1;
			n*=2;
		}
	}
	
};

int main(){
	
	long long N;
	cin>>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);
	};
	
	segtree<long long,decltype(f)> seg(A,f,0LL);
	
	long long last = 1;
	long long ans = 0;
	
	for(int i=0;i<N;i++){
		long long X = seg.query(i,last);
		while(X!=1&&last!=N){
			X = gcd(X,A[last]);
			last++;
		}
		//cout<<last<<endl;
		if(X==1)ans += N+1-last;
	}
	
	cout<<ans<<endl;
		
	return 0;
}
0