結果

問題 No.1084 積の積
ユーザー kcvlexkcvlex
提出日時 2020-06-19 22:05:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 9,002 bytes
コンパイル時間 1,449 ms
コンパイル使用メモリ 155,696 KB
実行使用メモリ 7,680 KB
最終ジャッジ日時 2024-07-03 14:46:39
合計ジャッジ時間 4,223 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 57 ms
7,552 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 57 ms
7,680 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 8 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 5 ms
6,944 KB
testcase_12 AC 46 ms
6,944 KB
testcase_13 AC 105 ms
7,168 KB
testcase_14 AC 35 ms
6,940 KB
testcase_15 AC 32 ms
6,940 KB
testcase_16 AC 123 ms
7,552 KB
testcase_17 AC 45 ms
6,944 KB
testcase_18 AC 76 ms
6,944 KB
testcase_19 AC 107 ms
7,296 KB
testcase_20 AC 22 ms
6,944 KB
testcase_21 AC 35 ms
6,940 KB
testcase_22 AC 27 ms
6,944 KB
testcase_23 AC 62 ms
6,940 KB
testcase_24 AC 55 ms
6,940 KB
testcase_25 AC 107 ms
7,168 KB
testcase_26 AC 119 ms
7,552 KB
testcase_27 AC 119 ms
7,552 KB
testcase_28 AC 123 ms
7,552 KB
testcase_29 AC 120 ms
7,552 KB
testcase_30 AC 123 ms
7,552 KB
testcase_31 AC 125 ms
7,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define CPP17
#include <limits>
#include <initializer_list>
#include <utility>
#include <bitset>
#include <tuple>
#include <type_traits>
#include <functional>
#include <string>
#include <array>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iterator>
#include <algorithm>
#include <complex>
#include <random>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <regex>
#include <cassert>
#include <cstddef>
#ifdef CPP17
#include <variant>
#endif

// Yay!!
#define endl codeforces

// macros for iterator
#define ALL(v) std::begin(v), std::end(v)
#define ALLR(v) std::rbegin(v), std::rend(v)

// alias
using ll = std::int64_t;
using ull = std::uint64_t;
using pii = std::pair<int, int>;
using tii = std::tuple<int, int, int>;
using pll = std::pair<ll, ll>;
using tll = std::tuple<ll, ll, ll>;
template <typename T> using vec = std::vector<T>;
template <typename T> using vvec = vec<vec<T>>;

// variadic min/max
template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename T, typename... Tail> const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); }
template <typename T, typename... Tail> const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); }

// variadic chmin/chmax
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

// multi demension array
template <typename T, std::size_t Head, std::size_t... Tail> struct multi_dim_array { using type = std::array<typename multi_dim_array<T, Tail...>::type, Head>; };
template <typename T, std::size_t Head> struct multi_dim_array<T, Head> { using type = std::array<T, Head>; };
template <typename T, std::size_t... Args> using mdarray = typename multi_dim_array<T, Args...>::type;

#ifdef CPP17
// fill container
template <typename T, typename F, typename... Args> 
void fill_seq(T &t, F f, Args... args) { if constexpr (std::is_invocable<F, Args...>::value) { t = f(args...); } else { for (ssize_t i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); } }
#endif

// make multi dimension vector
template <typename T> vec<T> make_v(ssize_t sz) { return vec<T>(sz); }
template <typename T, typename... Tail> auto make_v(ssize_t hs, Tail&&... ts) { auto v = std::move(make_v<T>(std::forward<Tail>(ts)...)); return vec<decltype(v)>(hs, v); }

// init
namespace init__ { 
struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; 
}


namespace math {

template <typename T>
constexpr T pow(const T &n, ll k) {
    T ret = n.mul_id_ele();
    T cur = n;
    while (k) {
        if (k & 1) ret *= cur;
        cur *= cur;
        k /= 2;
    }
    return ret;
}

}

namespace math {

template <ll Mod>
struct Modint {

    constexpr Modint(ll x) noexcept : x((Mod + x % Mod) % Mod) { }
    
    constexpr Modint() noexcept : Modint(0) { }
    
    constexpr Modint<Mod> add_id_ele() const noexcept { 
        return Modint<Mod>(0); 
    }
    
    constexpr Modint<Mod> mul_id_ele() const noexcept {
        return Modint<Mod>(1); 
    }
    
    constexpr ll& value() noexcept { 
        return x; 
    }
    
    constexpr ll value() const noexcept {
        return x; 
    }

    constexpr Modint& operator +=(const Modint &oth) noexcept {
        x += oth.value();
        if (Mod <= x) x -= Mod;
        return *this;
    }

    constexpr Modint& operator -=(const Modint &oth) noexcept {
        x += Mod - oth.value();
        if (Mod <= x) x -= Mod;
        return *this;
    }

    constexpr Modint& operator *=(const Modint &oth) noexcept {
        x *= oth.value();
        x %= Mod;
        return *this;
    }

    constexpr Modint& operator /=(const Modint &oth) noexcept {
        x *= oth.inv().value();
        x %= Mod;
        return *this;
    }

    constexpr Modint operator +(const Modint &oth) const noexcept {
        return Modint(x) += oth;
    }

    constexpr Modint operator -(const Modint &oth) const noexcept {
        return Modint(x) -= oth;
    }

