結果

問題 No.1036 Make One With GCD 2
ユーザー ningenMeningenMe
提出日時 2020-04-25 22:18:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,165 ms / 2,000 ms
コード長 2,540 bytes
コンパイル時間 1,896 ms
コンパイル使用メモリ 173,012 KB
実行使用メモリ 87,004 KB
最終ジャッジ日時 2023-10-14 20:07:44
合計ジャッジ時間 18,675 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,165 ms
86,808 KB
testcase_01 AC 364 ms
86,836 KB
testcase_02 AC 166 ms
86,868 KB
testcase_03 AC 34 ms
41,992 KB
testcase_04 AC 66 ms
84,720 KB
testcase_05 AC 1 ms
4,356 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 124 ms
42,628 KB
testcase_08 AC 103 ms
42,056 KB
testcase_09 AC 340 ms
86,488 KB
testcase_10 AC 322 ms
86,464 KB
testcase_11 AC 338 ms
86,768 KB
testcase_12 AC 319 ms
86,220 KB
testcase_13 AC 423 ms
86,608 KB
testcase_14 AC 425 ms
86,504 KB
testcase_15 AC 400 ms
86,032 KB
testcase_16 AC 403 ms
86,348 KB
testcase_17 AC 415 ms
86,552 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 3 ms
4,356 KB
testcase_20 AC 4 ms
4,352 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 396 ms
86,020 KB
testcase_23 AC 298 ms
84,772 KB
testcase_24 AC 412 ms
86,280 KB
testcase_25 AC 378 ms
85,820 KB
testcase_26 AC 392 ms
86,044 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 2 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 163 ms
86,828 KB
testcase_39 AC 909 ms
86,992 KB
testcase_40 AC 299 ms
84,884 KB
testcase_41 AC 837 ms
87,004 KB
testcase_42 AC 822 ms
86,832 KB
testcase_43 AC 814 ms
86,800 KB
testcase_44 AC 882 ms
86,752 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) {
		return Op.funcNode(node[idx[r-l]*length+l],node[idx[r-l]*length+r - (1<<idx[r-l])]);
	}
};

//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);



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

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

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

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