結果

問題 No.2688 Cell Proliferation (Hard)
ユーザー ゆにぽけゆにぽけ
提出日時 2024-03-21 02:28:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,456 ms / 4,000 ms
コード長 7,091 bytes
コンパイル時間 3,265 ms
コンパイル使用メモリ 139,592 KB
実行使用メモリ 44,436 KB
最終ジャッジ日時 2024-03-22 18:03:30
合計ジャッジ時間 45,629 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 3 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 213 ms
8,104 KB
testcase_04 AC 2,300 ms
42,896 KB
testcase_05 AC 1,126 ms
23,808 KB
testcase_06 AC 515 ms
13,476 KB
testcase_07 AC 513 ms
13,404 KB
testcase_08 AC 2,304 ms
42,236 KB
testcase_09 AC 2,279 ms
41,540 KB
testcase_10 AC 2,431 ms
43,472 KB
testcase_11 AC 2,300 ms
42,536 KB
testcase_12 AC 607 ms
13,968 KB
testcase_13 AC 2,456 ms
44,424 KB
testcase_14 AC 2,270 ms
40,460 KB
testcase_15 AC 2,455 ms
44,436 KB
testcase_16 AC 2,272 ms
40,244 KB
testcase_17 AC 1,253 ms
24,392 KB
testcase_18 AC 2,421 ms
43,616 KB
testcase_19 AC 1,250 ms
24,452 KB
testcase_20 AC 1,028 ms
21,992 KB
testcase_21 AC 1,027 ms
21,980 KB
testcase_22 AC 1,182 ms
24,188 KB
testcase_23 AC 1,048 ms
22,460 KB
testcase_24 AC 2,443 ms
43,328 KB
testcase_25 AC 745 ms
14,040 KB
testcase_26 AC 2,421 ms
43,784 KB
testcase_27 AC 1,315 ms
24,536 KB
testcase_28 AC 2,267 ms
40,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;
template<int m> struct modint
{
	private:
	unsigned int value;
	static constexpr int mod() {return m;}

	public:
	constexpr modint(const long long x = 0) noexcept
	{
		long long y = x;
		if(y < 0 || y >= mod())
		{
			y %= mod();
			if(y < 0) y += mod();
		}
		value = (unsigned int)y;
	}
	static constexpr int get_mod() noexcept {return m;}
	static constexpr int primitive_root() noexcept
	{
		assert(m == 998244353);
		return 3;
	}
	constexpr unsigned int val() noexcept {return value;}
	constexpr modint &operator+=(const modint &other) noexcept
	{
		value += other.value;
		if(value >= mod()) value -= mod();
		return *this;
	}
	constexpr modint &operator-=(const modint &other) noexcept
	{
		unsigned int x = value;
		if(x < other.value) x += mod();
		x -= other.value;
		value = x;
		return *this;
	}
	constexpr modint &operator*=(const modint &other) noexcept
	{
		unsigned long long x = value;
		x *= other.value;
		value = (unsigned int) (x % mod());
		return *this;
	}
	constexpr modint &operator/=(const modint &other) noexcept
	{
		return *this *= other.inverse();
	}
	constexpr modint inverse() const noexcept
	{
		assert(value);
		long long a = value,b = mod(),x = 1,y = 0;
		while(b)
		{
			long long q = a/b;
			a -= q*b; swap(a,b);
			x -= q*y; swap(x,y);
		}
		return modint(x);
	}
	constexpr modint power(long long N) const noexcept
	{
		assert(N >= 0);
		modint p = *this,ret = 1;
		while(N)
		{
			if(N & 1) ret *= p;
			p *= p;
			N >>= 1;
		}
		return ret;
	}
	constexpr modint operator+() {return *this;}
	constexpr modint operator-() {return modint() - *this;}
	constexpr modint &operator++(int) noexcept {return *this += 1;}
	constexpr modint &operator--(int) noexcept {return *this -= 1;}
	friend modint operator+(const modint& lhs, const modint& rhs) {return modint(lhs) += rhs;}
	friend modint operator-(const modint& lhs, const modint& rhs) {return modint(lhs) -= rhs;}
	friend modint operator*(const modint& lhs, const modint& rhs) {return modint(lhs) *= rhs;}
	friend modint operator/(const modint& lhs, const modint& rhs) {return modint(lhs) /= rhs;}
	friend ostream &operator<<(ostream &os,const modint &x) {return os << x.value;}
};
using mint = modint<998244353>;
/* using mint = modint<1000000007>; */

int ceil_log2(int n)
{
	int res = 0;
	while((1U << res) < (unsigned int)n)
	{
		res++;
	}
	return res;
}

