結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-04-25 16:38:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,992 bytes
コンパイル時間 1,845 ms
コンパイル使用メモリ 174,500 KB
実行使用メモリ 98,560 KB
最終ジャッジ日時 2024-04-24 22:22:09
合計ジャッジ時間 25,239 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,978 ms
98,560 KB
testcase_01 AC 469 ms
93,184 KB
testcase_02 AC 395 ms
93,184 KB
testcase_03 AC 83 ms
45,568 KB
testcase_04 AC 154 ms
91,904 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 152 ms
45,952 KB
testcase_08 AC 131 ms
45,568 KB
testcase_09 AC 704 ms
93,184 KB
testcase_10 AC 649 ms
92,928 KB
testcase_11 AC 697 ms
93,056 KB
testcase_12 AC 640 ms
92,800 KB
testcase_13 AC 933 ms
93,056 KB
testcase_14 AC 1,002 ms
92,928 KB
testcase_15 AC 876 ms
92,672 KB
testcase_16 AC 879 ms
92,928 KB
testcase_17 AC 904 ms
93,056 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 869 ms
92,800 KB
testcase_23 AC 655 ms
91,904 KB
testcase_24 AC 926 ms
92,928 KB
testcase_25 AC 843 ms
92,800 KB
testcase_26 AC 854 ms
92,672 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 2 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 1 ms
5,376 KB
testcase_36 AC 1 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 379 ms
93,056 KB
testcase_39 TLE -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

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]));
	}
};

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
	int N; cin >> N;
	vector<ll> A(N+1,1);
	for(int i = 0; i < N; ++i) cin >> A[i];
	DisjointSparseTable<nodeGCD<ll>> dst(A);
	ll ans = 0;
    for(int i = 0; i < N; ++i) {
        if(A[i] == 1){
            ans += (N-i);
        }
        else{
            int ok = N,ng = i,md;
            while(ok-ng>1){
                md = (ok+ng)>>1;
                (dst.get(i,md+1)==1?ok:ng)=md;
            }
            ans += (N-ok);
        }
    }
    cout << ans << endl;
}
0