結果

問題 No.856 増える演算
ユーザー square1001square1001
提出日時 2019-07-26 21:42:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 3,153 ms
コード長 6,299 bytes
コンパイル時間 1,163 ms
コンパイル使用メモリ 88,140 KB
実行使用メモリ 24,952 KB
最終ジャッジ日時 2023-09-15 00:56:04
合計ジャッジ時間 10,581 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 6 ms
4,380 KB
testcase_34 AC 10 ms
4,380 KB
testcase_35 AC 7 ms
4,376 KB
testcase_36 AC 10 ms
4,380 KB
testcase_37 AC 8 ms
4,380 KB
testcase_38 AC 3 ms
4,376 KB
testcase_39 AC 4 ms
4,376 KB
testcase_40 AC 5 ms
4,376 KB
testcase_41 AC 6 ms
4,380 KB
testcase_42 AC 11 ms
4,376 KB
testcase_43 AC 5 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 4 ms
4,380 KB
testcase_46 AC 6 ms
4,376 KB
testcase_47 AC 5 ms
4,376 KB
testcase_48 AC 8 ms
4,376 KB
testcase_49 AC 6 ms
4,380 KB
testcase_50 AC 8 ms
4,376 KB
testcase_51 AC 10 ms
4,380 KB
testcase_52 AC 11 ms
4,376 KB
testcase_53 AC 188 ms
24,656 KB
testcase_54 AC 175 ms
24,640 KB
testcase_55 AC 185 ms
24,772 KB
testcase_56 AC 172 ms
24,732 KB
testcase_57 AC 188 ms
24,808 KB
testcase_58 AC 182 ms
24,720 KB
testcase_59 AC 195 ms
24,888 KB
testcase_60 AC 177 ms
24,596 KB
testcase_61 AC 198 ms
24,920 KB
testcase_62 AC 193 ms
24,844 KB
testcase_63 AC 161 ms
24,836 KB
testcase_64 AC 190 ms
24,936 KB
testcase_65 AC 170 ms
24,552 KB
testcase_66 AC 176 ms
24,580 KB
testcase_67 AC 183 ms
24,640 KB
testcase_68 AC 202 ms
24,880 KB
testcase_69 AC 193 ms
24,912 KB
testcase_70 AC 201 ms
24,812 KB
testcase_71 AC 194 ms
24,884 KB
testcase_72 AC 189 ms
24,688 KB
testcase_73 AC 209 ms
24,920 KB
testcase_74 AC 208 ms
24,920 KB
testcase_75 AC 205 ms
24,944 KB
testcase_76 AC 205 ms
24,948 KB
testcase_77 AC 206 ms
24,848 KB
testcase_78 AC 203 ms
24,952 KB
testcase_79 AC 204 ms
24,852 KB
testcase_80 AC 204 ms
24,908 KB
testcase_81 AC 203 ms
24,836 KB
testcase_82 AC 186 ms
24,844 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef CLASS_POLYNOMIAL
#define CLASS_POLYNOMIAL

