結果

問題 No.1036 Make One With GCD 2
ユーザー Ricky_ponRicky_pon
提出日時 2020-04-24 22:09:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 2,543 bytes
コンパイル時間 2,113 ms
コンパイル使用メモリ 208,136 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2023-10-14 19:20:31
合計ジャッジ時間 9,612 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
7,516 KB
testcase_01 AC 130 ms
7,356 KB
testcase_02 AC 101 ms
15,452 KB
testcase_03 AC 17 ms
4,924 KB
testcase_04 AC 28 ms
6,044 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 47 ms
5,232 KB
testcase_08 AC 40 ms
4,792 KB
testcase_09 AC 157 ms
7,392 KB
testcase_10 AC 142 ms
7,020 KB
testcase_11 AC 155 ms
7,432 KB
testcase_12 AC 144 ms
7,056 KB
testcase_13 AC 239 ms
7,252 KB
testcase_14 AC 246 ms
7,508 KB
testcase_15 AC 224 ms
7,084 KB
testcase_16 AC 224 ms
7,088 KB
testcase_17 AC 231 ms
7,084 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 226 ms
7,048 KB
testcase_23 AC 162 ms
5,988 KB
testcase_24 AC 226 ms
7,196 KB
testcase_25 AC 212 ms
6,724 KB
testcase_26 AC 221 ms
6,928 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 1 ms
4,352 KB
testcase_31 AC 1 ms
4,356 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 1 ms
4,356 KB
testcase_34 AC 1 ms
4,348 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 2 ms
4,348 KB
testcase_38 AC 96 ms
15,488 KB
testcase_39 AC 130 ms
7,356 KB
testcase_40 AC 162 ms
5,980 KB
testcase_41 AC 147 ms
7,400 KB
testcase_42 AC 151 ms
7,504 KB
testcase_43 AC 141 ms
9,296 KB
testcase_44 AC 141 ms
8,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

template<typename T> struct SWAG{
    using P = pair<T, T>;
    stack<P> st_front, st_back;
    function<T(T, T)> f;

    SWAG(function<T(T, T)> f_): f(f_){}

    int size(){
        return st_front.size() + st_back.size();
    }

    bool empty(){
        return size() == 0;
    }

    T fold_all(){
        if(st_front.empty()) return st_back.top().se;
        if(st_back.empty()) return st_front.top().se;
        return f(st_front.top().se, st_back.top().se);
    }

    void push(T a){
        if(st_back.empty()) st_back.emplace(a, a);
        else st_back.emplace(a, f(st_back.top().se, a));
    }

    void pop(){
        if(!size()) return;
        if(!st_front.empty()) st_front.pop();
        else{
            while(!st_back.empty()){
                P tmp=st_back.top();
                if(st_front.empty()){
                    st_front.emplace(tmp.fi, tmp.fi);
                    st_back.pop();
                }
                else{
                    st_front.emplace(tmp.fi, f(tmp.fi, st_front.top().se));
                    st_back.pop();
                }
            }
            st_front.pop();
        }
    }
};

int main(){
    int n;
    scanf("%d", &n);
    lint a[n];
    rep(i, n) scanf("%lld", &a[i]);
    auto f = [](lint p, lint q){return gcd(p, q);};
    SWAG<lint> que(f);
    lint ans = 0;
    for(int l=0, r=1; l<n; ++l){
        if(que.empty()) que.push(a[l]);
        if(r <= l) r = l+1;
        while(r < n && que.fold_all() != 1){
            que.push(a[r]);
            ++r;
        }
        if(r == n && que.fold_all() != 1) break;
        ans += n-r+1;
        que.pop();
    }
    printf("%lld\n", ans);
}
0