题解:P17080 「Miku × Nene」共鸣序列
P17080 「Miku × Nene」共鸣序列 题解
前言
生成函数大法好。
思路
用生成函数解决数列问题是很常见的方法。
对于本题,我们有递推式:
结合
解得:
那么题目的要求的,我们发现是:
这是因为:
而乘上
然后目的就是求出
我们将
分子那一部分可以拆成常数项,一次项,平方项分别处理,关键是分母。
按照常规的操作,我们设:
为了节省篇幅,这里介绍
将左右同乘
还是将左右同乘
然后现在最大的问题在于,有没有发现,
所以我们采取扩域的办法,将一元的数字扩域成二元的,具体的
然后写一下四则运算就好了。
剩下的还不会的话去看一下生成函数教学就可以了,复杂度
代码
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#include <numeric>
#include <vector>
#ifdef debug_mode
#include "EXTRA/debug"
#else
#define debug(...)
#define debug_numbers(...)
#endif
#define re_pr(val) return cout << #val "\n", void()
using namespace std;
typedef long long LL;
constexpr int mod = 998244353;
template <typename Tp1, typename Tp2>
inline void cmax(Tp1 &A, const Tp2 &B)
{
if (A < B) A = B;
return;
}
template <typename Tp1, typename Tp2>
inline void cmin(Tp1 &A, const Tp2 &B)
{
if (A > B) A = B;
return;
}
template <typename Tp1, typename Tp2, int mod = ::mod>
inline void ad(Tp1 &A, const Tp2 &B)
{
A = (A + B) % mod;
return;
}
template <typename Tp1, typename Tp2, int mod = ::mod>
inline void sb(Tp1 &A, const Tp2 &B)
{
A = (A + mod - B) % mod;
return;
}
constexpr int N = 200010;
LL quick_pow(LL base, LL ind)
{
LL res = 1ll;
while (ind)
{
if (ind & 1ll) res = res * base % mod;
ind >>= 1;
base = base * base % mod;
}
return res;
}
LL _div(LL x, LL y) { return x * quick_pow(y, mod - 2) % mod; }
LL a, b, p, q;
LL n, d;
struct my_t
{
my_t() = default;
my_t(LL _A, LL _B) : A(_A % mod), B(_B % mod) {}
LL A, B;
my_t operator+(const my_t &Q) const
{
return my_t((A + Q.A) % mod, (B + Q.B) % mod);
}
my_t operator-(const my_t &Q) const
{
return my_t((A + mod - Q.A) % mod, (B + mod - Q.B) % mod);
}
my_t operator*(const my_t &Q) const
{
return my_t((A * Q.A + B * Q.B % mod * d) % mod, (A * Q.B + B * Q.A) % mod);
}
my_t inv() const
{
LL iv = _div(1, ((A * A - B * B % mod * d) % mod + mod) % mod);
return my_t(A * iv % mod, (mod - B) * iv % mod);
}
my_t pow(LL ind) const
{
my_t res(1, 0);
my_t base = *this;
while (ind)
{
if (ind & 1ll) res = res * base;
ind >>= 1;
base = base * base;
}
return res;
}
void dbg() const { cerr << A << ' ' << B << endl; }
};
void solve()
{
cin >> a >> b >> p >> q >> n;
LL u = b - p * a;
u = (u % mod + mod) % mod;
d = (LL)p * p + (LL)q * 4;
d %= mod;
my_t r, s;
r = my_t(p * (mod + 1) / 2 % mod, (mod + 1) / 2);
s = my_t(p * (mod + 1) / 2 % mod, mod - (mod + 1) / 2);
// debug(r.A);
my_t e(1, 0);
my_t A, B, C, D, E;
A = ((e - r.inv()) * (e - s * r.inv()) * (e - s * r.inv())).inv();
// A.dbg();
B = ((e - r.inv()) * (e - r.inv()) * (e - s * r.inv()) * (e - s * r.inv()))
.inv();
B = B + my_t(2, 0) * s *
((e - r.inv()) * (e - s * r.inv()) * (e - s * r.inv()) *
(e - s * r.inv()))
.inv();
B = B * my_t(mod - r.A, mod - r.B).inv();
C = ((e - s.inv()) * (e - r * s.inv()) * (e - r * s.inv())).inv();
E = ((e - r) * (e - r) * (e - s) * (e - s)).inv();
D = e - A - B - C - E;
auto f = [=](LL x)
{
if (x < 0) return my_t(0, 0);
// if (x == 0) return my_t(1, 0);
// if (x == 1) return my_t(b, 0);
return A * r.pow(x) * my_t(x + 1, 0) + B * r.pow(x) +
C * s.pow(x) * my_t(x + 1, 0) + D * s.pow(x) + E;
};
my_t res1 = my_t((LL)a * a % mod, 0ll) * f(n);
my_t res2 = my_t(2ll * u * a % mod, 0ll) * f(n - 1);
my_t res3 = my_t((LL)u * u % mod, 0ll) * f(n - 2);
cout << ((res1 + res2 + res3).A % mod + mod) * p % mod * q % mod << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
while (T--) solve();
return 0;
}