結果

問題 No.856 増える演算
ユーザー kimiyukikimiyuki
提出日時 2020-08-11 20:42:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 118 ms / 3,153 ms
コード長 7,807 bytes
コンパイル時間 2,276 ms
コンパイル使用メモリ 214,664 KB
実行使用メモリ 23,036 KB
最終ジャッジ日時 2024-10-09 11:42:51
合計ジャッジ時間 7,466 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 2 ms
6,816 KB
testcase_10 AC 2 ms
6,820 KB
testcase_11 AC 2 ms
6,820 KB
testcase_12 AC 2 ms
6,820 KB
testcase_13 AC 2 ms
6,816 KB
testcase_14 AC 2 ms
6,816 KB
testcase_15 AC 2 ms
6,820 KB
testcase_16 AC 2 ms
6,820 KB
testcase_17 AC 2 ms
6,816 KB
testcase_18 AC 2 ms
6,816 KB
testcase_19 AC 3 ms
6,816 KB
testcase_20 AC 2 ms
6,820 KB
testcase_21 AC 2 ms
6,820 KB
testcase_22 AC 2 ms
6,816 KB
testcase_23 AC 3 ms
6,820 KB
testcase_24 AC 5 ms
6,820 KB
testcase_25 AC 2 ms
6,816 KB
testcase_26 AC 2 ms
6,816 KB
testcase_27 AC 2 ms
6,820 KB
testcase_28 AC 4 ms
6,816 KB
testcase_29 AC 3 ms
6,816 KB
testcase_30 AC 3 ms
6,820 KB
testcase_31 AC 2 ms
6,816 KB
testcase_32 AC 4 ms
6,820 KB
testcase_33 AC 7 ms
6,824 KB
testcase_34 AC 12 ms
6,816 KB
testcase_35 AC 8 ms
6,816 KB
testcase_36 AC 11 ms
6,816 KB
testcase_37 AC 10 ms
6,820 KB
testcase_38 AC 3 ms
6,824 KB
testcase_39 AC 4 ms
6,816 KB
testcase_40 AC 6 ms
6,820 KB
testcase_41 AC 6 ms
6,816 KB
testcase_42 AC 13 ms
6,816 KB
testcase_43 AC 6 ms
6,816 KB
testcase_44 AC 3 ms
6,820 KB
testcase_45 AC 3 ms
6,816 KB
testcase_46 AC 6 ms
6,820 KB
testcase_47 AC 5 ms
6,820 KB
testcase_48 AC 9 ms
6,820 KB
testcase_49 AC 7 ms
6,816 KB
testcase_50 AC 9 ms
6,816 KB
testcase_51 AC 11 ms
6,820 KB
testcase_52 AC 13 ms
6,820 KB
testcase_53 AC 92 ms
22,752 KB
testcase_54 AC 78 ms
22,612 KB
testcase_55 AC 91 ms
22,676 KB
testcase_56 AC 71 ms
22,532 KB
testcase_57 AC 89 ms
22,728 KB
testcase_58 AC 85 ms
22,684 KB
testcase_59 AC 106 ms
22,780 KB
testcase_60 AC 81 ms
22,608 KB
testcase_61 AC 108 ms
22,808 KB
testcase_62 AC 105 ms
22,780 KB
testcase_63 AC 61 ms
22,460 KB
testcase_64 AC 100 ms
22,708 KB
testcase_65 AC 74 ms
22,612 KB
testcase_66 AC 81 ms
22,668 KB
testcase_67 AC 86 ms
22,648 KB
testcase_68 AC 100 ms
22,800 KB
testcase_69 AC 99 ms
22,804 KB
testcase_70 AC 112 ms
22,824 KB
testcase_71 AC 103 ms
22,952 KB
testcase_72 AC 96 ms
22,784 KB
testcase_73 AC 116 ms
22,868 KB
testcase_74 AC 116 ms
22,884 KB
testcase_75 AC 116 ms
22,864 KB
testcase_76 AC 118 ms
22,896 KB
testcase_77 AC 116 ms
22,896 KB
testcase_78 AC 114 ms
23,036 KB
testcase_79 AC 117 ms
23,020 KB
testcase_80 AC 115 ms
22,864 KB
testcase_81 AC 115 ms
22,932 KB
testcase_82 AC 104 ms
22,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"
#include <bits/stdc++.h>
#line 2 "/home/user/Library/utils/macros.hpp"
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#line 4 "/home/user/Library/modulus/modpow.hpp"

