結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-04-25 16:31:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 951 ms / 2,000 ms
コード長 1,910 bytes
コンパイル時間 2,090 ms
コンパイル使用メモリ 172,732 KB
実行使用メモリ 89,020 KB
最終ジャッジ日時 2023-10-14 19:50:04
合計ジャッジ時間 19,787 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 445 ms
88,876 KB
testcase_01 AC 304 ms
88,876 KB
testcase_02 AC 231 ms
88,820 KB
testcase_03 AC 63 ms
43,544 KB
testcase_04 AC 120 ms
87,492 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 103 ms
43,648 KB
testcase_08 AC 88 ms
43,396 KB
testcase_09 AC 540 ms
88,760 KB
testcase_10 AC 508 ms
88,600 KB
testcase_11 AC 552 ms
88,772 KB
testcase_12 AC 512 ms
88,620 KB
testcase_13 AC 803 ms
88,732 KB
testcase_14 AC 812 ms
88,832 KB
testcase_15 AC 763 ms
88,520 KB
testcase_16 AC 769 ms
88,604 KB
testcase_17 AC 791 ms
88,608 KB
testcase_18 AC 2 ms
4,356 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 4 ms
4,368 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 756 ms
88,612 KB
testcase_23 AC 568 ms
87,644 KB
testcase_24 AC 782 ms
88,716 KB
testcase_25 AC 717 ms
88,296 KB
testcase_26 AC 745 ms
88,312 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,356 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 235 ms
88,880 KB
testcase_39 AC 951 ms
88,760 KB
testcase_40 AC 566 ms
87,516 KB
testcase_41 AC 435 ms
88,784 KB
testcase_42 AC 442 ms
89,020 KB
testcase_43 AC 373 ms
88,876 KB
testcase_44 AC 402 ms
88,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define SPEED cin.tie(0);ios::sync_with_stdio(false);

template<class Operator> class DisjointSparseTable{
public:
	Operator Op;                           
	using typeNode = decltype(Op.unitNode);
	size_t depth;
	size_t length;
	vector<typeNode> node;
	vector<size_t> msb;

	DisjointSparseTable(const vector<typeNode>& vec) {
		for(depth = 0;(1<<depth)<=vec.size();++depth);
		length = (1<<depth);
		
		//msb
		msb.resize(length,0);
		for(int i = 0; i < length; ++i) for(int j = 0; j < depth; ++j) if(i>>j) msb[i] = j;

		//init value
		node.resize(depth*length,Op.unitNode);
		for(int i = 0; i < vec.size(); ++i) node[i] = vec[i];

		for(int i = 1; i < depth; ++i) {
			for(int r = (1<<i),l = r-1; r < length; r += (2<<i),l = r-1){
				//init accumulate
				node[i*length+l] = node[l];
				node[i*length+r] = node[r];
				//accumulate
				for(int k = 1; k < (1<<i); ++k) {
					node[i*length+l-k] = Op.funcNode(node[i*length+l-k+1],node[l-k]);
					node[i*length+r+k] = Op.funcNode(node[i*length+r+k-1],node[r+k]);
				}
			}
		}
	}

	//[l,r)
	typeNode get(int l,int r) {
		r--;
		return (l>r||l<0||length<=r) ? Op.unitNode: (l==r ? node[l] : Op.funcNode(node[msb[l^r]*length+l],node[msb[l^r]*length+r]));
	}
};

template<class typeNode> struct nodeGCD {
	typeNode unitNode = 0;
	typeNode funcNode(typeNode l,typeNode r){return ((r == 0) ? l : funcNode(r, l % r));}
};

// solution by binary search in arbitary range on disjn sparse table 
int main() {
	SPEED
	ll N; cin >> N;
    assert(1 <= N && N <= 500000);
	vector<ll> A(N+1,1);
	for(int i = 0; i < N; ++i) cin >> A[i];
	for(int i = 0; i < N; ++i) assert(1 <= A[i] && A[i] <= 1000000000000000000LL);
	DisjointSparseTable<nodeGCD<ll>> dst(A);
	ll ans=0;
	int r=1;
	for(int i=0; i<N; i++){
		while(r<=N){
			if(dst.get(i, r)==1) break;
			r++;
		}
		ans+=N+1-r;
	}
	cout << ans << endl;
}
0