結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-04-25 21:46:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,871 bytes
コンパイル時間 1,509 ms
コンパイル使用メモリ 168,956 KB
実行使用メモリ 87,040 KB
最終ジャッジ日時 2024-04-25 11:02:53
合計ジャッジ時間 25,468 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 364 ms
87,040 KB
testcase_01 AC 368 ms
86,912 KB
testcase_02 AC 251 ms
86,912 KB
testcase_03 AC 68 ms
42,240 KB
testcase_04 AC 130 ms
84,864 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 119 ms
42,752 KB
testcase_08 AC 103 ms
41,984 KB
testcase_09 AC 461 ms
86,784 KB
testcase_10 AC 494 ms
86,400 KB
testcase_11 AC 530 ms
86,912 KB
testcase_12 AC 498 ms
86,400 KB
testcase_13 AC 604 ms
86,784 KB
testcase_14 AC 615 ms
86,656 KB
testcase_15 AC 584 ms
86,528 KB
testcase_16 AC 587 ms
86,400 KB
testcase_17 AC 598 ms
86,656 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 570 ms
86,272 KB
testcase_23 AC 435 ms
84,736 KB
testcase_24 AC 595 ms
86,656 KB
testcase_25 AC 544 ms
86,016 KB
testcase_26 AC 565 ms
86,016 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 AC 239 ms
86,912 KB
testcase_39 AC 390 ms
87,040 KB
testcase_40 AC 438 ms
84,864 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
権限があれば一括ダウンロードができます

ソースコード

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 SparseTable{
public:
	Operator Op;                           
	using typeNode = decltype(Op.unitNode);
	vector<typeNode> node;
	vector<int> idx;
	size_t depth;
	size_t length;

	SparseTable(const vector<typeNode>& vec) {
		for(depth = 0;(1<<depth)<=vec.size();++depth);
		length = (1<<depth);
		node.resize(depth*length);
		for(int i = 0; i < vec.size(); ++i) node[i] = vec[i];
		for(int i = 1; i < depth; ++i) for(int j = 0; j + (1<<i) < (1<<depth); ++j) node[i*length+j] = Op.funcNode(node[(i-1)*length+j],node[(i-1)*length+j + (1 << (i-1))]);
		idx.resize(vec.size()+1);
		for(int i = 2; i < vec.size()+1; ++i) idx[i] = idx[i>>1] + 1;
	}

	//[l,r)
	typeNode get(int l,int r) {
		int bit = idx[r-l];
		return Op.funcNode(node[bit*length+l],node[bit*length+r - (1<<bit)]);
	}
};

//verify https://atcoder.jp/contests/joi2016ho/tasks/joi2016ho_a

	// function<ll(ll,ll)> funcNode1 = [&](ll l,ll r){return min(l,r);};
	// function<ll(ll,ll)> funcNode2 = [&](ll l,ll r){return max(l,r);};
	// SparseTable<ll> stMin(A,funcNode1),stMax(A,funcNode2);


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

// solution by binary search in arbitary range on disjn sparse table 
int main() {
	SPEED
	int N; cin >> N;
	vector<ll> A(N+1,1);
	for(int i = 0; i < N; ++i) cin >> A[i];
	SparseTable<nodeGCD<ll>> st(A);
	ll ans = 0;
    for(int i = 0; i < N; ++i) {
        if(A[i] == 1){
            ans += (N-i);
			continue;
        }
		if(st.get(i,N) != 1){
			continue;
		}
		int ok = N,ng = i,md;
		while(ok-ng>1){
			md = (ok+ng)>>1;
			(st.get(i,md+1)==1?ok:ng)=md;
		}
		ans += (N-ok);
    }
    cout << ans << endl;
}
0