結果

問題 No.1036 Make One With GCD 2
コンテスト
ユーザー ケネン
提出日時 2026-09-14 15:58:04
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,362 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,166 ms
コンパイル使用メモリ 355,784 KB
実行使用メモリ 42,624 KB
最終ジャッジ日時 2026-09-14 15:58:58
合計ジャッジ時間 12,115 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 17 WA * 24
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/segtree>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
using pll = pair<ll, ll>;

template <class S, auto op, auto e> struct ST {
  int n = 0;
  vector<vector<S>> v;
  ST(int _n) { 
    n = _n;
    v = vector<vector<S>>(bit_width<unsigned int>(n), vector<S>(n, e()));
  }
  ST(vector<S> _v) { 
    n = _v.size();
    v = vector<vector<S>>(bit_width<unsigned int>(n), vector<S>(n, e()));
    v[0] = _v;
  }
  void set(int i, S x) { v[0][i] = x; }
  void build() {
    for (int k = 1; k < v.size(); k++) {
      for (int i = 0; i <= n-(1<<k); i++) {
        v[k][i] = op(v[k-1][i], v[k-1][i+(1<<(k-1))]);
      }
    }
  }
  S query(int l, int r) {
    if (l >= r) return e();
    int k = bit_width<unsigned>(r-l)-1;
    return op(v[k][l], v[k][r-(1<<k)]);
  }
};

int op(int a, int b) { return gcd(a, b); }
int e() { return 1; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

  int n;
  cin >> n;
  ST<int, op, e> st(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    st.set(i, x);
  }
  st.build();

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    int l = i+1, r = n;
    while (l <= r) {
      int x = (l+r)/2;
      if (st.query(i, x) == 1) r = x-1;
      else l = x+1;
    }
    ans += n-r;
    // cout << i << ": " << n-r << "\n";
  }
  cout << ans;
}
0