結果
| 問題 |
No.1036 Make One With GCD 2
|
| コンテスト | |
| ユーザー |
monkukui2
|
| 提出日時 | 2020-04-24 21:58:52 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,308 bytes |
| コンパイル時間 | 1,007 ms |
| コンパイル使用メモリ | 100,060 KB |
| 実行使用メモリ | 29,168 KB |
| 最終ジャッジ日時 | 2024-11-07 02:19:23 |
| 合計ジャッジ時間 | 9,440 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | -- * 4 |
| other | TLE * 1 -- * 40 |
ソースコード
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
/* do your best */
// a, b の最大公約数を返す O( log max(a, b) )
long long gcd(long long a, long long b) {
if(b == 0) return a;
return gcd(b, a % b);
}
// quoted from beet-aizu
template <typename T, typename E, typename F, typename G>
struct SegmentTree{
// using F = function<T(T, T)>
// using G = function<T(T, E)>
int n;
F f;
G g;
T ti;
vector<T> dat;
SegmentTree(){};
SegmentTree(F f,G g,T ti):f(f),g(g),ti(ti){}
void init(int n_){
n=1;
while(n<n_) n<<=1;
dat.assign(n<<1,ti);
}
void build(const vector<T> &v){
int n_=v.size();
init(n_);
for(int i=0;i<n_;i++) dat[n+i]=v[i];
for(int i=n-1;i;i--)
dat[i]=f(dat[(i<<1)|0],dat[(i<<1)|1]);
}
void update(int k,const E &x){
k += n;
dat[k] = g(dat[k], x);
while(k>>=1)
dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
}
T operator [](int k) const { return dat[k+n]; }
T query(int a,int b) const {
T vl=ti,vr=ti;
for(int l=a+n,r=b+n;l<r;l>>=1,r>>=1) {
if(l&1) vl=f(vl,dat[l++]);
if(r&1) vr=f(dat[--r],vr);
}
return f(vl,vr);
}
/* TODO わからない 聞く
template<typename C>
int find(int a,int b,C &check,int k,int l,int r){
if(!check(dat[k])||r<=a||b<=l) return -1;
if(k>=n) return k-n;
int m=(l+r)>>1;
int vl=find(a,b,check,(k<<1)|0,l,m);
if(~vl) return vl;
return find(a,b,check,(k<<1)|1,m,r);
}
template<typename C>
int find(int a,int b,C &check){
return find(a,b,check,1,0,n);
}*/
};
/** テンプレ
int main(){
}
**/
int main() {
int n; cin >> n;
vector<lint> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
using T = lint; // type T
using E = lint; // type E
auto f = [](T a, T b){ // return type T value
return gcd(a, b);
};
auto g = [](T a, E b){ // return type T value
return b; // return b;
};
T ti = 0; // identity element
SegmentTree<T, E, decltype(f), decltype(g)> sg(f, g, ti); // don't change
sg.build(a);
lint ans = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 1) continue;
int l = i;
int r = n + 1;
while (r - l > 1) {
int mid = (r + l) / 2;
lint g = sg.query(i, mid);
if (g != 1) l = mid;
else r = mid;
}
ans += (l - i);
}
cout << n * (n + 1) / 2 - ans << endl;
return 0;
}
monkukui2