結果
| 問題 |
No.2688 Cell Proliferation (Hard)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-03-20 23:29:06 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 923 ms / 4,000 ms |
| コード長 | 33,909 bytes |
| コンパイル時間 | 3,762 ms |
| コンパイル使用メモリ | 267,992 KB |
| 実行使用メモリ | 59,164 KB |
| 最終ジャッジ日時 | 2024-09-30 09:31:36 |
| 合計ジャッジ時間 | 19,979 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
// #pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
// #include <x86intrin.h>
using namespace std;
#if __cplusplus >= 202002L
using namespace numbers;
#endif
template<class data_t, data_t _mod>
struct modular_fixed_base{
#define IS_INTEGRAL(T) (is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>)
#define IS_UNSIGNED(T) (is_unsigned_v<T> || is_same_v<T, __uint128_t>)
static_assert(IS_UNSIGNED(data_t));
static_assert(_mod >= 1);
static constexpr bool VARIATE_MOD_FLAG = false;
static constexpr data_t mod(){
return _mod;
}
template<class T>
static vector<modular_fixed_base> precalc_power(T base, int SZ){
vector<modular_fixed_base> res(SZ + 1, 1);
for(auto i = 1; i <= SZ; ++ i) res[i] = res[i - 1] * base;
return res;
}
static vector<modular_fixed_base> _INV;
static void precalc_inverse(int SZ){
if(_INV.empty()) _INV.assign(2, 1);
for(auto x = _INV.size(); x <= SZ; ++ x) _INV.push_back(_mod / x * -_INV[_mod % x]);
}
// _mod must be a prime
static modular_fixed_base _primitive_root;
static modular_fixed_base primitive_root(){
if(_primitive_root) return _primitive_root;
if(_mod == 2) return _primitive_root = 1;
if(_mod == 998244353) return _primitive_root = 3;
data_t divs[20] = {};
divs[0] = 2;
int cnt = 1;
data_t x = (_mod - 1) / 2;
while(x % 2 == 0) x /= 2;
for(auto i = 3; 1LL * i * i <= x; i += 2){
if(x % i == 0){
divs[cnt ++] = i;
while(x % i == 0) x /= i;
}
}
if(x > 1) divs[cnt ++] = x;
for(auto g = 2; ; ++ g){
bool ok = true;
for(auto i = 0; i < cnt; ++ i){
if(modular_fixed_base(g).power((_mod - 1) / divs[i]) == 1){
ok = false;
break;
}
}
if(ok) return _primitive_root = g;
}
}
constexpr modular_fixed_base(){ }
modular_fixed_base(const double &x){ data = _normalize(llround(x)); }
modular_fixed_base(const long double &x){ data = _normalize(llround(x)); }
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base(const T &x){ data = _normalize(x); }
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> static data_t _normalize(const T &x){
int sign = x >= 0 ? 1 : -1;
data_t v = _mod <= sign * x ? sign * x % _mod : sign * x;
if(sign == -1 && v) v = _mod - v;
return v;
}
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> operator T() const{ return data; }
modular_fixed_base &operator+=(const modular_fixed_base &otr){ if((data += otr.data) >= _mod) data -= _mod; return *this; }
modular_fixed_base &operator-=(const modular_fixed_base &otr){ if((data += _mod - otr.data) >= _mod) data -= _mod; return *this; }
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator+=(const T &otr){ return *this += modular_fixed_base(otr); }
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr> modular_fixed_base &operator-=(const T &otr){ return *this -= modular_fixed_base(otr); }
modular_fixed_base &operator++(){ return *this += 1; }
modular_fixed_base &operator--(){ return *this += _mod - 1; }
modular_fixed_base operator++(int){ modular_fixed_base result(*this); *this += 1; return result; }
modular_fixed_base operator--(int){ modular_fixed_base result(*this); *this += _mod - 1; return result; }
modular_fixed_base operator-() const{ return modular_fixed_base(_mod - data); }
modular_fixed_base &operator*=(const modular_fixed_base &rhs){
if constexpr(is_same_v<data_t, unsigned int>) data = (unsigned long long)data * rhs.data % _mod;
else if constexpr(is_same_v<data_t, unsigned long long>){
long long res = data * rhs.data - _mod * (unsigned long long)(1.L / _mod * data * rhs.data);
data = res + _mod * (res < 0) - _mod * (res >= (long long)_mod);
}
else data = _normalize(data * rhs.data);
return *this;
}
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>
modular_fixed_base &inplace_power(T e){
if(e == 0) return *this = 1;
if(data == 0) return *this = {};
if(data == 1 || e == 1) return *this;
if(data == mod() - 1) return e % 2 ? *this : *this = -*this;
if(e < 0) *this = 1 / *this, e = -e;
if(e == 1) return *this;
modular_fixed_base res = 1;
for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this;
return *this = res;
}
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>
modular_fixed_base power(T e) const{
return modular_fixed_base(*this).inplace_power(e);
}
modular_fixed_base &operator/=(const modular_fixed_base &otr){
make_signed_t<data_t> a = otr.data, m = _mod, u = 0, v = 1;
if(a < _INV.size()) return *this *= _INV[a];
while(a){
make_signed_t<data_t> t = m / a;
m -= t * a; swap(a, m);
u -= t * v; swap(u, v);
}
assert(m == 1);
return *this *= u;
}
#define ARITHMETIC_OP(op, apply_op)\
modular_fixed_base operator op(const modular_fixed_base &x) const{ return modular_fixed_base(*this) apply_op x; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
modular_fixed_base operator op(const T &x) const{ return modular_fixed_base(*this) apply_op modular_fixed_base(x); }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
friend modular_fixed_base operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x) apply_op y; }
ARITHMETIC_OP(+, +=) ARITHMETIC_OP(-, -=) ARITHMETIC_OP(*, *=) ARITHMETIC_OP(/, /=)
#undef ARITHMETIC_OP
#define COMPARE_OP(op)\
bool operator op(const modular_fixed_base &x) const{ return data op x.data; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
bool operator op(const T &x) const{ return data op modular_fixed_base(x).data; }\
template<class T, typename enable_if<IS_INTEGRAL(T)>::type* = nullptr>\
friend bool operator op(const T &x, const modular_fixed_base &y){ return modular_fixed_base(x).data op y.data; }
COMPARE_OP(==) COMPARE_OP(!=) COMPARE_OP(<) COMPARE_OP(<=) COMPARE_OP(>) COMPARE_OP(>=)
#undef COMPARE_OP
friend istream &operator>>(istream &in, modular_fixed_base &number){
long long x;
in >> x;
number.data = modular_fixed_base::_normalize(x);
return in;
}
#define _SHOW_FRACTION
friend ostream &operator<<(ostream &out, const modular_fixed_base &number){
out << number.data;
#if defined(LOCAL) && defined(_SHOW_FRACTION)
cerr << "(";
for(auto d = 1; ; ++ d){
if((number * d).data <= 1000000){
cerr << (number * d).data;
if(d != 1) cerr << "/" << d;
break;
}
else if((-number * d).data <= 1000000){
cerr << "-" << (-number * d).data;
if(d != 1) cerr << "/" << d;
break;
}
}
cerr << ")";
#endif
return out;
}
data_t data = 0;
#undef _SHOW_FRACTION
#undef IS_INTEGRAL
#undef IS_UNSIGNED
};
template<class data_t, data_t _mod> vector<modular_fixed_base<data_t, _mod>> modular_fixed_base<data_t, _mod>::_INV;
template<class data_t, data_t _mod> modular_fixed_base<data_t, _mod> modular_fixed_base<data_t, _mod>::_primitive_root;
const unsigned int mod = (119 << 23) + 1; // 998244353
// const unsigned int mod = 1e9 + 7; // 1000000007
// const unsigned int mod = 1e9 + 9; // 1000000009
// const unsigned long long mod = (unsigned long long)1e18 + 9;
using modular = modular_fixed_base<decay_t<decltype(mod)>, mod>;
// T must be of modular type
// mod must be a prime
// Requires modular
template<class T>
struct number_theoric_transform_wrapper{
// i \in [2^k, 2^{k+1}) holds w_{2^k+1}^{i-2^k}
static vector<T> root, buffer1, buffer2;
static void adjust_root(int n){
if(root.empty()) root = {1, 1};
for(auto k = (int)root.size(); k < n; k <<= 1){
root.resize(n, 1);
T w = T::primitive_root().power((T::mod() - 1) / (k << 1));
for(auto i = k; i < k << 1; ++ i) root[i] = i & 1 ? root[i >> 1] * w : root[i >> 1];
}
}
// n must be a power of two
// p must have next n memories allocated
// O(n * log(n))
static void transform(int n, T *p, bool invert = false){
assert(n && __builtin_popcount(n) == 1 && (T::mod() - 1) % n == 0);
for(auto i = 1, j = 0; i < n; ++ i){
int bit = n >> 1;
for(; j & bit; bit >>= 1) j ^= bit;
j ^= bit;
if(i < j) swap(p[i], p[j]);
}
adjust_root(n);
for(auto len = 1; len < n; len <<= 1) for(auto i = 0; i < n; i += len << 1) for(auto j = 0; j < len; ++ j){
T x = p[i + j], y = p[len + i + j] * root[len + j];
p[i + j] = x + y, p[len + i + j] = x - y;
}
if(invert){
reverse(p + 1, p + n);
T inv_n = T(1) / n;
for(auto i = 0; i < n; ++ i) p[i] *= inv_n;
}
}
static void transform(vector<T> &p, bool invert = false){
transform((int)p.size(), p.data(), invert);
}
// Double the length of the ntt array
// n must be a power of two
// p must have next 2n memories allocated
// O(n * log(n))
static void double_up(int n, T *p){
assert(n && __builtin_popcount(n) == 1 && (T().mod() - 1) % (n << 1) == 0);
buffer1.resize(n << 1);
for(auto i = 0; i < n; ++ i) buffer1[i << 1] = p[i];
transform(n, p, true);
adjust_root(n << 1);
for(auto i = 0; i < n; ++ i) p[i] *= root[n | i];
transform(n, p);
for(auto i = 0; i < n; ++ i) buffer1[i << 1 | 1] = p[i];
copy(buffer1.begin(), buffer1.begin() + 2 * n, p);
}
static void double_up(vector<T> &p){
int n = (int)p.size();
p.resize(n << 1);
double_up(n, p.data());
}
// O(n * m)
static vector<T> convolute_naive(const vector<T> &p, const vector<T> &q){
vector<T> res(max((int)p.size() + (int)q.size() - 1, 0));
for(auto i = 0; i < (int)p.size(); ++ i) for(auto j = 0; j < (int)q.size(); ++ j) res[i + j] += p[i] * q[j];
return res;
}
// O((n + m) * log(n + m))
static vector<T> convolute(const vector<T> &p, const vector<T> &q){
if(min(p.size(), q.size()) < 55) return convolute_naive(p, q);
int m = (int)p.size() + (int)q.size() - 1, n = 1 << __lg(m) + 1;
buffer1.resize(n, 0);
copy(p.begin(), p.end(), buffer1.begin());
transform(buffer1);
buffer2.resize(n, 0);
copy(q.begin(), q.end(), buffer2.begin());
transform(buffer2);
for(auto i = 0; i < n; ++ i) buffer1[i] *= buffer2[i];
transform(buffer1, true);
return vector<T>(buffer1.begin(), buffer1.begin() + m);
}
// O((n + m) * log(n + m))
static vector<T> square(const vector<T> &p){
if((int)p.size() < 40) return convolute_naive(p, p);
int m = 2 * (int)p.size() - 1, n = 1 << __lg(m) + 1;
buffer1.resize(n, 0);
copy(p.begin(), p.end(), buffer1.begin());
transform(buffer1);
for(auto i = 0; i < n; ++ i) buffer1[i] *= buffer1[i];
transform(buffer1, true);
return vector<T>(buffer1.begin(), buffer1.begin() + m);
}
// O((n + m) * log(n + m))
static vector<T> arbitrarily_convolute(const vector<T> &p, const vector<T> &q){
using modular0 = modular_fixed_base<unsigned int, 1045430273>;
using modular1 = modular_fixed_base<unsigned int, 1051721729>;
using modular2 = modular_fixed_base<unsigned int, 1053818881>;
using ntt0 = number_theoric_transform_wrapper<modular0>;
using ntt1 = number_theoric_transform_wrapper<modular1>;
using ntt2 = number_theoric_transform_wrapper<modular2>;
vector<modular0> p0((int)p.size()), q0((int)q.size());
for(auto i = 0; i < (int)p.size(); ++ i) p0[i] = p[i].data;
for(auto i = 0; i < (int)q.size(); ++ i) q0[i] = q[i].data;
auto xy0 = ntt0::convolute(p0, q0);
vector<modular1> p1((int)p.size()), q1((int)q.size());
for(auto i = 0; i < (int)p.size(); ++ i) p1[i] = p[i].data;
for(auto i = 0; i < (int)q.size(); ++ i) q1[i] = q[i].data;
auto xy1 = ntt1::convolute(p1, q1);
vector<modular2> p2((int)p.size()), q2((int)q.size());
for(auto i = 0; i < (int)p.size(); ++ i) p2[i] = p[i].data;
for(auto i = 0; i < (int)q.size(); ++ i) q2[i] = q[i].data;
auto xy2 = ntt2::convolute(p2, q2);
static const modular1 r01 = 1 / modular1(modular0::mod());
static const modular2 r02 = 1 / modular2(modular0::mod());
static const modular2 r12 = 1 / modular2(modular1::mod());
static const modular2 r02r12 = r02 * r12;
static const T w1 = modular0::mod();
static const T w2 = w1 * modular1::mod();
int n = (int)p.size() + (int)q.size() - 1;
vector<T> res(n);
for(auto i = 0; i < n; ++ i){
using ull = unsigned long long;
ull a = xy0[i].data;
ull b = (xy1[i].data + modular1::mod() - a) * r01.data % modular1::mod();
ull c = ((xy2[i].data + modular2::mod() - a) * r02r12.data + (modular2::mod() - b) * r12.data) % modular2::mod();
res[i] = xy0[i].data + w1 * b + w2 * c;
}
return res;
}
};
template<class T> vector<T> number_theoric_transform_wrapper<T>::root;
template<class T> vector<T> number_theoric_transform_wrapper<T>::buffer1;
template<class T> vector<T> number_theoric_transform_wrapper<T>::buffer2;
using ntt = number_theoric_transform_wrapper<modular>;
// Specialized for FFT
template<class T, class FFT>
struct power_series_base: vector<T>{
#define data (*this)
power_series_base &_inplace_transform(bool invert = false){
FFT::transform(data, invert);
return *this;
}
power_series_base _transform(bool invert = false) const{
return power_series_base(*this)._inplace_transform(invert);
}
template<class ...Args>
power_series_base(Args... args): vector<T>(args...){}
power_series_base(initializer_list<T> init): vector<T>(init){}
operator bool() const{
return find_if(data.begin(), data.end(), [&](const T &x){ return x != T{0}; }) != data.end();
}
// Returns \sum_{i=0}^{n-1} a_i/i! * X^i
static power_series_base EGF(vector<T> a){
int n = (int)a.size();
T fact = 1;
for(auto x = 2; x < n; ++ x) fact *= x;
fact = 1 / fact;
for(auto i = n - 1; i >= 0; -- i) a[i] *= fact, fact *= i;
return power_series_base(a);
}
// Returns exp(coef * X).take(n) = \sum_{i=0}^{n-1} coef^i/i! * X^i
static power_series_base EGF(int n, T coef = 1){
vector<T> a(n, 1);
for(auto i = 1; i < n; ++ i) a[i] = a[i - 1] * coef;
return EGF(a);
}
vector<T> EGF_to_seq() const{
int n = (int)data.size();
vector<T> seq(n);
T fact = 1;
for(auto i = 0; i < n; ++ i){
seq[i] = data[i] * fact;
fact *= i + 1;
}
return seq;
}
power_series_base &inplace_reduce(){
while(!data.empty() && !data.back()) data.pop_back();
return *this;
}
power_series_base reduce() const{
return power_series_base(*this).inplace_reduce();
}
friend ostream &operator<<(ostream &out, const power_series_base &p){
if(p.empty()) return out << "{}";
else{
out << "{";
for(auto i = 0; i < (int)p.size(); ++ i){
out << p[i];
i + 1 < (int)p.size() ? out << ", " : out << "}";
}
return out;
}
}
power_series_base &inplace_take(int n){
data.erase(data.begin() + min((int)data.size(), n), data.end());
data.resize(n, T{0});
return *this;
}
power_series_base take(int n) const{
auto res = vector<T>(data.begin(), data.begin() + min((int)data.size(), n));
res.resize(n, T{0});
return res;
}
power_series_base &inplace_drop(int n){
data.erase(data.begin(), data.begin() + min((int)data.size(), n));
return *this;
}
power_series_base drop(int n) const{
return vector<T>(data.begin() + min((int)data.size(), n), data.end());
}
power_series_base &inplace_slice(int l, int r){
assert(0 <= l && l <= r);
data.erase(data.begin(), data.begin() + min((int)data.size(), l));
data.resize(r - l, T{0});
return *this;
}
power_series_base slice(int l, int r) const{
auto res = vector<T>(data.begin() + min((int)data.size(), l), data.begin() + min((int)data.size(), r));
res.resize(r - l, T{0});
return res;
}
power_series_base &inplace_reverse(int n){
data.resize(max(n, (int)data.size()), T{0});
std::reverse(data.begin(), data.begin() + n);
return *this;
}
power_series_base reverse(int n) const{
return power_series_base(*this).inplace_reverse(n);
}
power_series_base &inplace_shift(int n, T x = T{0}){
data.insert(data.begin(), n, x);
return *this;
}
power_series_base shift(int n, T x = T{0}) const{
return power_series_base(*this).inplace_shift(n, x);
}
T evaluate(T x) const{
T res = {};
for(auto i = (int)data.size() - 1; i >= 0; -- i) res = res * x + data[i];
return res;
}
// Takes mod x^n-1
power_series_base &inplace_circularize(int n){
assert(n >= 1);
for(auto i = n; i < (int)data.size(); ++ i) data[i % n] += data[i];
data.resize(n, T{0});
return *this;
}
// Takes mod x^n-1
power_series_base circularize(int n) const{
return power_series_base(*this).inplace_circularize(n);
}
power_series_base operator*(const power_series_base &p) const{
return FFT::convolute(data, p);
}
power_series_base &operator*=(const power_series_base &p){
return *this = *this * p;
}
template<class U>
power_series_base &operator*=(U x){
for(auto &c: data) c *= x;
return *this;
}
template<class U>
power_series_base operator*(U x) const{
return power_series_base(*this) *= x;
}
template<class U>
friend power_series_base operator*(U x, power_series_base p){
for(auto &c: p) c = x * c;
return p;
}
// Compute p^e mod x^n - 1.
template<class U>
power_series_base &inplace_power_circular(U e, int n){
assert(n >= 1);
power_series_base p = *this;
data.assign(n, 0);
data[0] = 1;
for(; e; e >>= 1){
if(e & 1) (*this *= p).inplace_circularize(n);
(p *= p).inplace_circularize(n);
}
return *this;
}
template<class U>
power_series_base power_circular(U e, int len) const{
return power_series_base(*this).inplace_power_circular(e, len);
}
power_series_base &operator+=(const power_series_base &p){
data.resize(max(data.size(), p.size()), T{0});
for(auto i = 0; i < (int)p.size(); ++ i) data[i] += p[i];
return *this;
}
power_series_base operator+(const power_series_base &p) const{
return power_series_base(*this) += p;
}
template<class U>
power_series_base &operator+=(const U &x){
if(data.empty()) data.emplace_back();
data[0] += x;
return *this;
}
template<class U>
power_series_base operator+(const U &x) const{
return power_series_base(*this) += x;
}
template<class U>
friend power_series_base operator+(const U &x, const power_series_base &p){
return p + x;
}
power_series_base &operator-=(const power_series_base &p){
data.resize(max(data.size(), p.size()), T{0});
for(auto i = 0; i < (int)p.size(); ++ i) data[i] -= p[i];
return *this;
}
power_series_base operator-(const power_series_base &p) const{
return power_series_base(*this) -= p;
}
template<class U>
power_series_base &operator-=(const U &x){
if(data.empty()) data.emplace_back();
data[0] -= x;
return *this;
}
template<class U>
power_series_base operator-(const U &x) const{
return power_series_base(*this) -= x;
}
template<class U>
friend power_series_base operator-(const U &x, const power_series_base &p){
return -p + x;
}
power_series_base operator-() const{
power_series_base res = *this;
for(auto i = 0; i < data.size(); ++ i) res[i] = T{0} - res[i];
return res;
}
power_series_base &operator++(){
if(data.empty()) data.push_back(1);
else ++ data[0];
return *this;
}
power_series_base &operator--(){
if(data.empty()) data.push_back(-1);
else -- data[0];
return *this;
}
power_series_base operator++(int){
power_series_base result(*this);
if(data.empty()) data.push_back(1);
else ++ data[0];
return result;
}
power_series_base operator--(int){
power_series_base result(*this);
if(data.empty()) data.push_back(-1);
else -- data[0];
return result;
}
power_series_base &inplace_clear_range(int l, int r){
assert(0 <= l && l <= r);
for(auto i = l; i < min(r, (int)data.size()); ++ i) data[i] = T{0};
return *this;
}
power_series_base clear_range(int l, int r) const{
return power_series_base(*this).inplace_clear_range(l, r);
}
power_series_base &inplace_dot_product(const power_series_base &p){
for(auto i = 0; i < min(data.size(), p.size()); ++ i) data[i] *= p[i];
return *this;
}
power_series_base dot_product(const power_series_base &p) const{
return power_series_base(*this).inplace_power_series_product(p);
}
power_series_base &_inverse_doubled_up(power_series_base &f, const power_series_base &freq) const{
assert((f.size() & -f.size()) == f.size());
int s = f.size();
power_series_base buffer = take(s << 1);
buffer._inplace_transform();
buffer.inplace_dot_product(freq);
buffer._inplace_transform(true);
buffer.inplace_clear_range(0, s);
buffer._inplace_transform();
buffer.inplace_dot_product(freq);
buffer._inplace_transform(true);
f.resize(s << 1, T{0});
return f -= buffer.inplace_clear_range(0, s);
}
power_series_base &_inverse_doubled_up(power_series_base &f) const{
assert((f.size() & -f.size()) == f.size());
return _inverse_doubled_up(f, f.take(f.size() << 1)._transform());
}
// Returns the first n terms of the inverse series
// O(n * log(n))
power_series_base inverse(int n) const{
assert(!data.empty() && data[0] != T{0});
auto inv = 1 / data[0];
power_series_base res{inv};
for(auto s = 1; s < n; s <<= 1) _inverse_doubled_up(res);
res.resize(n, T{0});
return res;
}
// Returns the first n terms of the inverse series
// O(n * log(n))
power_series_base &inplace_inverse(int n){
return *this = this->inverse(n);
}
// O(n * log(n))
power_series_base &inplace_power_series_division(power_series_base p, int n){
int i = 0;
while(i < min(data.size(), p.size()) && !data[i] && !p[i]) ++ i;
data.erase(data.begin(), data.begin() + i);
p.erase(p.begin(), p.begin() + i);
(*this *= p.inverse(n)).resize(n, T{0});
return *this;
}
// O(n * log(n))
power_series_base power_series_division(const power_series_base &p, int n){
return power_series_base(*this).inplace_power_series_division(p, n);
}
// Euclidean division
// O(min(n * log(n), # of non-zero indices))
power_series_base &operator/=(const power_series_base &p){
int n = (int)p.size();
while(n && p[n - 1] == T{0}) -- n;
assert(n >= 1);
inplace_reduce();
if(data.size() < n){
data.clear();
return *this;
}
if(n - count(p.begin(), p.begin() + n, T{0}) <= 100){
T inv = 1 / p[n - 1];
static vector<int> indices;
for(auto i = 0; i < n - 1; ++ i) if(p[i]) indices.push_back(i);
power_series_base res((int)data.size() - n + 1);
for(auto i = (int)data.size() - 1; i >= n - 1; -- i) if(data[i]){
T x = data[i] * inv;
res[i - n + 1] = x;
for(auto j: indices) data[i - (n - 1 - j)] -= x * p[j];
}
indices.clear();
return *this = res;
}
power_series_base b;
n = data.size() - p.size() + 1;
b.assign(n, {});
copy(p.rbegin(), p.rbegin() + min(p.size(), b.size()), b.begin());
std::reverse(data.begin(), data.end());
data = FFT::convolute(data, b.inverse(n));
data.erase(data.begin() + n, data.end());
std::reverse(data.begin(), data.end());
return *this;
}
power_series_base operator/(const power_series_base &p) const{
return power_series_base(*this) /= p;
}
template<class U>
power_series_base &operator/=(U x){
assert(x);
T inv_x = T(1) / x;
for(auto &c: data) c *= inv_x;
return *this;
}
template<class U>
power_series_base operator/(U x) const{
return power_series_base(*this) /= x;
}
pair<power_series_base, power_series_base> divrem(const power_series_base &p) const{
auto q = *this / p, r = *this - q * p;
while(!r.empty() && r.back() == 0) r.pop_back();
return {q, r};
}
power_series_base &operator%=(const power_series_base &p){
int n = (int)p.size();
while(n && p[n - 1] == T{0}) -- n;
assert(n >= 1);
inplace_reduce();
if(data.size() < n) return *this;
if(n - count(p.begin(), p.begin() + n, 0) <= 100){
T inv = 1 / p[n - 1];
static vector<int> indices;
for(auto i = 0; i < n - 1; ++ i) if(p[i]) indices.push_back(i);
for(auto i = (int)data.size() - 1; i >= n - 1; -- i) if(data[i]){
T x = data[i] * inv;
data[i] = 0;
for(auto j: indices) data[i - (n - 1 - j)] -= x * p[j];
}
indices.clear();
return inplace_reduce();
}
return *this = this->divrem(p).second;
}
power_series_base operator%(const power_series_base &p) const{
return power_series_base(*this) %= p;
}
power_series_base &inplace_derivative(){
if(!data.empty()){
for(auto i = 0; i < data.size(); ++ i) data[i] *= i;
data.erase(data.begin());
}
return *this;
}
// p'
power_series_base derivative() const{
return power_series_base(*this).inplace_derivative();
}
power_series_base &inplace_derivative_shift(){
for(auto i = 0; i < data.size(); ++ i) data[i] *= i;
return *this;
}
// xP'
power_series_base derivative_shift() const{
return power_series_base(*this).inplace_derivative_shift();
}
power_series_base &inplace_antiderivative(){
T::precalc_inverse(data.size());
data.push_back(0);
for(auto i = (int)data.size() - 1; i >= 1; -- i) data[i] = data[i - 1] / i;
data[0] = 0;
return *this;
}
// Integral(P)
power_series_base antiderivative() const{
return power_series_base(*this).inplace_antiderivative();
}
power_series_base &inplace_shifted_antiderivative(){
T::precalc_inverse(data.size());
if(!data.empty()) data[0] = 0;
for(auto i = 1; i < data.size(); ++ i) data[i] /= i;
return *this;
}
// Integral(P/x)
power_series_base shifted_antiderivative() const{
return power_series_base(*this).inplace_shifted_antiderivative();
}
// O(n * log(n))
power_series_base &inplace_log(int n){
assert(!data.empty() && data[0] == 1);
if(!n){
data.clear();
return *this;
}
(*this = derivative() * inverse(n)).resize(n - 1, T{0});
inplace_antiderivative();
return *this;
}
// O(n * log(n))
power_series_base log(int n) const{
return power_series_base(*this).inplace_log(n);
}
// O(n * log(n))
power_series_base exp(int n) const{
assert(data.empty() || data[0] == 0);
power_series_base f{1}, g{1};
for(auto s = 1; s < n; s <<= 1){
power_series_base f2 = f.take(s << 1)._inplace_transform();
power_series_base g2 = g.take(s << 1)._inplace_transform();
power_series_base dt = take(s).inplace_derivative_shift();
power_series_base w = dt;
w._inplace_transform();
for(auto i = 0; i < s; ++ i) w[i] *= f2[i << 1];
w._inplace_transform(true);
w -= f.derivative_shift();
w.resize(s << 1, T{0});
w._inplace_transform();
w.inplace_dot_product(g2);
w._inplace_transform(true);
w.resize(s, T{0});
w.insert(w.begin(), s, 0);
w -= dt;
power_series_base z = take(s << 1);
z += w.inplace_shifted_antiderivative();
z._inplace_transform();
z.inplace_dot_product(f2);
z._inplace_transform(true);
f.resize(s << 1, T{0});
f += z.inplace_clear_range(0, s);
if(s << 1 < n) f._inverse_doubled_up(g, g2);
}
f.resize(n, T{0});
return f;
}
// O(n * log(n))
power_series_base &inplace_exp(int n){
return *this = this->exp(n);
}
// O(n * log(n))
template<class U>
power_series_base &inplace_power(U e, int n){
data.resize(n, T{0});
if(e == 0 || n == 0){
if(n) data[0] = T{1};
return *this;
}
if(e < 0) return inplace_inverse(n).inplace_power(-e, n);
if(all_of(data.begin(), data.end(), [&](auto x){ return x == T{0}; })) return *this;
int pivot = find_if(data.begin(), data.end(), [&](auto x){ return x; }) - data.begin();
if(pivot && e >= (n + pivot - 1) / pivot){
fill(data.begin(), data.end(), T{0});
return *this;
}
data.erase(data.begin(), data.begin() + pivot);
n -= pivot * e;
T pivot_c = T{1}, base = data[0];
for(auto x = e; x; x >>= 1, base *= base) if(x & 1) pivot_c *= base;
((*this /= data[0]).inplace_log(n) *= e).inplace_exp(n);
data.insert(data.begin(), pivot * e, T{0});
return *this *= pivot_c;
}
// O(n * log(n))
template<class U>
power_series_base power(U e, int n) const{
return power_series_base(*this).inplace_power(e, n);
}
// Suppose there are data[i] distinct objects with weight i.
// Returns a power series where i-th coefficient represents # of ways to select a set of objects with sum of weight i.
// O(n * log(n))
power_series_base &inplace_set(int n){
assert(!data.empty() && data[0] == T{0});
data.resize(n);
for(auto i = n - 1; i >= 1; -- i) for(auto j = 2 * i; j < n; j += i) data[j] += data[i];
for(auto i = 1; i < n; ++ i) (data[i] /= i) *= (i & 1 ? 1 : -1);
return inplace_exp(n);
}
power_series_base set(int n) const{
return power_series_base(*this).inplace_set(n);
}
// Suppose there are data[i] distinct objects with weight i.
// Returns a power series where i-th coefficient represents # of ways to select a multiset of objects with sum of weight i.
// O(n * log(n))
power_series_base &inplace_multiset(int n){
assert(!data.empty() && data[0] == T{0});
data.resize(n);
static vector<T> inv;
inv.resize(n);
for(auto i = 1; i < n; ++ i) inv[i] = T{1} / i;
for(auto i = n - 1; i >= 1; -- i) for(auto j = 2 * i; j < n; j += i) data[j] += data[i] * inv[j / i];
inv.clear(), inv.shrink_to_fit();
return inplace_exp(n);
}
power_series_base multiset(int n) const{
return power_series_base(*this).inplace_multiset(n);
}
static power_series_base multiply_all(const vector<power_series_base> &a){
if(a.empty()) return {1};
auto solve = [&](auto self, int l, int r)->power_series_base{
if(r - l == 1) return a[l];
int m = l + (r - l >> 1);
return self(self, l, m) * self(self, m, r);
};
return solve(solve, 0, (int)a.size());
}
friend power_series_base gcd(power_series_base p, power_series_base q){
while(q) p = exchange(q, p % q);
return p;
}
friend power_series_base lcm(power_series_base p, power_series_base q){
return p / gcd(p, q) * q;
}
#undef data
};
// Requires modular and number_theoric_transform
using power_series = power_series_base<modular, ntt>;
// DEBUG BEGIN
#ifdef LOCAL
// DECLARATION BEGIN
template<class L, class R> ostream &operator<<(ostream &out, const pair<L, R> &p);
template<class Tuple, size_t N> struct _tuple_printer;
template<class... Args> ostream &_print_tuple(ostream &out, const tuple<Args...> &t);
template<class ...Args> ostream &operator<<(ostream &out, const tuple<Args...> &t);
template<class T> ostream &operator<<(typename enable_if<!is_same<T, string>::value, ostream>::type &out, const T &arr);
ostream &operator<<(ostream &out, const _Bit_reference &bit);
template<size_t SZ> ostream &operator<<(ostream &out, const bitset<SZ> &b);
template<class T, class A, class C>
ostream &operator<<(ostream &out, priority_queue<T, A, C> pq);
// DECLARATION END
template<class L, class R> ostream &operator<<(ostream &out, const pair<L, R> &p){
return out << "{" << p.first << ", " << p.second << "}";
}
template<class Tuple, size_t N> struct _tuple_printer{
static ostream &_print(ostream &out, const Tuple &t){ return _tuple_printer<Tuple, N-1>::_print(out, t) << ", " << get<N-1>(t); }
};
template<class Tuple> struct _tuple_printer<Tuple, 1>{
static ostream &_print(ostream &out, const Tuple& t){ return out << get<0>(t); }
};
template<class... Args> ostream &_print_tuple(ostream &out, const tuple<Args...> &t){
return _tuple_printer<decltype(t), sizeof...(Args)>::_print(out << "{", t) << "}";
}
template<class ...Args> ostream &operator<<(ostream &out, const tuple<Args...> &t){
return _print_tuple(out, t);
}
template<class T> ostream &operator<<(typename enable_if<!is_same<T, string>::value, ostream>::type &out, const T &arr){
if(arr.empty()) return out << "{}";
out << "{";
for(auto it = arr.begin(); it != arr.end(); ++ it){
out << *it;
next(it) != arr.end() ? out << ", " : out << "}";
}
return out;
}
ostream &operator<<(ostream &out, const _Bit_reference &bit){
return out << bool(bit);
}
template<size_t SZ> ostream &operator<<(ostream &out, const bitset<SZ> &b){
for(auto i = 0; i < SZ; ++ i) out << b[i];
return out;
}
template<class T, class A, class C>
ostream &operator<<(ostream &out, priority_queue<T, A, C> pq){
vector<T> a;
while(!pq.empty()) a.push_back(pq.top()), pq.pop();
return out << a;
}
template<class Head>
void debug_out(Head H){ cerr << H << endl; }
template<class Head, class... Tail>
void debug_out(Head H, Tail... T){ cerr << H << ", ", debug_out(T...); }
void debug2_out(){ }
template<class Head, class... Tail>
void debug2_out(Head H, Tail... T){ cerr << "\n"; for(auto x: H) cerr << x << ",\n"; debug2_out(T...); }
template<class Width, class Head>
void debugbin_out(Width w, Head H){
for(auto rep = w; rep; -- rep, H >>= 1) cerr << (H & 1);
cerr << endl;
}
template<class Width, class Head, class... Tail>
void debugbin_out(Width w, Head H, Tail... T){
for(auto rep = w; rep; -- rep, H >>= 1) cerr << (H & 1);
cerr << ", "; debugbin_out(w, T...);
}
enum CODE{ CCRED = 31, CCGREEN = 32, CCYELLOW = 33, CCBLUE = 34, CCDEFAULT = 39 };
#define debug_endl() cerr << endl
#define debug(...) cerr << "\033[" << (int)CODE(CCRED) << "mL" << setw(3) << std::left << __LINE__ << std::right << " [" << #__VA_ARGS__ << "] \033[" << (int)CODE(CCBLUE) << "m", debug_out(__VA_ARGS__), cerr << "\33[" << (int)CODE(CCDEFAULT) << "m"
#define debug2(...) cerr << "\033[" << (int)CODE(CCRED) << "mL" << setw(3) << std::left << __LINE__ << std::right << " [" << #__VA_ARGS__ << "] \033[" << (int)CODE(CCBLUE) << "m", debug2_out(__VA_ARGS__), cerr << "\33[" << (int)CODE(CCDEFAULT) << "m"
#define debugbin(...) cerr << "\033[" << (int)CODE(CCRED) << "mL" << setw(3) << std::left << __LINE__ << std::right << " [" << #__VA_ARGS__ << "] \033[" << (int)CODE(CCBLUE) << "m", debugbin_out(__VA_ARGS__), cerr << "\33[" << (int)CODE(CCDEFAULT) << "m"
#else
#define debug_endl() 42
#define debug(...) 42
#define debug2(...) 42
#define debugbin(...) 42
#endif
// DEBUG END
int main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(ios::badbit | ios::failbit);
modular p, q;
cin >> p;
{
int x;
cin >> x;
p /= x;
}
cin >> q;
{
int x;
cin >> x;
q /= x;
}
int obj;
cin >> obj;
power_series f(obj + 1);
for(auto i = 1; i <= obj; ++ i){
f[i] = p * q.power(1LL * i * (i - 1) / 2);
}
debug(f);
(f *= (1 - f).inverse(obj + 1)) += 1;
debug(f);
modular res = 0;
for(auto i = 0; i <= obj; ++ i){
res += f[obj - i] * q.power(1LL * i * (i + 1) / 2);
}
cout << res << "\n";
return 0;
}
/*
*/