結果
| 問題 |
No.1036 Make One With GCD 2
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2020-07-04 01:53:04 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,204 bytes |
| コンパイル時間 | 896 ms |
| コンパイル使用メモリ | 89,784 KB |
| 実行使用メモリ | 29,020 KB |
| 最終ジャッジ日時 | 2024-09-17 06:16:52 |
| 合計ジャッジ時間 | 11,767 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | TLE * 1 -- * 40 |
ソースコード
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
template <typename T>
T gcd(T a, T b) {
if (a < b) swap(a, b);
while (b != 0) {
T tmp = b;
b = a % b;
a = tmp;
}
return a;
}
template <typename T>
struct segtree{
int n;
T UNIT;
vector<T> dat;
segtree(int n_, T unit){
UNIT = unit;
n = 1;
while(n < n_) n *= 2;
dat = vector<T>(2*n);
for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
}
T calc(T a, T b){
T ans;
ans = gcd<ll>(a, b);
return ans;
}
void insert(int k, T a){
dat[k+n-1] = a;
}
void update_all(){
for(int i = n-2; i >= 0; i--){
dat[i] = calc(dat[i*2+1], dat[i*2+2]);
}
}
//k番目の値(0-indexed)をaに変更
void update(int k, T a){
k += n-1;
dat[k] = a;
while(k > 0){
k = (k-1)/2;
dat[k] = calc(dat[k*2+1], dat[k*2+2]);
}
}
//[a, b)
//区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
T query(int a, int b, int k=0, int l=0, int r=-1){
if(r < 0) r = n;
if(r <= a || b <= l) return UNIT;
if(a <= l && r <= b) return dat[k];
else{
T vl = query(a, b, k*2+1, l, (l+r)/2);
T vr = query(a, b, k*2+2, (l+r)/2, r);
return calc(vl, vr);
}
}
};
int N;
ll A[500000];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
cin >> N;
for(int i = 0; i < N; i++) cin >> A[i];
segtree<ll> sgt(N, 0);
for(int i = 0; i < N; i++) sgt.update(i, A[i]);
ll ans = 0;
for(int i = 0; i < N; i++){
int l = i, r = N-1;
if(A[i] == 1){
ans += (N-i);
}else if(sgt.query(i, N) != 1){
}else{
while(r-l > 1){
int c = (l+r)/2;
if(sgt.query(i, c+1) == 1) r = c;
else l = c;
}
ans += N-r;
}
// cout << ans << endl;
}
cout << ans << endl;
}
milanis48663220