結果

問題 No.1036 Make One With GCD 2
ユーザー ChanyuhChanyuh
提出日時 2020-04-25 19:54:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 846 ms / 2,000 ms
コード長 3,050 bytes
コンパイル時間 1,967 ms
コンパイル使用メモリ 106,592 KB
実行使用メモリ 15,344 KB
最終ジャッジ日時 2023-10-14 20:01:11
合計ジャッジ時間 15,405 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 406 ms
15,140 KB
testcase_01 AC 95 ms
15,236 KB
testcase_02 AC 262 ms
15,156 KB
testcase_03 AC 19 ms
8,500 KB
testcase_04 AC 32 ms
14,096 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 37 ms
8,708 KB
testcase_08 AC 31 ms
8,432 KB
testcase_09 AC 260 ms
15,156 KB
testcase_10 AC 245 ms
14,896 KB
testcase_11 AC 266 ms
15,112 KB
testcase_12 AC 245 ms
14,820 KB
testcase_13 AC 446 ms
14,896 KB
testcase_14 AC 452 ms
15,132 KB
testcase_15 AC 423 ms
14,576 KB
testcase_16 AC 425 ms
14,756 KB
testcase_17 AC 442 ms
14,892 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 2 ms
4,368 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 417 ms
14,568 KB
testcase_23 AC 309 ms
13,936 KB
testcase_24 AC 433 ms
14,912 KB
testcase_25 AC 395 ms
14,580 KB
testcase_26 AC 414 ms
14,776 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 2 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 2 ms
4,368 KB
testcase_32 AC 2 ms
4,372 KB
testcase_33 AC 1 ms
4,372 KB
testcase_34 AC 2 ms
4,372 KB
testcase_35 AC 2 ms
4,368 KB
testcase_36 AC 2 ms
4,368 KB
testcase_37 AC 1 ms
4,368 KB
testcase_38 AC 259 ms
15,148 KB
testcase_39 AC 846 ms
15,224 KB
testcase_40 AC 309 ms
13,948 KB
testcase_41 AC 524 ms
15,196 KB
testcase_42 AC 521 ms
15,344 KB
testcase_43 AC 481 ms
15,224 KB
testcase_44 AC 518 ms
15,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

template <typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  F f;//二項演算
  T ti;//単位元
  vector<T> dat;

  SegmentTree(){}
  SegmentTree(F f,T ti):f(f),ti(ti){}

  void init(int n_){//sizeがn_のsegtreeを作る
    n=1;
    while(n<n_) n<<=1;
    dat.assign(n<<1,ti);
  }

  void build(const vector<T> &v){//vによってsegtreeをbuildする
    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 set_val(int k,T x){//k番目をxにする
    dat[k+=n]=x;
    while(k>>=1)
      dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]);
  }

  T query(int a,int b){//区間[a,b)に対しFを適応した値を返す
    if(a>=b) return ti;
    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);
  }

  template<typename C>
  int find(int st,C &check,T &acc,int k,int l,int r){//
    if(l+1==r){
      acc=f(acc,dat[k]);
      return check(acc)?k-n:-1;
    }
    int m=(l+r)>>1;
    if(m<=st) return find(st,check,acc,(k<<1)|1,m,r);
    if(st<=l&&!check(f(acc,dat[k]))){
      acc=f(acc,dat[k]);
      return -1;
    }
    int vl=find(st,check,acc,(k<<1)|0,l,m);
    if(~vl) return vl;
    return find(st,check,acc,(k<<1)|1,m,r);
  }

  template<typename C>
  int find(int st,C &check){
    T acc=ti;
    return find(st,check,acc,1,0,n);
  }

  T &operator [] (int i) {return dat[i+n];};
};

ll gcd(ll a,ll b){
  if(a<b) swap(a,b);
  if(b==0) return a;
  return gcd(b,a%b);
}

int n;
vector<ll> as;

void solve(){
  cin >> n;
  auto f=[](ll a,ll b){
    return gcd(a,b);
  };
  as.resize(n+1);
  SegmentTree<ll> seg(f,0);
  rep(i,n){
    cin >> as[i];
  }
  as[n]=1;
  seg.build(as);
  int r=1;
  ll ans=0;
  rep(l,n){
    ll g=seg.query(l,r);
    while(g!=1){
      g=gcd(g,as[r]);
      r+=1;
    }
    ans+=n-r+1;
  }
  cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}
0