#include <vector>
#include <complex>
#include <cstdint>
#include <algorithm>
class polynomial {
private:
	using type = long double;
	const type epsilon = 1.0e-9;
	std::size_t sz;
	std::vector<type> a;
	inline bool equivalent(type ra, type rb) const {
		return (epsilon <= ra - rb && ra - rb <= epsilon);
	}
	void discrete_fourier_transform(std::vector<std::complex<type> >& v, bool rev) {
		std::size_t n = v.size();
		const type pi = acos(type(-1));
		for (std::size_t i = 0, j = 1; j < n - 1; ++j) {
			for (std::size_t k = n >> 1; k > (i ^= k); k >>= 1);
			if (i > j) std::swap(v[i], v[j]);
		}
		for (std::size_t b = 1; b < n; b <<= 1) {
			std::complex<type> wr = std::polar(type(1), (rev ? type(-1) : type(1)) * pi / b);
			for (std::size_t i = 0; i < n; i += 2 * b) {
				std::complex<type> w = type(1);
				for (std::size_t j = 0; j < b; ++j) {
					std::complex<type> v0 = v[i + j];
					std::complex<type> v1 = w * v[i + j + b];
					v[i + j] = v0 + v1;
					v[i + j + b] = v0 - v1;
					w *= wr;
				}
			}
		}
		if (!rev) return;
		for (std::size_t i = 0; i < n; i++) v[i] /= n;
	}
public:
	explicit polynomial() : sz(1), a(std::vector<type>({ type() })) {};
	explicit polynomial(std::size_t sz_) : sz(sz_), a(std::vector<type>(sz_, type())) {};
	explicit polynomial(std::vector<type> a_) : sz(a_.size()), a(a_) {};
	polynomial& operator=(const polynomial& p) {
		sz = p.sz;
		a = p.a;
		return (*this);
	}
	std::size_t size() const { return sz; }
	std::size_t degree() const { return sz - 1; }
	type operator[](std::size_t idx) const {
		return a[idx];
	}
	type& operator[](std::size_t idx) {
		return a[idx];
	}
	bool operator==(const polynomial& p) const {
		for (std::size_t i = 0; i < sz || i < p.sz; ++i) {
			if (!equivalent(i < sz ? a[i] : type(0), i < p.sz ? p.a[i] : type(0))) {
				return false;
			}
		}
		return true;
	}
	bool operator!=(const polynomial& p) const {
		return !(operator==(p));
	}
	polynomial resize_transform(std::size_t d) const {
		// Resize polynomial to d: in other words, f(x) := f(x) mod x^d
		polynomial ans(*this);
		ans.sz = d;
		ans.a.resize(d, type(0));
		return ans;
	}
	polynomial star_transform() const {
		// f*(x) = x^degree * f(1/x)
		polynomial ans(*this);
		reverse(ans.a.begin(), ans.a.end());
		return ans;
	}
	polynomial inverse(std::size_t d) const {
		// Find g(x) where g(x) * f(x) = 1 (mod x^d)
		polynomial ans(std::vector<type>({ type(1) / a[0] }));
		while (ans.size() < d) {
			polynomial nxt;
			nxt = -ans * resize_transform(ans.size() * 2);
			nxt.a[0] += type(2);
			nxt *= ans;
			ans = nxt.resize_transform(ans.size() * 2);
		}
		ans = ans.resize_transform(d);
		return ans;
	}
	polynomial& operator+=(const polynomial& p) {
		sz = std::max(sz, p.sz);
		a.resize(sz);
		for (std::size_t i = 0; i < sz; ++i) a[i] += p.a[i];
		return (*this);
	}
	polynomial& operator-=(const polynomial& p) {
		sz = std::max(sz, p.sz);
		a.resize(sz);
		for (std::size_t i = 0; i < sz; ++i) a[i] -= p.a[i];
		return (*this);
	}
	polynomial& operator*=(const polynomial& p) {
		std::size_t n = 2;
		while (n < sz * 2 || n < p.sz * 2) n <<= 1;
		std::vector<std::complex<type> > v(n), pv(n);
		for (std::size_t i = 0; i < sz; ++i) v[i] = a[i];
		for (std::size_t i = 0; i < p.sz; ++i) pv[i] = p.a[i];
		discrete_fourier_transform(v, false);
		discrete_fourier_transform(pv, false);
		for (std::size_t i = 0; i < n; ++i) v[i] *= pv[i];
		discrete_fourier_transform(v, true);
		sz += p.sz - 1;
		a.resize(sz, type(0));
		for (std::size_t i = 0; i < sz; ++i) a[i] = v[i].real();
		return (*this);
	}
	polynomial operator+() const {
		return polynomial(*this);
	}
	polynomial operator-() const {
		return polynomial() - polynomial(*this);
	}
	polynomial operator+(const polynomial& p) const {
		return polynomial(*this) += p;
	}
	polynomial operator-(const polynomial& p) const {
		return polynomial(*this) -= p;
	}
	polynomial operator*(const polynomial& p) const {
		return polynomial(*this) *= p;
	}
};

#endif

#ifndef CLASS_MODINT
#define CLASS_MODINT

#include <cstdint>

template <std::uint32_t mod>
class modint {
private:
	std::uint32_t n;
public:
	modint() : n(0) {};
	modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {};
	static constexpr std::uint32_t get_mod() { return mod; }
	std::uint32_t get() const { return n; }
	bool operator==(const modint& m) const { return n == m.n; }
	bool operator!=(const modint& m) const { return n != m.n; }
	modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; }
	modint operator+(const modint& m) const { return modint(*this) += m; }
	modint operator-(const modint& m) const { return modint(*this) -= m; }
	modint operator*(const modint& m) const { return modint(*this) *= m; }
	modint inv() const { return (*this).pow(mod - 2); }
	modint pow(std::uint64_t b) const {
		modint ans = 1, m = modint(*this);
		while (b) {
			if (b & 1) ans *= m;
			m *= m;
			b >>= 1;
		}
		return ans;
	}
};

#endif // CLASS_MODINT

#include <vector>
#include <iostream>
using namespace std;
using modulo = modint<1000000007>;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; ++i) cin >> A[i];
	int mx = *max_element(A.begin(), A.end());
	polynomial hist(mx + 1);
	for (int i = 0; i < N; ++i) hist[A[i]] += 1.0;
	hist *= hist;
	for (int i = 0; i < N; ++i) hist[A[i] * 2] -= 1.0;
	for (int i = 0; i <= 2 * mx; ++i) hist[i] *= 0.5;
	modulo ans = 1;
	for (int i = 0; i <= 2 * mx; ++i) {
		if (hist[i] > 0) {
			ans *= modulo(i).pow((long long)(hist[i] + 0.5));
		}
	}
	long long sum = 0;
	for (int i = N - 1; i >= 0; --i) {
		ans *= modulo(A[i]).pow(sum);
		sum += A[i];
	}
	int curmin = 1 << 30, p1 = -1, p2 = -1; double curlog = 1.0e+99;
	for (int i = N - 1; i >= 0; --i) {
		double newlog = log(A[i]) * curmin + log(A[i] + curmin);
		if (curlog > newlog) {
			curlog = newlog;
			p1 = A[i];
			p2 = curmin;
		}
		curmin = min(curmin, A[i]);
	}
	ans *= modulo(p1 + p2).inv() * modulo(p1).pow(p2).inv();
	cout << ans.get() << endl;
	return 0;
}
0