結果

問題 No.1036 Make One With GCD 2
ユーザー queeequeee
提出日時 2020-04-24 22:26:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,284 ms / 2,000 ms
コード長 1,941 bytes
コンパイル時間 899 ms
コンパイル使用メモリ 81,928 KB
実行使用メモリ 15,348 KB
最終ジャッジ日時 2023-10-14 19:25:13
合計ジャッジ時間 24,390 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 845 ms
15,116 KB
testcase_01 AC 486 ms
15,348 KB
testcase_02 AC 459 ms
15,176 KB
testcase_03 AC 123 ms
8,628 KB
testcase_04 AC 225 ms
13,672 KB
testcase_05 AC 1 ms
4,356 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 201 ms
8,664 KB
testcase_08 AC 162 ms
8,616 KB
testcase_09 AC 601 ms
14,796 KB
testcase_10 AC 558 ms
14,724 KB
testcase_11 AC 617 ms
15,260 KB
testcase_12 AC 564 ms
15,020 KB
testcase_13 AC 826 ms
14,872 KB
testcase_14 AC 837 ms
14,916 KB
testcase_15 AC 782 ms
14,528 KB
testcase_16 AC 789 ms
15,024 KB
testcase_17 AC 813 ms
15,120 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 3 ms
4,352 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 4 ms
4,352 KB
testcase_22 AC 774 ms
14,948 KB
testcase_23 AC 568 ms
14,192 KB
testcase_24 AC 805 ms
14,952 KB
testcase_25 AC 733 ms
14,692 KB
testcase_26 AC 760 ms
14,864 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,356 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 1 ms
4,352 KB
testcase_38 AC 710 ms
15,112 KB
testcase_39 AC 1,284 ms
15,056 KB
testcase_40 AC 570 ms
14,036 KB
testcase_41 AC 1,085 ms
15,136 KB
testcase_42 AC 1,077 ms
15,172 KB
testcase_43 AC 1,020 ms
15,108 KB
testcase_44 AC 1,090 ms
15,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std; typedef long long ll; const int INF=1e9;
typedef pair<int,int> P;

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

template <typename T>
struct seg_pL {
  // 一点更新、区間取得 1~N
  using F = function<T(T,T)>;
  // f: [](int x, int y){return min(x,y);}
  // g: [](int x, int y){return y;} // 新しい者が常に正しい!

  int n0, n1; const F f, g; const T first_t; vector<T> seg;
  // f: 子の計算 g: 親と子(f)の計算

  seg_pL(int n, const F _f, const F _g, const T& _t) : f(_f), g(_g), first_t(_t) {
    n0 = n, n1 = 1; while(n0 > n1) n1<<=1; seg.assign(2*n1, first_t);
  }

  void set(int k, const T& _t) {seg[k+n1]=_t;} // 1~Nのk番目に値を入れる
  void bulid() { for(int k=n1-1; k>=0; k--) seg[k] = f(seg[2*k], seg[2*k+1]);}

  void update(int k, const T& t) {
    k += n1; seg[k] = g(seg[k], t); k /= 2;
    while(k > 0) {seg[k] = g(seg[k], f(seg[2*k], seg[2*k+1])); k /= 2;}
    //cout << seg << endl;
  }

  T query(int a, int b, int k, int l, int r) { // (0, 4+1)
    if (r<=a || l>=b) return first_t;
    if (a<=l && r<=b) return seg[k];
    else {
      T q1 = query(a, b, 2*k, l, (l+r)/2);
      T q2 = query(a, b, 2*k+1, (l+r)/2, r);
      //cout << q1 << " " << q2 << endl;
      return f(q1, q2);
    }
  }

  T query(int a, int b) {
    return query(a, b, 1, 0, n1);
  }
};


int main() {
  int n; cin>>n;
  ll a[n]; for(int i=0;i<n;i++) cin>>a[i];

  seg_pL<ll> seg(n, [](ll x, ll y){return gcd(x,y);}, [](ll x,ll y){return y;}, 0);
  for(int i=0;i<n;i++) seg.set(i, a[i]);
  seg.bulid();

  ll an=0, ii=0, jj=0;
  while(jj<n) {
    if (ii == jj) {
      if (seg.query(ii, jj+1) == 1) an++;
      jj++;
    }
    else if (seg.query(ii, jj+1) == 1) {
      an += n - jj; ii++;
    } else {
      jj++;
    }
    //cout<<ii<<" "<<jj<<" "<<an<<endl;
  }
  cout<<an<<endl;
}
0