    constexpr Modint operator *(const Modint &oth) const noexcept {
        return Modint(x) *= oth;
    }

    constexpr Modint operator /(const Modint &oth) const noexcept {
        return Modint(x) /= oth;
    }

    constexpr Modint operator -() const noexcept {
        return Modint((x != 0) * (Mod - x)); 
    }

    template <typename T>
    constexpr typename std::enable_if<std::is_integral<T>::value, const Modint&>::type
    operator =(T t) noexcept {
        (*this) = Modint(std::forward<T>(t)); 
        return *this;
    }

    constexpr Modint inv() const noexcept {
        return ::math::pow(*this, Mod - 2);
    }

    constexpr ll mod() const noexcept {
        return Mod;
    }

private:
    ll x;
};

}
ssize_t ceil_pow2(ssize_t s) {
    ssize_t ret = 1;
    while (ret < s) ret *= 2;
    return ret;
}

namespace segtree {

template <typename T, typename Op>
struct SegmentTree {
    vec<T> nodes;
    Op op;
    T id_ele;

    auto get_children_idx(ssize_t idx) const -> std::pair<ssize_t, ssize_t> {
        return std::make_pair(2 * idx + 1, 2 * idx + 2);
    }

    T get_merged(ssize_t idx) const {
        ssize_t a, b;
        std::tie(a, b) = get_children_idx(idx);
        return op(nodes[a], nodes[b]);
    }

public:
    SegmentTree(const vec<T> &init_v, Op op, T id_ele)
        : op(op), id_ele(id_ele)
    {
        build(init_v);
    }

    void build(const vec<T> &v) {
        nodes.resize(ceil_pow2(v.size()) * 2 - 1, id_ele);
        ssize_t sz = size();
        std::copy(ALL(v), nodes.begin() + sz - 1);
        build_parents();
    }

    void build_parents() {
        ssize_t sz = size();
        for (ssize_t i = sz - 2; 0 <= i; i--) nodes[i] = get_merged(i);
    }

    ssize_t size() const {
        return (nodes.size() + 1) / 2;
    }

    const T& operator [](ll idx) const {
        return nodes[idx + size() - 1];
    }

    T get_query(ll ql, ll qr) const {
        return get_query(ql, qr, id_ele);
    }

    T get_query(ll ql, ll qr, T init) const {
        T ret = init;
        ssize_t lnode = ql + size(), rnode = qr + size();
        while (lnode < rnode) {
            if (lnode & 1) {
                ret = std::move(op(nodes[lnode - 1], ret));
                lnode++;
            }
            if (rnode & 1) {
                rnode--;
                ret = std::move(op(ret, nodes[rnode - 1]));
            }
            lnode /= 2;
            rnode /= 2;
        }
        return ret;
    }

    void update_query(ll idx, T val) {
        idx += size() - 1;
        nodes[idx] = val;
        while (idx) {
            ll pidx = (idx - 1) / 2;
            nodes[pidx] = get_merged(pidx);
            idx = pidx;
        }
    }

    template <typename F>
    ssize_t lower_bound(F f) const {
        T sum = id_ele;
        ssize_t l = 0, r = size(), idx = 0, bidx = 0;
        if (!f(nodes[0])) return -1;
        while (true) {
            if (r - l == 1) {
                if (!f(sum)) bidx++;
                break;
            }
            auto lidx = 2 * idx + 1;
            auto ridx = lidx + 1;
            T nsum = op(sum, nodes[lidx]);
            ssize_t m = (l + r) / 2;
            if (f(nsum)) {
                r = m;
                idx = lidx;
            } else {
                l = m;
                idx = ridx;
                bidx = m;
                sum = std::move(nsum);
            }
        }
        return bidx;
    }
};

}

const ll mod = 1e9 + 7;
using mint = math::Modint<mod>;

mint solve() {
    ll n;
    std::cin >> n;
    vec<ll> av(n);
    for (ll &e : av) {
        std::cin >> e;
        if (e == 0) return mint(0);
    }
    vec<mint> sum_m(n + 1), sum(n + 1);
    sum_m[0] = 1;
    sum[0] = 1;
    for (ll i = 0; i < n; i++) sum_m[i + 1] = sum_m[i] * av[i];
    for (ll i = 0; i < n; i++) sum[i + 1] = sum[i] * sum_m[i + 1];
    
    const ll maxv = 1e9;
    auto merge = [&](ll a, ll b) { return std::min(maxv, a * b); };
    segtree::SegmentTree<ll, decltype(merge)> seg(av, merge, maxv);
    mint ans = 1;
    for (ll i = 0; i < n; i++) {
        {
            auto tmp = seg.get_query(i, n, 1);
            if (tmp < maxv) {
                ans *= (sum[n] / sum[i]) / pow(sum_m[i], n - i);
                continue;
            }
        }

        ll ok = i + 1, ng = n;
        while (1 < std::abs(ok - ng)) {
            ll mid = (ok + ng) / 2;
            (seg.get_query(i, mid, 1) < maxv ? ok : ng) = mid;
        }
        ans *= (sum[ok] / sum[i]) / pow(sum_m[i], ok - i);
    }

    return ans;
}

int main() {
    std::cout << solve().value() << "\n";
    return 0;
}
0