inline int32_t modpow(uint_fast64_t x, uint64_t k, int32_t MOD) {
    assert (/* 0 <= x and */ x < (uint_fast64_t)MOD);
    uint_fast64_t y = 1;
    for (; k; k >>= 1) {
        if (k & 1) (y *= x) %= MOD;
        (x *= x) %= MOD;
    }
    assert (/* 0 <= y and */ y < (uint_fast64_t)MOD);
    return y;
}
#line 5 "/home/user/Library/modulus/modinv.hpp"

inline int32_t modinv_nocheck(int32_t value, int32_t MOD) {
    assert (0 <= value and value < MOD);
    if (value == 0) return -1;
    int64_t a = value, b = MOD;
    int64_t x = 0, y = 1;
    for (int64_t u = 1, v = 0; a; ) {
        int64_t q = b / a;
        x -= q * u; std::swap(x, u);
        y -= q * v; std::swap(y, v);
        b -= q * a; std::swap(b, a);
    }
    if (not (value * x + MOD * y == b and b == 1)) return -1;
    if (x < 0) x += MOD;
    assert (0 <= x and x < MOD);
    return x;
}

inline int32_t modinv(int32_t x, int32_t MOD) {
    int32_t y = modinv_nocheck(x, MOD);
    assert (y != -1);
    return y;
}
#line 6 "/home/user/Library/modulus/mint.hpp"

/**
 * @brief quotient ring / 剰余環 $\mathbb{Z}/n\mathbb{Z}$
 */
