結果

問題 No.1036 Make One With GCD 2
ユーザー 👑 jupirojupiro
提出日時 2020-04-25 19:43:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 282 ms / 2,000 ms
コード長 2,818 bytes
コンパイル時間 1,337 ms
コンパイル使用メモリ 138,216 KB
実行使用メモリ 15,656 KB
最終ジャッジ日時 2023-10-14 20:01:33
合計ジャッジ時間 9,521 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
7,476 KB
testcase_01 AC 151 ms
6,880 KB
testcase_02 AC 112 ms
15,656 KB
testcase_03 AC 17 ms
4,352 KB
testcase_04 AC 29 ms
5,704 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 49 ms
4,696 KB
testcase_08 AC 40 ms
4,384 KB
testcase_09 AC 172 ms
6,800 KB
testcase_10 AC 162 ms
6,480 KB
testcase_11 AC 173 ms
6,976 KB
testcase_12 AC 155 ms
6,620 KB
testcase_13 AC 266 ms
6,688 KB
testcase_14 AC 282 ms
6,612 KB
testcase_15 AC 261 ms
6,496 KB
testcase_16 AC 257 ms
6,504 KB
testcase_17 AC 272 ms
6,848 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 265 ms
6,408 KB
testcase_23 AC 186 ms
5,748 KB
testcase_24 AC 264 ms
6,944 KB
testcase_25 AC 252 ms
6,548 KB
testcase_26 AC 249 ms
6,540 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 1 ms
4,348 KB
testcase_33 AC 1 ms
4,352 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 1 ms
4,352 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 115 ms
15,540 KB
testcase_39 AC 131 ms
7,048 KB
testcase_40 AC 188 ms
5,644 KB
testcase_41 AC 157 ms
7,316 KB
testcase_42 AC 159 ms
6,960 KB
testcase_43 AC 161 ms
9,208 KB
testcase_44 AC 165 ms
8,036 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