結果

問題 No.1036 Make One With GCD 2
ユーザー 👑 emthrmemthrm
提出日時 2020-04-24 21:58:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,232 ms / 2,000 ms
コード長 2,442 bytes
コンパイル時間 2,558 ms
コンパイル使用メモリ 208,488 KB
実行使用メモリ 87,464 KB
最終ジャッジ日時 2023-08-06 23:09:54
合計ジャッジ時間 28,790 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 755 ms
87,272 KB
testcase_01 AC 795 ms
87,264 KB
testcase_02 AC 320 ms
87,464 KB
testcase_03 AC 52 ms
32,748 KB
testcase_04 AC 89 ms
58,372 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 271 ms
37,312 KB
testcase_08 AC 222 ms
31,332 KB
testcase_09 AC 816 ms
85,300 KB
testcase_10 AC 776 ms
79,692 KB
testcase_11 AC 835 ms
87,064 KB
testcase_12 AC 783 ms
80,312 KB
testcase_13 AC 1,217 ms
83,416 KB
testcase_14 AC 1,232 ms
84,196 KB
testcase_15 AC 1,153 ms
79,168 KB
testcase_16 AC 1,164 ms
79,908 KB
testcase_17 AC 1,197 ms
82,196 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 1,142 ms
78,396 KB
testcase_23 AC 855 ms
58,920 KB
testcase_24 AC 1,190 ms
81,048 KB
testcase_25 AC 1,081 ms
74,668 KB
testcase_26 AC 1,139 ms
77,032 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 1 ms
4,384 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 316 ms
87,340 KB
testcase_39 AC 888 ms
87,268 KB
testcase_40 AC 852 ms
58,828 KB
testcase_41 AC 787 ms
86,964 KB
testcase_42 AC 794 ms
87,168 KB
testcase_43 AC 697 ms
87,100 KB
testcase_44 AC 742 ms
87,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Semigroup>
struct DisjointSparseTable {
  using Fn = function<Semigroup(Semigroup, Semigroup)>;

  DisjointSparseTable(const vector<Semigroup> &a, Fn fn) : fn(fn) {
    int n = a.size(), table_h = 1;
    while ((1 << table_h) < n) ++table_h;
    lg.assign(1 << table_h, 0);
    FOR(i, 2, 1 << table_h) lg[i] = lg[i >> 1] + 1;
    dat.assign(table_h, vector<Semigroup>(n));
    REP(j, n) dat[0][j] = a[j];
    FOR(i, 1, table_h) {
      int shift = 1 << i;
      for (int left = 0; left < n; left += shift << 1) {
        int mid = min(left + shift, n);
        dat[i][mid - 1] = a[mid - 1];
        for (int j = mid - 2; j >= left; --j) dat[i][j] = fn(a[j], dat[i][j + 1]);
        if (n <= mid) break;
        int right = min(mid + shift, n);
        dat[i][mid] = a[mid];
        for (int j = mid + 1; j < right; ++j) dat[i][j] = fn(dat[i][j - 1], a[j]);
      }
    }
  }

  Semigroup query(int left, int right) {
    assert(left < right);
    if (left == --right) return dat[0][left];
    int h = lg[left ^ right];
    return fn(dat[h][left], dat[h][right]);
  }

private:
  Fn fn;
  vector<int> lg;
  vector<vector<Semigroup>> dat;
};

int main() {
  int n; cin >> n;
  vector<ll> a(n); REP(i, n) cin >> a[i];
  DisjointSparseTable<ll> dst(a, [](ll a, ll b) { return gcd(a, b); });
  ll ans = 0;
  REP(i, n) {
    if (a[i] == 1) {
      ans += n - i;
      continue;
    }
    int lb = i, ub = n;
    while (ub - lb > 1) {
      int mid = (lb + ub) >> 1;
      (dst.query(i, mid + 1) > 1 ? lb : ub) = mid;
    }
    ans += n - ub;
  }
  cout << ans << '\n';
  return 0;
}
0