結果

問題 No.1036 Make One With GCD 2
ユーザー fura
提出日時 2020-04-24 22:00:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,031 bytes
コンパイル時間 2,341 ms
コンパイル使用メモリ 203,348 KB
最終ジャッジ日時 2025-01-09 23:46:37
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37 TLE * 4
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:33:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   33 |         int n; scanf("%d",&n);
      |                ~~~~~^~~~~~~~~
main.cpp:35:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   35 |         rep(i,n) scanf("%lld",&a[i]);
      |                  ~~~~~^~~~~~~~~~~~~~

ソースコード

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