結果
問題 | No.1036 Make One With GCD 2 |
ユーザー | ningenMe |
提出日時 | 2020-04-26 02:44:28 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,943 bytes |
コンパイル時間 | 2,143 ms |
コンパイル使用メモリ | 177,496 KB |
実行使用メモリ | 37,120 KB |
最終ジャッジ日時 | 2024-11-08 19:39:48 |
合計ジャッジ時間 | 43,335 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | TLE | - |
testcase_01 | AC | 1,525 ms
31,744 KB |
testcase_02 | AC | 1,241 ms
31,616 KB |
testcase_03 | AC | 23 ms
17,024 KB |
testcase_04 | AC | 42 ms
30,464 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 302 ms
17,024 KB |
testcase_08 | AC | 250 ms
16,896 KB |
testcase_09 | TLE | - |
testcase_10 | AC | 1,959 ms
31,488 KB |
testcase_11 | TLE | - |
testcase_12 | AC | 1,953 ms
31,488 KB |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | AC | 3 ms
5,248 KB |
testcase_19 | AC | 4 ms
5,248 KB |
testcase_20 | AC | 6 ms
5,248 KB |
testcase_21 | AC | 5 ms
5,248 KB |
testcase_22 | TLE | - |
testcase_23 | AC | 1,452 ms
30,336 KB |
testcase_24 | TLE | - |
testcase_25 | AC | 1,993 ms
31,104 KB |
testcase_26 | TLE | - |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 2 ms
5,248 KB |
testcase_29 | AC | 2 ms
5,248 KB |
testcase_30 | AC | 2 ms
5,248 KB |
testcase_31 | AC | 3 ms
5,248 KB |
testcase_32 | AC | 3 ms
5,248 KB |
testcase_33 | AC | 2 ms
5,248 KB |
testcase_34 | AC | 2 ms
5,248 KB |
testcase_35 | AC | 2 ms
5,248 KB |
testcase_36 | AC | 2 ms
5,248 KB |
testcase_37 | AC | 3 ms
5,248 KB |
testcase_38 | AC | 1,237 ms
31,616 KB |
testcase_39 | TLE | - |
testcase_40 | AC | 1,518 ms
30,336 KB |
testcase_41 | TLE | - |
testcase_42 | -- | - |
testcase_43 | -- | - |
testcase_44 | -- | - |
ソースコード
#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 SegmentTree { Operator Op; using typeNode = decltype(Op.unitNode); size_t length; size_t num; vector<typeNode> node; vector<pair<size_t,size_t>> range; public: //unitで初期化 SegmentTree(const size_t num): num(num) { for (length = 1; length < num; length *= 2); node.resize(2 * length, Op.unitNode); range.resize(2 * length); for (int i = 0; i < length; ++i) range[i+length] = make_pair(i,i+1); for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i<<1)+0].first,range[(i<<1)+1].second); } //vectorで初期化 SegmentTree(const vector<typeNode> & vec) : num(vec.size()) { for (length = 1; length < vec.size(); length *= 2); node.resize(2 * length, Op.unitNode); for (int i = 0; i < vec.size(); ++i) node[i + length] = vec[i]; for (int i = length - 1; i >= 0; --i) node[i] = Op.funcNode(node[(i<<1)+0],node[(i<<1)+1]); range.resize(2 * length); for (int i = 0; i < length; ++i) range[i+length] = make_pair(i,i+1); for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i<<1)+0].first,range[(i<<1)+1].second); } //同じinitで初期化 SegmentTree(const size_t num, const typeNode init) : num(num) { for (length = 1; length < num; length *= 2); node.resize(2 * length, init); range.resize(2 * length); for (int i = 0; i < length; ++i) range[i+length] = make_pair(i,i+1); for (int i = length - 1; i >= 0; --i) range[i] = make_pair(range[(i<<1)+0].first,range[(i<<1)+1].second); } //[idx,idx+1) void update(size_t idx, const typeNode var) { if(idx < 0 || length <= idx) return; idx += length; node[idx] = Op.funcMerge(node[idx],var); while(idx >>= 1) node[idx] = Op.funcNode(node[(idx<<1)+0],node[(idx<<1)+1]); } //[l,r) typeNode get(int l, int r) { if (l < 0 || length <= l || r < 0 || length < r) return Op.unitNode; typeNode vl = Op.unitNode, vr = Op.unitNode; for(l += length, r += length; l < r; l >>=1, r >>=1) { if(l&1) vl = Op.funcNode(vl,node[l++]); if(r&1) vr = Op.funcNode(node[--r],vr); } return Op.funcNode(vl,vr); } //return [0,length] int PrefixBinarySearch(typeNode var) { if(!Op.funcCheck(node[1],var)) return num; typeNode ret = Op.unitNode; size_t idx = 2; for(; idx < 2*length; idx<<=1){ if(!Op.funcCheck(Op.funcNode(ret,node[idx]),var)) { ret = Op.funcNode(ret,node[idx]); idx++; } } return min((idx>>1) - length,num); } //range[l,r) return [l,r] int BinarySearch(size_t l, size_t r, typeNode var) { if (l < 0 || length <= l || r < 0 || length < r) return -1; typeNode ret = Op.unitNode; size_t off = l; for(size_t idx = l+length; idx < 2*length && off < r; ){ if(range[idx].second<=r && !Op.funcCheck(Op.funcNode(ret,node[idx]),var)) { ret = Op.funcNode(ret,node[idx]); off = range[idx++].second; if(!(idx&1)) idx >>= 1; } else{ idx <<=1; } } return off; } }; class Gcd{ public: inline static long long impl(long long n, long long m) { static constexpr long long K = 5; long long t,s; for(int i = 0; t = n - m, s = n - m * K, i < 80; ++i) { if(t<m){ if(!t) return m; n = m, m = t; } else{ if(!m) return t; n=t; if(t >= m * K) n = s; } } return impl(m, n % m); } inline static long long pre(long long n, long long m) { long long t; for(int i = 0; t = n - m, i < 4; ++i) { (t < m ? n=m,m=t : n=t); if(!m) return n; } return impl(n, m); } inline static long long gcd(long long n, long long m) { return (n>m ? pre(n,m) : pre(m,n)); } inline static constexpr long long pureGcd(long long a, long long b) { return (b ? pureGcd(b, a % b):a); } inline static constexpr long long lcm(long long a, long long b) { return (a*b ? (a / gcd(a, b)*b): 0); } inline static constexpr long long extGcd(long long a, long long b, long long &x, long long &y) { if (b == 0) return x = 1, y = 0, a; long long d = extGcd(b, a%b, y, x); return y -= a / b * x, d; } }; template<class typeNode> struct nodeGCDPointUpdate { typeNode unitNode = 0; typeNode funcNode(typeNode l,typeNode r){return Gcd::gcd(l,r) ;} typeNode funcMerge(typeNode l,typeNode r){return r;} bool funcCheck(typeNode nodeVal,typeNode var){return var == nodeVal;} }; // 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]; SegmentTree<nodeGCDPointUpdate<ll>> seg(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; (seg.get(i,md+1)==1?ok:ng)=md; } ans += (N-ok); } } cout << ans << endl; }