题解:P17080 「Miku × Nene」共鸣序列

· · 题解

P17080 「Miku × Nene」共鸣序列 题解

前言

生成函数大法好。

思路

用生成函数解决数列问题是很常见的方法。

对于本题,我们有递推式:

f_n=pf_{n-1}+qf_{n-2}

结合 f_0=a,f_1=b,我们有生成函数 f(x) 的方程:

f(x)=pxf(x)+qx^2f(x)+a+(b-ap)x

解得:

f(x)=\frac{a+(b-ap)x}{1-px-qx^2}

那么题目的要求的,我们发现是:

[x^n]\frac{pqf^2(x)}{1-x}

这是因为:

f^2(x)=\sum_{i\ge0}\sum_{0\le j\le i} f_jf_{i-j}x^i

而乘上 \frac{1}{1-x} 相当于做前缀和操作。

然后目的就是求出 f^2(x) 了。

我们将 1-px-qx^2 分解为 (1-rx)(1-sx),那么 \frac{f^2(x)}{1-x} 就等于:

\frac{(a+(b-ap)x)^2}{(1-x)(1-rx)^2(1-sx)^2}

分子那一部分可以拆成常数项,一次项,平方项分别处理,关键是分母。

按照常规的操作,我们设:

\frac{1}{{(1-x)(1-rx)^2(1-sx)^2}}=\frac{A}{(1-rx)^2}+\frac{B}{1-rx}+\frac{C}{(1-sx)^2}+\frac{D}{1-sx}+\frac{E}{1-x}

为了节省篇幅,这里介绍 A,B,E 怎么求。

A=\left.\frac{1}{{(1-x)(1-sx)^2}}\right|_{x=\frac{1}{r}}

将左右同乘 A 部分的分母就能看出来为什么。

\left.\frac{\mathrm{d}}{\mathrm{d}x}(1-rx)B\right|_{x=\frac{1}{r}}=\left.\frac{\mathrm{d}}{\mathrm{d}x}\frac{1}{{(1-x)(1-sx)^2}}\right|_{x=\frac{1}{r}}

还是将左右同乘 A 的分母,然后对两边微分,就得出来了 B。

E=\left.\frac{1}{{(1-rx)^2(1-sx)^2}}\right|_{x=1}

然后现在最大的问题在于,有没有发现,r,s 是带根号的。。。

所以我们采取扩域的办法,将一元的数字扩域成二元的,具体的 (i,j) 代表 i+j\sqrt{p^2+4q}。

然后写一下四则运算就好了。

剩下的还不会的话去看一下生成函数教学就可以了,复杂度 \mathcal{O}(T\log_2n),瓶颈在快速幂,常数巨大。

代码

#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;
}