結果
問題 |
No.1036 Make One With GCD 2
|
ユーザー |
|
提出日時 | 2020-04-25 19:02:55 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 949 ms / 2,000 ms |
コード長 | 1,808 bytes |
コンパイル時間 | 1,819 ms |
コンパイル使用メモリ | 175,976 KB |
実行使用メモリ | 89,088 KB |
最終ジャッジ日時 | 2024-09-16 13:52:59 |
合計ジャッジ時間 | 19,963 ms |
ジャッジサーバーID (参考情報) |
judge6 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#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<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*length,Op.unitNode); for(int i = 0; i < vec.size(); ++i) node[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*length+l] = node[l]; node[i*length+r] = node[r]; //accumulate for(int k = 1; k < (1<<i); ++k) { node[i*length+l-k] = Op.funcNode(node[i*length+l-k+1],node[l-k]); node[i*length+r+k] = Op.funcNode(node[i*length+r+k-1],node[r+k]); } } } } //[l,r) typeNode get(int l,int r) { r--; return (l>r||l<0||length<=r) ? Op.unitNode: (l==r ? node[l] : Op.funcNode(node[msb[l^r]*length+l],node[msb[l^r]*length+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 ll 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; int r=1; for(int i=0; i<N; i++,r=max(r,i+1)){ while(r<=N){ if(dst.get(i, r)==1) break; r++; } ans+=N+1-r; } cout << ans << endl; }