結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-04-25 16:15:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 931 ms / 2,000 ms
コード長 2,151 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 176,904 KB
実行使用メモリ 93,284 KB
最終ジャッジ日時 2023-10-14 19:49:00
合計ジャッジ時間 21,520 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 255 ms
93,284 KB
testcase_01 AC 535 ms
93,220 KB
testcase_02 AC 173 ms
93,212 KB
testcase_03 AC 55 ms
45,472 KB
testcase_04 AC 93 ms
91,560 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 215 ms
45,740 KB
testcase_08 AC 177 ms
45,528 KB
testcase_09 AC 570 ms
92,964 KB
testcase_10 AC 528 ms
92,600 KB
testcase_11 AC 578 ms
93,228 KB
testcase_12 AC 541 ms
92,852 KB
testcase_13 AC 927 ms
92,992 KB
testcase_14 AC 931 ms
93,112 KB
testcase_15 AC 869 ms
92,800 KB
testcase_16 AC 894 ms
92,660 KB
testcase_17 AC 923 ms
92,852 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 4 ms
4,352 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 874 ms
92,848 KB
testcase_23 AC 686 ms
91,608 KB
testcase_24 AC 909 ms
92,836 KB
testcase_25 AC 840 ms
92,416 KB
testcase_26 AC 861 ms
92,628 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 174 ms
93,208 KB
testcase_39 AC 422 ms
93,200 KB
testcase_40 AC 657 ms
91,696 KB
testcase_41 AC 370 ms
93,272 KB
testcase_42 AC 392 ms
93,124 KB
testcase_43 AC 246 ms
93,084 KB
testcase_44 AC 278 ms
93,208 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<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,vector<typeNode>(length,Op.unitNode));
		for(int i = 0; i < vec.size(); ++i) node[0][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][l] = node[0][l];
				node[i][r] = node[0][r];
				//accumulate
				for(int k = 1; k < (1<<i); ++k) {
					node[i][l-k] = Op.funcNode(node[i][l-k+1],node[0][l-k]);
					node[i][r+k] = Op.funcNode(node[i][r+k-1],node[0][r+k]);
				}
			}
		}
	}

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

int ctz(long long a) { return __builtin_ctzll(a); }
template <class T> T gcd(T a, T b) {
  a = abs(a), b = abs(b);
  if (a == 0) return b;
  int k = ctz(a | b);
  a >>= ctz(a);
  while (b) {
    b >>= ctz(b);
    if (a > b) swap(a, b);
    b -= a;
  }
  return a << k;
}

template<class typeNode> struct nodeGCD {
	typeNode unitNode = 0;
	typeNode funcNode(typeNode l,typeNode r){return gcd(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