結果
問題 | No.1036 Make One With GCD 2 |
ユーザー |
|
提出日時 | 2020-04-25 17:03:23 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,074 ms / 2,000 ms |
コード長 | 1,686 bytes |
コンパイル時間 | 1,815 ms |
コンパイル使用メモリ | 173,404 KB |
実行使用メモリ | 19,328 KB |
最終ジャッジ日時 | 2024-09-16 13:47:04 |
合計ジャッジ時間 | 22,058 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 41 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) using namespace std; using ll = long long ; using P = pair<int,int> ; const int INF = 1e9; const int MOD = 1000000007; ll gcd(ll i,ll j){ if(j == 0) return i; return gcd(j,i%j); } struct SegmentTree{ private: ll n; vector<ll> node; public: SegmentTree(vector<ll> v){ ll sz = v.size(); n = 1; while(n < sz) n *= 2; node.resize(2*n-1,0); for(ll i=0;i<sz;i++) node[i+n-1] = v[i]; for(ll i=n-2;i>=0;i--) node[i] = gcd(node[2*i+1],node[2*i+2]); } void update(ll x,ll val){ x += n-1; node[x] = val; while(x > 0){ x = (x-1)/2; node[x] = gcd(node[2*x+1],node[2*x+2]); } } ll getgcd(ll a,ll b,ll k=0,ll l=0,ll r=-1){ if(r < 0) r = n; if(r <= a || b <= l) return 0; if(a <= l && r <= b) return node[k]; ll vl = getgcd(a,b,2*k+1,l,(l+r)/2); ll vr = getgcd(a,b,2*k+2,(l+r)/2,r); return gcd(vl,vr); } }; int main(){ int n; cin >> n; vector<ll> a(n); rep(i,n) cin >> a[i]; SegmentTree seg(a); ll ans = 0; int j = 0; ll res = 0; rep(i,n){ while(j < n && gcd(res,a[j]) != 1){ res = gcd(res,a[j]); ++j; } ans += n - j; if(i == j) ++j; res = seg.getgcd(i+1,j+1); } cout << ans << endl; return 0; }