結果

問題 No.1036 Make One With GCD 2
ユーザー rogi52rogi52
提出日時 2022-10-17 20:32:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 1,970 bytes
コンパイル時間 2,377 ms
コンパイル使用メモリ 206,492 KB
実行使用メモリ 15,516 KB
最終ジャッジ日時 2023-09-10 18:59:25
合計ジャッジ時間 14,941 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
7,672 KB
testcase_01 AC 155 ms
6,988 KB
testcase_02 AC 118 ms
15,516 KB
testcase_03 AC 16 ms
4,884 KB
testcase_04 AC 26 ms
5,652 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 49 ms
4,864 KB
testcase_08 AC 40 ms
4,380 KB
testcase_09 AC 181 ms
6,884 KB
testcase_10 AC 169 ms
6,436 KB
testcase_11 AC 185 ms
6,896 KB
testcase_12 AC 170 ms
6,736 KB
testcase_13 AC 298 ms
6,684 KB
testcase_14 AC 302 ms
6,804 KB
testcase_15 AC 284 ms
6,420 KB
testcase_16 AC 286 ms
6,420 KB
testcase_17 AC 294 ms
6,672 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 3 ms
4,384 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 280 ms
6,444 KB
testcase_23 AC 206 ms
5,660 KB
testcase_24 AC 292 ms
6,700 KB
testcase_25 AC 265 ms
6,348 KB
testcase_26 AC 277 ms
6,516 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,388 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,384 KB
testcase_34 AC 2 ms
4,504 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,384 KB
testcase_38 AC 113 ms
15,504 KB
testcase_39 AC 139 ms
6,940 KB
testcase_40 AC 207 ms
5,712 KB
testcase_41 AC 173 ms
6,920 KB
testcase_42 AC 171 ms
6,980 KB
testcase_43 AC 167 ms
9,364 KB
testcase_44 AC 171 ms
8,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

template <class T, class Op> class SWAG {
  private:
    class node {
      public:
        T val, sum;
        node(const T &val, const T &sum) : val(val), sum(sum) {}
    };
 
    Op op;
    std::stack<node> front_stack, back_stack;
 
  public:
    SWAG(const Op &op = Op()) : op(op), front_stack(), back_stack() {}
 
    bool empty() const { return front_stack.empty() && back_stack.empty(); }
 
    std::size_t size() const { return front_stack.size() + back_stack.size(); }
 
    T fold() const {
        assert(!empty());
        if(front_stack.empty()) return  back_stack.top().sum;
        if( back_stack.empty()) return front_stack.top().sum;
        return op(front_stack.top().sum, back_stack.top().sum);
    }
 
    void push(const T &x) {
        if(back_stack.empty()) {
            back_stack.emplace(x, x);
        } else {
            T s = op(back_stack.top().sum, x);
            back_stack.emplace(x, s);
        }
    }
 
    void pop() {
        assert(!empty());
        if(front_stack.empty()) {
            front_stack.emplace(back_stack.top().val, back_stack.top().val);
            back_stack.pop();
            while (!back_stack.empty()) {
                T s = op(back_stack.top().val, front_stack.top().sum);
                front_stack.emplace(back_stack.top().val, s);
                back_stack.pop();
            }
        }
        front_stack.pop();
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    int N; cin >> N;
    vector<ll> A(N);
    rep(i,N) cin >> A[i];
    
    auto op = [](ll x, ll y) { return gcd(x, y); };
    SWAG<ll,decltype(op)> swag(op);
    ll ans = 0;
    for(int L = 0, R = 0; L < N; L++) {
        while(swag.empty() || R < N && swag.fold() != 1LL) swag.push(A[R++]);
        if(swag.fold() == 1LL) ans += N - R + 1;
        swag.pop();
    }
    cout << ans << "\n";
}
0