結果

問題 No.1036 Make One With GCD 2
ユーザー furafura
提出日時 2020-04-24 22:00:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,031 bytes
コンパイル時間 2,476 ms
コンパイル使用メモリ 203,464 KB
実行使用メモリ 84,608 KB
最終ジャッジ日時 2024-04-25 10:04:17
合計ジャッジ時間 25,479 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 393 ms
79,616 KB
testcase_01 AC 368 ms
79,616 KB
testcase_02 AC 236 ms
79,488 KB
testcase_03 AC 68 ms
29,440 KB
testcase_04 AC 117 ms
51,328 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 119 ms
33,792 KB
testcase_08 AC 92 ms
27,776 KB
testcase_09 AC 478 ms
77,568 KB
testcase_10 AC 459 ms
72,064 KB
testcase_11 AC 483 ms
79,104 KB
testcase_12 AC 457 ms
72,704 KB
testcase_13 AC 610 ms
75,776 KB
testcase_14 AC 622 ms
76,544 KB
testcase_15 AC 587 ms
71,680 KB
testcase_16 AC 587 ms
72,192 KB
testcase_17 AC 608 ms
74,624 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 572 ms
70,656 KB
testcase_23 AC 421 ms
51,712 KB
testcase_24 AC 595 ms
73,728 KB
testcase_25 AC 553 ms
67,072 KB
testcase_26 AC 581 ms
69,504 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 237 ms
79,360 KB
testcase_39 AC 387 ms
79,488 KB
testcase_40 AC 427 ms
51,712 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