結果

問題 No.1036 Make One With GCD 2
ユーザー HaarHaar
提出日時 2020-04-26 19:18:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,143 ms / 2,000 ms
コード長 1,966 bytes
コンパイル時間 3,519 ms
コンパイル使用メモリ 209,300 KB
実行使用メモリ 98,924 KB
最終ジャッジ日時 2023-08-11 05:26:06
合計ジャッジ時間 36,494 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,096 ms
98,564 KB
testcase_01 AC 787 ms
98,848 KB
testcase_02 AC 867 ms
98,856 KB
testcase_03 AC 196 ms
38,332 KB
testcase_04 AC 394 ms
65,416 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 303 ms
43,752 KB
testcase_08 AC 240 ms
36,288 KB
testcase_09 AC 787 ms
96,432 KB
testcase_10 AC 730 ms
89,852 KB
testcase_11 AC 813 ms
98,348 KB
testcase_12 AC 737 ms
90,904 KB
testcase_13 AC 938 ms
94,280 KB
testcase_14 AC 945 ms
95,128 KB
testcase_15 AC 890 ms
89,324 KB
testcase_16 AC 902 ms
90,216 KB
testcase_17 AC 925 ms
92,708 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 3 ms
4,384 KB
testcase_20 AC 4 ms
4,384 KB
testcase_21 AC 4 ms
4,384 KB
testcase_22 AC 893 ms
88,296 KB
testcase_23 AC 628 ms
65,520 KB
testcase_24 AC 906 ms
91,948 KB
testcase_25 AC 824 ms
83,792 KB
testcase_26 AC 856 ms
86,960 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,384 KB
testcase_32 AC 1 ms
4,384 KB
testcase_33 AC 2 ms
4,384 KB
testcase_34 AC 2 ms
4,384 KB
testcase_35 AC 2 ms
4,384 KB
testcase_36 AC 2 ms
4,384 KB
testcase_37 AC 1 ms
4,384 KB
testcase_38 AC 843 ms
98,568 KB
testcase_39 AC 1,080 ms
98,772 KB
testcase_40 AC 625 ms
65,588 KB
testcase_41 AC 1,118 ms
98,924 KB
testcase_42 AC 1,107 ms
98,512 KB
testcase_43 AC 1,140 ms
98,740 KB
testcase_44 AC 1,143 ms
98,568 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;
  }
};


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 std::gcd(a, b);}
};

int main(){
  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