結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-04-25 22:21:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 429 ms / 2,000 ms
コード長 2,088 bytes
コンパイル時間 1,647 ms
コンパイル使用メモリ 172,508 KB
実行使用メモリ 86,836 KB
最終ジャッジ日時 2023-10-14 20:08:40
合計ジャッジ時間 12,124 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 415 ms
86,804 KB
testcase_01 AC 205 ms
86,716 KB
testcase_02 AC 162 ms
86,836 KB
testcase_03 AC 33 ms
42,032 KB
testcase_04 AC 61 ms
84,788 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 90 ms
42,592 KB
testcase_08 AC 76 ms
42,092 KB
testcase_09 AC 182 ms
86,572 KB
testcase_10 AC 173 ms
86,312 KB
testcase_11 AC 185 ms
86,828 KB
testcase_12 AC 173 ms
86,304 KB
testcase_13 AC 273 ms
86,500 KB
testcase_14 AC 276 ms
86,440 KB
testcase_15 AC 260 ms
86,216 KB
testcase_16 AC 263 ms
86,308 KB
testcase_17 AC 268 ms
86,272 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 3 ms
4,348 KB
testcase_22 AC 258 ms
86,004 KB
testcase_23 AC 200 ms
84,600 KB
testcase_24 AC 266 ms
86,184 KB
testcase_25 AC 247 ms
85,752 KB
testcase_26 AC 254 ms
86,104 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,356 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 259 ms
86,768 KB
testcase_39 AC 426 ms
86,804 KB
testcase_40 AC 201 ms
84,688 KB
testcase_41 AC 429 ms
86,712 KB
testcase_42 AC 429 ms
86,700 KB
testcase_43 AC 413 ms
86,828 KB
testcase_44 AC 428 ms
86,832 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 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)]);
	}
};


inline long long gcd_impl(long long n, long long m) {
  constexpr long long K = 5;
  for(int i = 0; i < 80; ++i) {
    long long t = n - m;
    long long s = n - m * K;
    bool q = t < m;
    bool p = t < m * K;
    n = q ? m : t;
    m = q ? t : m;
    if(m == 0) { return n; }
    n = p ? n : s;
  }
  return gcd_impl(m, n % m);
}

inline long long gcd_pre(long long n, long long m) {
  for(int i = 0; i < 4; ++i) {
    long long t = n - m;
    bool q = t < m;
    n = q ? m : t;
    m = q ? t : m;
    if(m == 0) { return n; }
  }
  return gcd_impl(n, m);
}

inline long long gcd(long long n, long long m) {
  return n > m ? gcd_pre(n, m) : gcd_pre(m, n);
}

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;
	vector<ll> A(N+1,1);
	for(int i = 0; i < N; ++i) cin >> A[i];
	SparseTable<nodeGCD<ll>> st(A);
	ll ans=0;
	int r=1;
	for(int i=0; i<N; i++,r=max(r,i+1)){
		while(r<=N){
			if(st.get(i, r)==1) break;
			r++;
		}
		ans+=N+1-r;
	}
	cout << ans << endl;
}
0