結果

問題 No.1036 Make One With GCD 2
ユーザー 沙耶花沙耶花
提出日時 2020-04-24 23:30:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,875 ms / 2,000 ms
コード長 1,897 bytes
コンパイル時間 2,483 ms
コンパイル使用メモリ 200,208 KB
実行使用メモリ 15,744 KB
最終ジャッジ日時 2024-04-24 18:21:38
合計ジャッジ時間 41,028 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,555 ms
15,616 KB
testcase_01 AC 1,139 ms
15,488 KB
testcase_02 AC 1,363 ms
15,488 KB
testcase_03 AC 234 ms
8,960 KB
testcase_04 AC 455 ms
14,080 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 404 ms
9,088 KB
testcase_08 AC 316 ms
8,960 KB
testcase_09 AC 1,140 ms
15,488 KB
testcase_10 AC 1,053 ms
15,232 KB
testcase_11 AC 1,174 ms
15,488 KB
testcase_12 AC 1,050 ms
15,232 KB
testcase_13 AC 1,394 ms
15,360 KB
testcase_14 AC 1,408 ms
15,488 KB
testcase_15 AC 1,319 ms
15,104 KB
testcase_16 AC 1,321 ms
15,104 KB
testcase_17 AC 1,362 ms
15,232 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 5 ms
5,376 KB
testcase_22 AC 1,293 ms
15,104 KB
testcase_23 AC 922 ms
14,208 KB
testcase_24 AC 1,357 ms
15,104 KB
testcase_25 AC 1,212 ms
14,976 KB
testcase_26 AC 1,260 ms
15,104 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 1,345 ms
15,616 KB
testcase_39 AC 1,661 ms
15,488 KB
testcase_40 AC 917 ms
14,080 KB
testcase_41 AC 1,799 ms
15,616 KB
testcase_42 AC 1,795 ms
15,488 KB
testcase_43 AC 1,869 ms
15,488 KB
testcase_44 AC 1,875 ms
15,744 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 ans = 0;
	
	for(int i=0;i<N;i++){
		int ok = i;
		int ng = N+1;
		
		while(ng-ok>1){
			int mid = (ok+ng)/2;
			if(seg.query(i,mid)==1)ng = mid;
			else ok=mid;
		}
		
		ans += N+1-ng;
	}
	
	cout<<ans<<endl;
		
	return 0;
}
0