結果

問題 No.1036 Make One With GCD 2
ユーザー seriru13
提出日時 2020-04-26 17:06:27
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,527 ms / 2,000 ms
コード長 3,323 bytes
コンパイル時間 4,575 ms
コンパイル使用メモリ 204,320 KB
最終ジャッジ日時 2025-01-10 01:59:38
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 10e17
#define rep(i,n) for(long long i=0; i<n; i++)
#define repr(i,n,m) for(long long i=m; i<n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x) for (auto&& itr : x) { cerr << (itr) << " "; }

template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t;}
template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t;}

template <typename T>
class SegTree {
  int n;                       // 葉の数
  vector<T> data;              // データを格納するvector
  T def;                       // 初期値かつ単位元
  function<T(T, T)> operation; // 区間クエリで使う処理
  function<T(T, T)> update;    // 点更新で使う処理

  // 区間[a, b)の総和。ノードk=[l, r)に着目している。
  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return def; // 交差しない
    if (a <= l && r <= b)
      return data[k]; // a,l,r,bの順で完全に含まれる
    else {
      T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子
      T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子
      return operation(c1, c2);
    }
  }

public:
  /**
   * コンストラクタ
   * @param _n 必要サイズ
   * @param _def 初期値かつ単位元
   * @param _operation クエリ関数
   * @param _update 更新関数
   */
  SegTree(size_t _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update)
  : def(_def), operation(_operation), update(_update) {
    n = 1;
    while (n < _n) {
      n *= 2;
    }
    data = vector<T>(2 * n - 1, def);
  }

  SegTree(vector<T>& A, T _def, function<T(T, T)> _operation, function<T(T, T)> _update)
  : def(_def), operation(_operation), update(_update) {
    n = 1;
    auto _n = A.size();
    while (n < _n) {
      n *= 2;
    }
    data.resize(2 * n, _def);
    for (int i = 0; i < _n; ++i) data[i + n - 1] = A[i];
    for (int i = n - 2; i >= 0; --i) data[i] = operation(data[i*2+1], data[i*2+2]);
  }

  /**
   * [a, b)の区間クエリを実行
   * @param a 左端
   * @param b 右端
   * @return クエリの結果
   */
  T query(int a, int b) {
    return _query(a, b, 0, 0, n);
  }

  /**
   * 場所i(0-indexed)の値をxで更新
   * @param i
   * @param x
   */
  void change(int i, T x) {
    i += n - 1;
    data[i] = update(data[i], x);
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }

  T operator[](int i) {
    return data[i + n - 1];
  }
};

// 最大公約数を求める(ユークリッドの互除法).
long long gcd (long long x, long long y) {
  if (y > x) swap(x,y);
  if (y == 0) return x;
  return gcd(x%y,y);
}

signed main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  repr(i, n, 0) {
    cin >> a[i];
  }

  SegTree<ll> segTree(a, 0,
      [](auto const a, auto const b){ return gcd(a,b); },
      [](auto const a, auto const b){ return b; });

  ll ans = 0;
  ll r = 0;
  for (ll l = 0; l < n; ++l) {
    if (l > r) r = l;
    while (r < n and segTree.query(l, r + 1) > 1) {
      r ++;
    }
    ans += n - r;
  }
  cout << ans << endl;
}
0