template<class mint> void Butterfly(vector<mint> &a,bool inverse = false)
{
	int N = (int)a.size();
	int H = __builtin_ctz(N);
	assert(N == (1 << H));

	static constexpr int pr = mint::primitive_root();
	static bool first_call = true;
	static vector<mint> w(30),iw(30);
	if(first_call)
	{
		first_call = false;
		int cnt = __builtin_ctz(mint::get_mod() - 1);
		mint e = mint(pr).power((mint::get_mod() - 1) >> cnt);
		mint ie = e.inverse();
		for(int i = cnt;i >= 1;i--)
		{
			w[i] = e;
			iw[i] = ie;
			e *= e;
			ie *= ie;
		}
	}

	if(!inverse)
	{
		int width = N;
		int log = H;
		const mint im = w[2];
		while(width > 1)
		{
			mint cur = w[log];

			if(width == 2)
			{
				int offset = width >> 1;
				for(int i = 0;i < N;i += width)
				{
					mint root = 1;
					for(int j = i;j < i + offset;j++)
					{
						mint s = a[j],t = a[j + offset];
						a[j] = s + t;
						a[j + offset] = (s - t) * root;
						root *= cur;
					}
				}
				width >>= 1;
				log--;
			}
			else
			{
				int offset = width >> 2;
				for(int i = 0;i < N;i += width)
				{
					mint root = 1;
					for(int j = i;j < i + offset;j++)
					{
						mint root2 = root * root;
						mint root3 = root2 * root;
						mint s = a[j],t = a[j + offset],u = a[j + offset * 2],v = a[j + offset * 3];
						mint spu = s + u;
						mint smu = s - u;
						mint tpv = t + v;
						mint tmvim = (t - v) * im;
						a[j] = spu + tpv;
						a[j + offset] = (spu - tpv) * root2;
						a[j + offset * 2] = (smu + tmvim) * root;
						a[j + offset * 3] = (smu - tmvim) * root3;
						root *= cur;
					}
				}
				width >>= 2;
				log -= 2;
			}

		}
	}
	else
	{
		int width = H & 1 ? 2 : 4;
		int log = H & 1 ? 1 : 2;
		const mint im = iw[2];
		while(width <= N)
		{
			mint cur = iw[log];

			if(width == 2)
			{
				int offset = width >> 1;
				for(int i = 0;i < N;i += width)
				{
					mint root = 1;
					for(int j = i;j < i + offset;j++)
					{
						mint s = a[j],t = a[j + offset] * root;
						a[j] = s + t;
						a[j + offset] = s - t;
						root *= cur;
					}
				}
			}
			else
			{
				int offset = width >> 2;
				for(int i = 0;i < N;i += width)
				{
					mint root = 1;
					for(int j = i;j < i + offset;j++)
					{
						mint root2 = root * root;
						mint root3 = root2 * root;
						mint s = a[j],t = a[j + offset] * root2,u = a[j + offset * 2] * root,v = a[j + offset * 3] * root3;
						mint spt = s + t;
						mint smt = s - t;
						mint upv = u + v;
						mint umvim = (u - v) * im;
						a[j] = spt + upv;
						a[j + offset] = smt + umvim;
						a[j + offset * 2] = spt - upv;
						a[j + offset * 3] = smt - umvim;
						root *= cur;
					}
				}
			}

			width <<= 2;
			log += 2;
		}
	}
}

template<class mint> vector<mint> Convolution(vector<mint> a,vector<mint> b)
{
	int N = (int)a.size(),M = (int)b.size();

	if(min(N,M) <= 60)
	{
		vector<mint> res(N + M - 1);
		if(N < M)
		{
			swap(N,M);
			swap(a,b);
		}
		for(int i = 0;i < N;i++)
		{
			for(int j = 0;j < M;j++)
			{
				res[i + j] += a[i] * b[j];
			}
		}
		return res;
	}

	int L = 1 << ceil_log2(N + M - 1);

	a.resize(L);
	b.resize(L);

	Butterfly(a);
	Butterfly(b);

	for(int i = 0;i < L;i++)
	{
		a[i] *= b[i];
	}
	Butterfly(a,true);
	a.resize(N + M - 1);
	const mint invL = mint(L).inverse();
	for(int i = 0;i < N + M - 1;i++)
	{
		a[i] *= invL;
	}
	return a;
}
void Main()
{
	mint P,Q;
	{
		int p1,p2;
		cin >> p1 >> p2;
		P = mint(p1) / p2;
	}
	{
		int q1,q2;
		cin >> q1 >> q2;
		Q = mint(q1) / q2;
	}
	int N;
	cin >> N;
	vector<mint> q(N + 1);
	q[0] = 1;
	for(int i = 1;i <= N;i++)
	{
		q[i] = q[i - 1] * Q;
	}
	for(int i = 1;i <= N;i++)
	{
		q[i] *= q[i - 1];
	}
	vector<mint> g(N + 1);
	g[0] = 1;
	for(int i = 1;i <= N;i++)
	{
		g[i] = -P * q[i - 1];
	}
	vector<mint> h(1);
	h[0] = 1;
	int k = 1;
	while(k - 1 < N)
	{
		vector<mint> nh = h;
		for(int i = 0;i < (int)h.size();i++)
		{
			nh[i] *= 2;
		}
		h = Convolution(h,h);
		h = Convolution(h,g);
		h.resize(k << 1);
		nh.resize(h.size());
		for(int i = 0;i < (int)h.size();i++)
		{
			nh[i] -= h[i];
		}
		swap(h,nh);
		k <<= 1;
	}
	mint ans = 0;
	for(int i = 0;i <= N;i++)
	{
		ans += q[N - i] * h[i];
	}
	cout << ans << "\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}
0