結果

問題 No.1036 Make One With GCD 2
ユーザー 👑 jupirojupiro
提出日時 2020-04-25 20:22:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 2,817 bytes
コンパイル時間 1,614 ms
コンパイル使用メモリ 137,900 KB
実行使用メモリ 15,556 KB
最終ジャッジ日時 2023-10-14 20:02:24
合計ジャッジ時間 9,649 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
7,612 KB
testcase_01 AC 148 ms
6,964 KB
testcase_02 AC 110 ms
15,556 KB
testcase_03 AC 16 ms
4,988 KB
testcase_04 AC 30 ms
5,700 KB
testcase_05 AC 1 ms
4,356 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 51 ms
4,664 KB
testcase_08 AC 38 ms
4,396 KB
testcase_09 AC 170 ms
7,088 KB
testcase_10 AC 169 ms
6,492 KB
testcase_11 AC 171 ms
6,996 KB
testcase_12 AC 157 ms
6,928 KB
testcase_13 AC 279 ms
6,704 KB
testcase_14 AC 284 ms
7,048 KB
testcase_15 AC 258 ms
6,784 KB
testcase_16 AC 269 ms
6,968 KB
testcase_17 AC 277 ms
6,784 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 2 ms
4,352 KB
testcase_22 AC 256 ms
6,368 KB
testcase_23 AC 190 ms
5,704 KB
testcase_24 AC 267 ms
6,712 KB
testcase_25 AC 244 ms
6,484 KB
testcase_26 AC 252 ms
6,388 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,356 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 2 ms
4,352 KB
testcase_31 AC 1 ms
4,352 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 1 ms
4,348 KB
testcase_36 AC 1 ms
4,352 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 106 ms
15,512 KB
testcase_39 AC 131 ms
6,956 KB
testcase_40 AC 196 ms
5,936 KB
testcase_41 AC 158 ms
7,324 KB
testcase_42 AC 154 ms
6,892 KB
testcase_43 AC 158 ms
9,076 KB
testcase_44 AC 159 ms
8,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

template< typename SemiGroup >
struct SlidingWindowAggregation {
    using F = function< SemiGroup(SemiGroup, SemiGroup) >;

    struct Node {
        SemiGroup val, sum;

        Node(const SemiGroup& val, const SemiGroup& sum) : val(val), sum(sum) {}
    };

    SlidingWindowAggregation(F f) : f(f) {}


    const F f;
    stack< Node > front, back;

    bool empty() const {
        return front.empty() && back.empty();
    }

    size_t size() const {
        return front.size() + back.size();
    }

    SemiGroup fold_all() const {
        if (front.empty()) {
            return back.top().sum;
        }
        else if (back.empty()) {
            return front.top().sum;
        }
        else {
            return f(front.top().sum, back.top().sum);
        }
    }

    void push(const SemiGroup& x) {
        if (back.empty()) {
            back.emplace(x, x);
        }
        else {
            back.emplace(x, f(back.top().sum, x));
        }
    }

    void pop() {
        if (front.empty()) {
            front.emplace(back.top().val, back.top().val);
            back.pop();
            while (!back.empty()) {
                front.emplace(back.top().val, f(back.top().val, front.top().sum));
                back.pop();
            }
        }
        front.pop();
    }
};

ll f(ll a, ll b) {
    return gcd(a, b);
}

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
    ll N; scanf("%lld", &N);
    vector<ll> a(N); for (int i = 0; i < N; i++) scanf("%lld", &a[i]);
    SlidingWindowAggregation<ll> swag(f);
    ll right = 0;
    ll res = 0;
    for (int left = 0; left < N; left++) {
        if (left != 0) swag.pop();
        while (right != N and (swag.empty() or swag.fold_all() != 1)) {
            swag.push(a[right]);
            right++;
        }
        if (swag.fold_all() == 1) res += N - right + 1;
    }
    cout << res << endl;
	return 0;
}

0