結果

問題 No.1036 Make One With GCD 2
ユーザー Ryuhei Mori
提出日時 2020-04-26 04:32:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 1,541 bytes
コンパイル時間 942 ms
コンパイル使用メモリ 51,692 KB
最終ジャッジ日時 2025-01-10 01:51:25
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 41
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:68:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   68 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
main.cpp:69:35: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   69 |   for(int i = 0; i < n; i++) scanf("%llu", A+i);
      |                              ~~~~~^~~~~~~~~~~~~

ソースコード

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.empty()){
        l.push(r.top());
        r.pop();
        cr.pop();
        cl.push(M::op(cl.top(), l.top()));
      }
    }
    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;
  for(int l = 0; l <= r; l++){
    for(; r <= n; r++){
      if(s.fold() == 1){
// printf("(%d, %d) ", l, r-1);
        ans += n - r + 1;
        break;
      }
      s.push_back(A[r]);
    }
    s.pop_front();
  }

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

0