結果

問題 No.1036 Make One With GCD 2
ユーザー seriru13seriru13
提出日時 2020-04-26 17:06:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,224 ms / 2,000 ms
コード長 3,323 bytes
コンパイル時間 2,334 ms
コンパイル使用メモリ 210,192 KB
実行使用メモリ 15,444 KB
最終ジャッジ日時 2023-10-14 20:21:07
合計ジャッジ時間 23,883 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 800 ms
15,116 KB
testcase_01 AC 394 ms
15,112 KB
testcase_02 AC 472 ms
15,204 KB
testcase_03 AC 62 ms
8,536 KB
testcase_04 AC 111 ms
13,596 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 143 ms
8,740 KB
testcase_08 AC 117 ms
8,628 KB
testcase_09 AC 509 ms
15,196 KB
testcase_10 AC 475 ms
14,668 KB
testcase_11 AC 517 ms
15,140 KB
testcase_12 AC 478 ms
14,900 KB
testcase_13 AC 738 ms
14,964 KB
testcase_14 AC 745 ms
15,040 KB
testcase_15 AC 705 ms
14,984 KB
testcase_16 AC 704 ms
15,048 KB
testcase_17 AC 727 ms
14,904 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,352 KB
testcase_20 AC 4 ms
4,352 KB
testcase_21 AC 4 ms
4,372 KB
testcase_22 AC 690 ms
14,660 KB
testcase_23 AC 510 ms
13,696 KB
testcase_24 AC 717 ms
15,156 KB
testcase_25 AC 654 ms
14,724 KB
testcase_26 AC 680 ms
14,756 KB
testcase_27 AC 2 ms
4,368 KB
testcase_28 AC 2 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 2 ms
4,372 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 611 ms
15,220 KB
testcase_39 AC 1,224 ms
15,280 KB
testcase_40 AC 508 ms
13,792 KB
testcase_41 AC 1,026 ms
15,264 KB
testcase_42 AC 1,011 ms
15,444 KB
testcase_43 AC 989 ms
15,128 KB
testcase_44 AC 1,052 ms
15,132 KB
権限があれば一括ダウンロードができます

ソースコード

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