結果

問題 No.1036 Make One With GCD 2
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2020-04-26 04:44:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 1,614 bytes
コンパイル時間 450 ms
コンパイル使用メモリ 53,236 KB
実行使用メモリ 15,036 KB
最終ジャッジ日時 2023-10-14 20:18:45
合計ジャッジ時間 8,020 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
7,156 KB
testcase_01 AC 112 ms
6,920 KB
testcase_02 AC 122 ms
15,000 KB
testcase_03 AC 17 ms
4,464 KB
testcase_04 AC 29 ms
5,528 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 40 ms
4,612 KB
testcase_08 AC 33 ms
4,356 KB
testcase_09 AC 147 ms
6,796 KB
testcase_10 AC 136 ms
6,552 KB
testcase_11 AC 150 ms
6,916 KB
testcase_12 AC 138 ms
6,576 KB
testcase_13 AC 234 ms
6,764 KB
testcase_14 AC 236 ms
6,756 KB
testcase_15 AC 222 ms
6,516 KB
testcase_16 AC 223 ms
6,520 KB
testcase_17 AC 230 ms
6,632 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 218 ms
6,508 KB
testcase_23 AC 160 ms
5,536 KB
testcase_24 AC 227 ms
6,608 KB
testcase_25 AC 206 ms
6,288 KB
testcase_26 AC 215 ms
6,428 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,348 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 119 ms
15,036 KB
testcase_39 AC 143 ms
6,952 KB
testcase_40 AC 161 ms
5,556 KB
testcase_41 AC 153 ms
7,004 KB
testcase_42 AC 154 ms
6,976 KB
testcase_43 AC 150 ms
9,160 KB
testcase_44 AC 153 ms
8,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <stack>

// using u64 = uint64_t;
 using u64 = unsigned long long int;

u64 bgcd(u64 x, u64 y){
  if(x == 0) return y;
  if(y == 0) return x;
  int bx = __builtin_ctzll(x);
  int by = __builtin_ctzll(y);
  int k = (bx < by) ? bx : by;
  x >>= bx;
  y >>= by;
  while(x!=y){
    if(x < y){
      y -= x;
      y >>= __builtin_ctzll(y);
    }
    else {
      x -= y;
      x >>= __builtin_ctzll(x);
    }
  }
  return x << k;
}


struct u64GCDMonoid {
  using element_type = u64;
  static u64 op(u64 x, u64 y) {
    return bgcd(x, y);
  }
  static constexpr u64 id = 0;
};

template <typename M>
struct swag {
  using T = typename M::element_type;
  std::stack<T> l, r, cl, cr;
  swag(){
    cl.push(M::id);
    cr.push(M::id);
  }
  void push_back(const T &x){ r.push(x); cr.push(M::op(cr.top(), x)); };
  void pop_front(){
    if(l.empty()){
      while(r.size() != 1){
        l.push(r.top());
        r.pop();
        cr.pop();
        cl.push(M::op(cl.top(), l.top()));
      }
      r.pop();
      cr.pop();
    }
    else {
      l.pop();
      cl.pop();
    }
  }
  T fold() const {
    return M::op(cl.top(), cr.top());
  }
};

int n;
u64 A[500001];

int main(){
  scanf("%d", &n);
  for(int i = 0; i < n; i++) scanf("%llu", A+i);
  
  swag<u64GCDMonoid> s;
  u64 ans = 0;
  int r = 0;
  s.push_back(0);
  for(int l = 0; l <= r; l++){
    s.pop_front();
    for(; r <= n; r++){
      if(s.fold() == 1){
// printf("(%d, %d) ", l, r-1);
        ans += n - r + 1;
        break;
      }
      s.push_back(A[r]);
    }
  }

  printf("%llu\n", ans);
  return 0;
}

0