結果

問題 No.1036 Make One With GCD 2
ユーザー HaarHaar
提出日時 2020-05-20 18:30:40
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,473 bytes
コンパイル時間 2,271 ms
コンパイル使用メモリ 205,420 KB
実行使用メモリ 99,108 KB
最終ジャッジ日時 2024-04-09 23:05:02
合計ジャッジ時間 30,460 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 939 ms
98,800 KB
testcase_01 AC 525 ms
98,980 KB
testcase_02 AC 1,143 ms
99,056 KB
testcase_03 AC 134 ms
38,424 KB
testcase_04 AC 270 ms
65,428 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 249 ms
43,896 KB
testcase_08 AC 197 ms
36,448 KB
testcase_09 AC 653 ms
96,556 KB
testcase_10 AC 605 ms
90,012 KB
testcase_11 AC 656 ms
98,660 KB
testcase_12 AC 614 ms
91,068 KB
testcase_13 AC 843 ms
94,436 KB
testcase_14 AC 850 ms
95,616 KB
testcase_15 AC 790 ms
89,696 KB
testcase_16 AC 800 ms
90,380 KB
testcase_17 AC 825 ms
93,004 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 3 ms
6,944 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 4 ms
6,944 KB
testcase_22 AC 787 ms
88,352 KB
testcase_23 AC 572 ms
65,848 KB
testcase_24 AC 812 ms
92,068 KB
testcase_25 AC 739 ms
84,208 KB
testcase_26 AC 771 ms
87,080 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 1 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 2 ms
6,940 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 TLE -
testcase_39 AC 825 ms
98,964 KB
testcase_40 AC 571 ms
66,056 KB
testcase_41 AC 877 ms
99,016 KB
testcase_42 AC 873 ms
98,800 KB
testcase_43 AC 964 ms
98,800 KB
testcase_44 AC 968 ms
98,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


template <typename Semilattice>
class SparseTable{
  using value_type = typename Semilattice::value_type;
  
  std::vector<std::vector<value_type>> a;
  std::vector<int> log_table;
  
public:
  SparseTable(const std::vector<value_type> &v){
    int n = v.size();
    int logn = 0;
    while((1 << logn) <= n) ++logn;
    
    a.assign(n, std::vector<value_type>(logn));
    for(int i = 0; i < n; ++i) a[i][0] = v[i];
    for(int j = 1; j < logn; ++j){
      for(int i = 0; i < n; ++i){
        a[i][j] = Semilattice::op(a[i][j-1], a[std::min<int>(n-1, i+(1<<(j-1)))][j-1]);
      }
    }

    log_table.assign(n+1, 0);
    for(int i = 2; i < n+1; ++i) log_table[i] = log_table[i>>1] + 1;
  }
  
  inline value_type get(int s, int t) const { // [s,t)
    int k = log_table[t-s];
    return Semilattice::op(a[s][k], a[t-(1<<k)][k]);
  }

  inline value_type get(std::vector<std::pair<int,int>> st) const {
    value_type ret;
    bool t = true;

    for(const auto &p : st){
      if(p.first < p.second){
        if(t){
          ret = get(p.first, p.second);
          t = false;
        }else{
          ret = Semilattice::op(ret, get(p.first, p.second));
        }
      }
    }

    return ret;
  }
};


int64_t binary_gcd(int64_t a, int64_t b){
  int64_t g = 1;

  while(1){
    if(a > b) std::swap(a, b);
    
    if(a == 0){
      break;
    }else{
      if((a & 1) == 0 and (b & 1) == 0){
        a >>= 1;
        b >>= 1;
        g <<= 1;
      }else if((a & 1) == 0){
        a >>= 1;
      }else if((b & 1) == 0){
        b >>= 1;
      }else{
        int64_t t = std::abs(a - b) >> 1;
        b = t;
      }
    }
  }

  return g * b;
}






template <typename T>
struct GcdMonoid{
  using value_type = T;
  constexpr inline static value_type id(){return 0;}
  constexpr inline static value_type op(const value_type &a, const value_type &b){return binary_gcd(a, b);}
};

int main(){
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  
  int N;

  while(std::cin >> N){
    std::vector<int64_t> A(N);
    for(int i = 0; i < N; ++i) std::cin >> A[i];

    SparseTable<GcdMonoid<int64_t>> s(A);

    int64_t ans = 0;

    for(int l = 0; l < N; ++l){
      int lb = l, ub = N+1;
      while(abs(lb-ub) > 1){
        int mid = (lb + ub) / 2;

        if(s.get(l, mid) == 1){
          ub = mid;
        }else{
          lb = mid;
        }
      }
      
      ans += (N - lb);
    }

    std::cout << ans << "\n";
  }

  return 0;
}
0