template <int32_t MOD>
struct mint {
    int32_t value;
    mint() : value() {}
    mint(int64_t value_) : value(value_ < 0 ? value_ % MOD + MOD : value_ >= MOD ? value_ % MOD : value_) {}
    mint(int32_t value_, std::nullptr_t) : value(value_) {}
    explicit operator bool() const { return value; }
    inline mint<MOD> operator + (mint<MOD> other) const { return mint<MOD>(*this) += other; }
    inline mint<MOD> operator - (mint<MOD> other) const { return mint<MOD>(*this) -= other; }
    inline mint<MOD> operator * (mint<MOD> other) const { return mint<MOD>(*this) *= other; }
    inline mint<MOD> & operator += (mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
    inline mint<MOD> & operator -= (mint<MOD> other) { this->value -= other.value; if (this->value <    0) this->value += MOD; return *this; }
    inline mint<MOD> & operator *= (mint<MOD> other) { this->value = (uint_fast64_t)this->value * other.value % MOD; return *this; }
    inline mint<MOD> operator - () const { return mint<MOD>(this->value ? MOD - this->value : 0, nullptr); }
    inline bool operator == (mint<MOD> other) const { return value == other.value; }
    inline bool operator != (mint<MOD> other) const { return value != other.value; }
    inline mint<MOD> pow(uint64_t k) const { return mint<MOD>(modpow(value, k, MOD), nullptr); }
    inline mint<MOD> inv() const { return mint<MOD>(modinv(value, MOD), nullptr); }
    inline mint<MOD> operator / (mint<MOD> other) const { return *this * other.inv(); }
    inline mint<MOD> & operator /= (mint<MOD> other) { return *this *= other.inv(); }
};
template <int32_t MOD> mint<MOD> operator + (int64_t value, mint<MOD> n) { return mint<MOD>(value) + n; }
template <int32_t MOD> mint<MOD> operator - (int64_t value, mint<MOD> n) { return mint<MOD>(value) - n; }
template <int32_t MOD> mint<MOD> operator * (int64_t value, mint<MOD> n) { return mint<MOD>(value) * n; }
template <int32_t MOD> mint<MOD> operator / (int64_t value, mint<MOD> n) { return mint<MOD>(value) / n; }
template <int32_t MOD> std::istream & operator >> (std::istream & in, mint<MOD> & n) { int64_t value; in >> value; n = value; return in; }
template <int32_t MOD> std::ostream & operator << (std::ostream & out, mint<MOD> n) { return out << n.value; }
#line 8 "/home/user/Library/number/fast_fourier_transformation.hpp"

/**
 * @note O(N log N)
 * @note radix-2, decimation-in-frequency, Cooley-Tukey
 * @note cache std::polar (~ 2x faster)
 */
template <typename R>
void fft_inplace(std::vector<std::complex<R> > & f, bool inverse) {
    const int n = f.size();
    assert (n == pow(2, log2(n)));

    // cache
    static std::vector<std::complex<R> > cache;
    if ((int)cache.size() != n / 2) {
        const R theta = 2 * M_PI / n;
        cache.resize(n / 2);
        REP (irev, n / 2) {
            cache[irev] = std::polar<R>(1, irev * theta);
        }
    }

    // bit reverse
    int i = 0;
    REP3 (j, 1, n - 1) {
        for (int k = n >> 1; (i ^= k) < k; k >>= 1);
        if (j < i) swap(f[i], f[j]);
    }

    // divide and conquer
    for (int mh = 1; (mh << 1) <= n; mh <<= 1) {
        int irev = 0;
        for (int i = 0; i < n; i += (mh << 1)) {
            auto w = (inverse ? conj(cache[irev]) : cache[irev]);
            for (int k = n >> 2; (irev ^= k) < k; k >>= 1);
            REP3 (j, i, mh + i) {
                int k = j + mh;
                std::complex<R> x = f[j] - f[k];
                f[j] += f[k];
                f[k] = w * x;
            }
        }
    }
}

template <typename R>
std::vector<std::complex<R> > fft(std::vector<std::complex<R> > f) {
    f.resize(pow(2, ceil(log2(f.size()))));
    fft_inplace(f, false);
    return f;
}
template <typename R>
std::vector<std::complex<R> > ifft(std::vector<std::complex<R> > f) {
    f.resize(pow(2, ceil(log2(f.size()))));
    fft_inplace(f, true);
    return f;
}

/**
 * @brief FFT convolution
 * @note O(N log N)
 * @note (f \ast g)(i) = \sum_{0 \le j \lt i + 1} f(j) g(i - j)
 */
template <typename T, typename R = double>
std::vector<T> fft_convolution(std::vector<T> const & a, std::vector<T> const & b) {
    assert (not a.empty() and not b.empty());
    int m = a.size() + b.size() - 1;
    int n = pow(2, ceil(log2(m)));
    std::vector<std::complex<R> > x(n), y(n);
    copy(ALL(a), x.begin());
    copy(ALL(b), y.begin());
    fft_inplace(x, false);
    fft_inplace(y, false);
    std::vector<std::complex<R> > z(n);
    REP (i, n) {
        z[i] = x[i] * y[i];
    }
    fft_inplace(z, true);
    std::vector<T> c(m);
    REP (i, m) {
        c[i] = std::is_integral<T>::value ? round(z[i].real() / n) : z[i].real() / n;
    }
    return c;
}
#line 5 "main.cpp"
using namespace std;

constexpr int64_t MOD = 1000000007;

mint<MOD> solve(int n, const vector<int> &a) {
    // \prod_i \prod_{j > i} (A_i + A_j)
    mint<MOD> x = 1;
    {
        int max_a = *max_element(ALL(a));
        vector<long double> cnt(max_a + 1);
        for (int a_i : a) {
            ++ cnt[a_i];
        }
        cnt = fft_convolution(cnt, cnt);
        for (int a_i : a) {
            -- cnt[2 * a_i];
        }
        REP (i, cnt.size()) {
            cnt[i] /= 2;
        }
        REP (i, cnt.size()) {
            int64_t k = round(cnt[i]);
            assert (abs(cnt[i] - k) <= 1e-6);
            x *= mint<MOD>(i).pow(k);
        }
    }

    // \prod_i \prod_{j > i} A_i^{A_j}
    mint<MOD> y = 1;
    {
        int64_t sum_a_j = 0;
        REP_R (i, n) {
            y *= mint<MOD>(a[i]).pow(sum_a_j);
            sum_a_j += a[i];
        }
    }

    // \min_i \mimn_{j > i} (A_i + A_j)A_i^{A_j}
    mint<MOD> z = 0;
    {
        double log_z = INFINITY;
        int64_t a_j = a[n - 1];
        REP_R (i, n - 1) {
            double log_a_i_a_j = log(a[i] + a_j) + a_j * log(a[i]);
            if (log_a_i_a_j < log_z) {
                log_z = log_a_i_a_j;
                z = mint<MOD>(a[i] + a_j) * mint<MOD>(a[i]).pow(a_j);
            }
            a_j = min<int64_t>(a[i], a_j);
        }
    }
    return x * y / z;
}

int main() {
    int n; cin >> n;
    vector<int> a(n);
    REP (i, n) {
        cin >> a[i];
    }
    auto ans = solve(n, a);
    cout << ans << endl;
    return 0;
}
0