結果

問題 No.1036 Make One With GCD 2
ユーザー tnakao0123tnakao0123
提出日時 2020-04-25 16:00:15
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 841 ms / 2,000 ms
コード長 2,198 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 96,260 KB
実行使用メモリ 11,916 KB
最終ジャッジ日時 2023-10-14 19:48:08
合計ジャッジ時間 15,477 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 402 ms
11,856 KB
testcase_01 AC 140 ms
11,752 KB
testcase_02 AC 270 ms
11,772 KB
testcase_03 AC 36 ms
7,544 KB
testcase_04 AC 64 ms
11,744 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 56 ms
7,628 KB
testcase_08 AC 46 ms
7,620 KB
testcase_09 AC 331 ms
11,768 KB
testcase_10 AC 309 ms
11,792 KB
testcase_11 AC 339 ms
11,852 KB
testcase_12 AC 311 ms
11,672 KB
testcase_13 AC 532 ms
11,772 KB
testcase_14 AC 538 ms
11,716 KB
testcase_15 AC 505 ms
11,804 KB
testcase_16 AC 508 ms
11,856 KB
testcase_17 AC 522 ms
11,856 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,372 KB
testcase_20 AC 3 ms
4,348 KB
testcase_21 AC 3 ms
4,352 KB
testcase_22 AC 497 ms
11,776 KB
testcase_23 AC 367 ms
11,916 KB
testcase_24 AC 517 ms
11,840 KB
testcase_25 AC 472 ms
11,656 KB
testcase_26 AC 490 ms
11,776 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 1 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,356 KB
testcase_37 AC 2 ms
4,352 KB
testcase_38 AC 266 ms
11,720 KB
testcase_39 AC 841 ms
11,796 KB
testcase_40 AC 368 ms
11,664 KB
testcase_41 AC 539 ms
11,728 KB
testcase_42 AC 536 ms
11,792 KB
testcase_43 AC 491 ms
11,848 KB
testcase_44 AC 532 ms
11,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1036.cc:  No.1036 Make One With GCD 2 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 500000;
const int MAX_E2 = 1 << 20; // = 1048576

/* typedef */

typedef long long ll;

template <typename T, const int MAX_E2>
struct SegTreeGCD {
  int e2;
  T nodes[MAX_E2];
  SegTreeGCD() {}

  void init(int n) {
    for (e2 = 1; e2 < n; e2 <<= 1);
  }

  T &get(int i) { return nodes[e2 - 1 + i]; }
  void set(int i, T v) { get(i) = v; }

  T __gcd(T m, T n) {
    if (m == 0) return n;
    if (n == 0) return m;
    if (m < n) swap(m, n);
    while (n > 0) {
      T r = m % n;
      m = n; n = r;
    }
    return m;
  }
  
  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = __gcd(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  T gcd_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return 0;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = gcd_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = gcd_range(r0, r1, k * 2 + 2, im, i1);
    return __gcd(v0, v1);
  }
  T gcd_range(int r0, int r1) { return gcd_range(r0, r1, 0, 0, e2); }
};

/* global variables */

SegTreeGCD<ll,MAX_E2> st;

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  st.init(n);

  for (int i = 0; i < n; i++) {
    ll ai;
    scanf("%lld", &ai);
    st.set(i, ai);
  }
  st.setall();

  //for (int i = 0; i < n; i++) {
  //for (int j = i; j <= n; j++) printf("%lld ", st.gcd_range(i, j));
  //putchar('\n');
  //}

  ll sum = 0;
  for (int i = 0, j = 0; i < n; i++) {
    if (j < i) j = i;
    ll gi = st.gcd_range(i, j);
    while (j < n) {
      ll gj = st.__gcd(gi, st.get(j));
      if (gj == 1) break;
      gi = gj;
      j++;
    }

    sum += j - i;
    //printf("%d: %d\n", i, j0 - i);
  }

  printf("%lld\n", (ll)n * (n + 1) / 2 - sum);
  return 0;
}
0