結果

問題 No.1036 Make One With GCD 2
ユーザー Ryuhei MoriRyuhei Mori
提出日時 2020-04-26 05:34:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 150 ms / 2,000 ms
コード長 3,004 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 54,556 KB
実行使用メモリ 27,312 KB
最終ジャッジ日時 2023-10-14 20:18:51
合計ジャッジ時間 5,941 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
19,544 KB
testcase_01 AC 53 ms
15,036 KB
testcase_02 AC 35 ms
27,312 KB
testcase_03 AC 6 ms
10,352 KB
testcase_04 AC 9 ms
10,900 KB
testcase_05 AC 2 ms
6,880 KB
testcase_06 AC 1 ms
6,880 KB
testcase_07 AC 18 ms
10,572 KB
testcase_08 AC 14 ms
10,252 KB
testcase_09 AC 88 ms
15,028 KB
testcase_10 AC 77 ms
15,004 KB
testcase_11 AC 86 ms
15,028 KB
testcase_12 AC 79 ms
15,092 KB
testcase_13 AC 146 ms
19,064 KB
testcase_14 AC 150 ms
19,092 KB
testcase_15 AC 134 ms
17,088 KB
testcase_16 AC 134 ms
19,148 KB
testcase_17 AC 133 ms
19,136 KB
testcase_18 AC 2 ms
6,840 KB
testcase_19 AC 2 ms
6,872 KB
testcase_20 AC 2 ms
6,828 KB
testcase_21 AC 2 ms
6,908 KB
testcase_22 AC 138 ms
17,076 KB
testcase_23 AC 97 ms
15,020 KB
testcase_24 AC 138 ms
19,132 KB
testcase_25 AC 125 ms
17,108 KB
testcase_26 AC 131 ms
17,072 KB
testcase_27 AC 2 ms
6,852 KB
testcase_28 AC 2 ms
6,868 KB
testcase_29 AC 2 ms
6,952 KB
testcase_30 AC 2 ms
6,848 KB
testcase_31 AC 3 ms
6,856 KB
testcase_32 AC 2 ms
6,848 KB
testcase_33 AC 2 ms
6,924 KB
testcase_34 AC 2 ms
6,796 KB
testcase_35 AC 2 ms
6,820 KB
testcase_36 AC 2 ms
6,868 KB
testcase_37 AC 2 ms
6,924 KB
testcase_38 AC 33 ms
27,308 KB
testcase_39 AC 58 ms
19,164 KB
testcase_40 AC 98 ms
15,028 KB
testcase_41 AC 70 ms
19,188 KB
testcase_42 AC 66 ms
19,228 KB
testcase_43 AC 64 ms
21,048 KB
testcase_44 AC 66 ms
19,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <stack>

#include <unistd.h>

struct IO {
  static const int bufsize=1<<24;
  char ibuf[bufsize], obuf[bufsize];
  char *ip, *op;
  IO(): ip(ibuf), op(obuf) { for(int t = 0, k = 0; (k = read(STDIN_FILENO, ibuf+t, sizeof(ibuf)-t)) > 0; t+=k); }
  ~IO(){ for(int t = 0, k = 0; (k = write(STDOUT_FILENO, obuf+t, op-obuf-t)) > 0; t+=k); }
  long long scan_int(){
    long long x=0;
    bool neg=false;
    for(;*ip<'+';ip++) ;
    if(*ip=='-'){ neg=true; ip++;}
    else if(*ip=='+') ip++;
    for(;*ip>='0';ip++) x = 10*x+*ip-'0';
    if(neg) x = -x;
    return x;
  }
  void put_int(long long x, char c=0){
    static char tmp[20];
    if(x==0) *op++ = '0';
    else {
      int i;
      if(x<0){
        *op++ = '-';
        x = -x;
      }
      for(i=0; x; i++){
        tmp[i] = x % 10;
        x /= 10;
      }
      for(i--; i>=0; i--)
        *op++ = tmp[i]+'0';
    }
    if(c) *op++ = c;
  }
  void put_double(double x, char c=0){
    unsigned y;
    const int mask = (1<<24) - 1;
    put_int(x);
    *op++ = '.';
    x = x - (int) x;
    if(x < 0) x = -x;
    y = x * (1<<24);

    for(int i=0;i<7;i++){
      y *= 10;
      *op++ = '0' + (y>>24);
      y &= mask;
    }
    if(c) *op++ = c;
  }
  inline char scan_char(){ return *ip++; }
  inline void put_char(char c){ *op++ = c; }
  inline char *scan_string(){ char *s = ip; while(*ip!='\n'&&*ip!=' ') ip++; *ip++='\0'; return s;}
  inline void put_string(const char *s, char c=0){ while(*s) *op++=*s++; if(c) *op++=c;}
} io;


using u64 = unsigned long long int;

u64 bgcd(u64 x, u64 y){
  if(x == 0 || y == 0) return x^y;
  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> 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(cl.size() == 1){
      while(r.size() != 1){
        cl.push(M::op(cl.top(), r.top()));
        r.pop();
        cr.pop();
      }
      r.pop();
      cr.pop();
    }
    else {
      cl.pop();
    }
  }
  T fold() const {
    return M::op(cl.top(), cr.top());
  }
};

int n;
u64 A[500001];

int main(){
  n = io.scan_int();
  for(int i = 0; i < n; i++) A[i] = io.scan_int();
  
  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]);
    }
  }

  io.put_int(ans, '\n');
  return 0;
}

0