結果

問題 No.1036 Make One With GCD 2
ユーザー furafura
提出日時 2020-04-24 22:00:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,031 bytes
コンパイル時間 2,152 ms
コンパイル使用メモリ 208,772 KB
実行使用メモリ 83,612 KB
最終ジャッジ日時 2023-08-07 16:23:01
合計ジャッジ時間 19,207 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 369 ms
79,432 KB
testcase_01 AC 359 ms
78,984 KB
testcase_02 AC 238 ms
79,236 KB
testcase_03 AC 69 ms
29,172 KB
testcase_04 AC 124 ms
51,020 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 123 ms
33,468 KB
testcase_08 AC 99 ms
27,592 KB
testcase_09 AC 496 ms
77,092 KB
testcase_10 AC 473 ms
71,940 KB
testcase_11 AC 508 ms
78,780 KB
testcase_12 AC 479 ms
72,396 KB
testcase_13 AC 649 ms
75,128 KB
testcase_14 AC 654 ms
76,096 KB
testcase_15 AC 612 ms
71,328 KB
testcase_16 AC 618 ms
71,820 KB
testcase_17 AC 637 ms
74,128 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 608 ms
70,440 KB
testcase_23 AC 454 ms
51,560 KB
testcase_24 AC 627 ms
73,264 KB
testcase_25 AC 575 ms
66,672 KB
testcase_26 AC 604 ms
69,320 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 234 ms
79,044 KB
testcase_39 AC 398 ms
78,956 KB
testcase_40 AC 453 ms
51,436 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;
using lint=long long;

template<class T> T gcd(const T& a,const T& b){ return b==0?a:gcd(b,a%b); }

template<class T>
class sparse_table{
	vector<vector<T>> st;
	vector<int> h;
public:
	sparse_table(const vector<T>& a){
		int n=a.size();
		h.assign(n+1,0);
		for(int i=2;i<=n;i++) h[i]=h[i>>1]+1;
		st.resize(h[n]+1);
		st[0]=a;
		for(int i=1;i<h[n]+1;i++){
			st[i].resize(n-(1<<i)+1);
			rep(j,n-(1<<i)+1) st[i][j]=gcd(st[i-1][j],st[i-1][j+(1<<(i-1))]);
		}
	}
	T query(int l,int r)const{
		int i=h[r-l];
		return gcd(st[i][l],st[i][r-(1<<i)]);
	}
};

int main(){
	int n; scanf("%d",&n);
	vector<lint> a(n);
	rep(i,n) scanf("%lld",&a[i]);

	sparse_table<lint> ST(a);

	lint ans=0;
	rep(i,n) if(ST.query(i,n)==1) {
		if(a[i]==1){
			ans+=n-i;
			continue;
		}

		int lo=i+1,hi=n;
		while(hi-lo>1){
			int mi=(lo+hi)/2;
			if(ST.query(i,mi)==1) hi=mi;
			else                  lo=mi;
		}
		ans+=n-hi+1;
	}
	printf("%lld\n",ans);

	return 0;
}
0