dmy4560 字字句句
naoliaok_lovely · · 题解
原题链接
前面
sub6
将递推式写成 GF,为:
考虑使用 BM 提取远端系数。以平方项为例,
:::info[模拟一下上面 sub3 递归的过程]{open}
以下不讨论奇偶,用箭头是因为只保留了递归的项,舍弃了常数项。
| 这里看起来后面会乘一大坨式子,但是我们只需要保留可以凑出 |
|---|
可以发现,我们处理的一定是
sub7
生成函数变为
重新考察此时的 BM 是怎么进行的:(其中
我们着重考察分母的变化,设
然后分别提取平方项/立方项,发现存在非常明显的规律:假设做了
分析一下复杂度,所有子问题形如
实现细节
上面只能算理论分析,代码方面还有一些问题。
注意到
其中平方操作是容易的,
常数方面,注意到大部分运算还是整数,只有极少部分运算是扩域后的。这是因为
code
#include<bits/stdc++.h>
using namespace std;
const int N = 1 << 13 | 10, M = 70, mod = 998244353;
namespace poly
{
#define LL long long
inline int lg2(int n)
{
int k = 0;
while(1 << k + 1 <= n) k++;
return k;
}
LL ksm(LL x, LL y)
{
LL res = 1;
x %= mod;
while(y)
{
if(y & 1) res = res * x % mod;
y >>= 1;
x = x * x % mod;
}
return res;
}
struct Complex
{
int x, y;
Complex() {x = y = 0;}
Complex(int x1) {x = x1, y = 0;}
Complex(int x1, int y1) {x = x1, y = y1;}
Complex operator-() const
{
return Complex(x ? mod - x : 0, y ? mod - y : 0);
}
Complex operator+(const Complex &t) const
{
int a = x + t.x, b = y + t.y;
return Complex(a < mod ? a : a - mod, b < mod ? b : b - mod);
}
Complex operator-(const Complex &t) const
{
int a = x - t.x, b = y - t.y;
return Complex(a >= 0 ? a : a + mod, b >= 0 ? b : b + mod);
}
Complex operator*(const Complex &t) const
{
if(!y && !t.y) return Complex(1ll * x * t.x % mod, 0);
int a = (1ll * x * t.x - 1ll * y * t.y % mod + mod) % mod;
int b = (1ll * x * t.y + 1ll * y * t.x - 1ll * y * t.y % mod + mod) % mod;
return Complex(a, b);
}
Complex& operator+=(const Complex &t) {return *this = *this + t;}
Complex& operator-=(const Complex &t) {return *this = *this - t;}
Complex& operator*=(const Complex &t) {return *this = *this * t;}
}omega(0, 1), omega2(mod - 1, mod - 1);
int bit, tot;
int w[N], inv_num[N];
struct Initial
{
Initial()
{
bit = lg2(N), tot = 1 << bit;
w[tot >> 1] = ksm(3, mod - 1 >> bit + 1), w[0] = 1;
for(int i = bit - 1; i; i--) w[1 << i - 1] = 1ll * w[1 << i] * w[1 << i] % mod;
for(int i = 1; i < tot; i++) w[i] = 1ll * w[i & -i] * w[i ^ (i & -i)] % mod;
inv_num[1] = 1;
for(int i = 2; i <= tot; i++) inv_num[i] = 1ll * (mod - mod / i) * inv_num[mod % i] % mod;
}
}initial;
void DFT(Complex a[])
{
for(int len = tot >> 1; len; len >>= 1)
for(int i = 0, l = 0; i < tot; i += len << 1, l++)
for(int j = i; j < i + len; j++)
{
Complex x = a[j], y = a[j + len] * w[l];
a[j] = x + y, a[j + len] = x - y;
}
}
void IDFT(Complex a[])
{
for(int len = 1; len < tot; len <<= 1)
for(int i = 0, l = 0; i < tot; i += len << 1, l++)
for(int j = i; j < i + len; j++)
{
Complex x = a[j], y = a[j + len];
a[j] = x + y, a[j + len] = (x - y) * w[l];
}
for(int i = 1; i < tot >> 1; i++) swap(a[i], a[tot - i]);
for(int i = 0; i < tot; i++) a[i] = a[i] * inv_num[tot];
}
void NTT(Complex a[], int inv)
{
inv == 1 ? DFT(a) : IDFT(a);
}
}
using namespace poly;
using Poly = vector<Complex>;
LL n, ans;
bool flag[10];
Poly p[M][M];
Poly operator+(const Poly &a, const Poly &b)
{
Poly c(max(a.size(), b.size()));
for(int i = 0; i < c.size(); i++)
{
if(i < a.size()) c[i] += a[i];
if(i < b.size()) c[i] += b[i];
}
return c;
}
Poly operator-(const Poly &a, const Poly &b)
{
Poly c(max(a.size(), b.size()));
for(int i = 0; i < c.size(); i++)
{
if(i < a.size()) c[i] += a[i];
if(i < b.size()) c[i] -= b[i];
}
return c;
}
Poly operator*(const Poly &a, const Poly &b)
{
Poly c(a.size() + b.size() - 1);
static Complex x[N], y[N];
bit = lg2(c.size()) + 1, tot = 1 << bit;
for(int i = 0; i < a.size(); i++) x[i] = a[i];
for(int i = a.size(); i < tot; i++) x[i] = 0;
for(int i = 0; i < b.size(); i++) y[i] = b[i];
for(int i = b.size(); i < tot; i++) y[i] = 0;
NTT(x, 1), NTT(y, 1);
for(int i = 0; i < tot; i++) x[i] *= y[i];
NTT(x, -1);
for(int i = 0; i < c.size(); i++) c[i] = x[i];
return c;
}
Poly& operator+=(Poly &a, const Poly &b)
{
return a = a + b;
}
Poly& operator*=(Poly &a, const Poly &b)
{
return a = a * b;
}
LL pw[M][M];
Poly gf, f[M][M], mul[M][M], mul1[M], mul2[M];
void init()
{
gf = {1, mod - 1, flag[2] ? mod - 1 : 0, flag[3] ? mod - 1 : 0};
for(int i = 0; ; i++)
{
if(i == 0)
{
pw[0][0] = 1;
f[0][0] = gf;
}
else
{
if(!flag[4]) break;
pw[i][0] = pw[i - 1][0] * 2;
if(pw[i][0] > n)
{
pw[i][0] = 0;
break;
}
Poly g = f[i - 1][0];
for(int j = 1; j < g.size(); j += 2)
g[j] = -g[j];
Poly t = g * f[i - 1][0];
f[i][0].resize(f[i - 1][0].size());
for(int j = 0; j < f[i][0].size(); j++)
f[i][0][j] = t[j << 1];
}
for(int j = 1; flag[5]; j++)
{
pw[i][j] = pw[i][j - 1] * 3;
if(pw[i][j] > n)
{
pw[i][j] = 0;
break;
}
Poly g1 = f[i][j - 1], g2 = f[i][j - 1];
Complex pw = 1;
for(int k = 0; k < g1.size(); k++, pw *= omega)
g1[k] *= pw;
pw = 1;
for(int k = 0; k < g2.size(); k++, pw *= omega2)
g2[k] *= pw;
Poly t = g1 * g2 * f[i][j - 1];
f[i][j].resize(f[i][j - 1].size());
for(int k = 0; k < f[i][j].size(); k++)
f[i][j][k] = t[k * 3];
}
}
for(int i = 0; pw[i][0]; i++)
for(int j = 0; pw[i][j]; j++)
{
mul[i][j] = f[i][j];
if(i) mul[i][j] *= mul[i - 1][j];
}
for(int i = 0; pw[i][0]; i++)
for(int j = 1; pw[i][j]; j++)
mul[i][j] *= mul[i][j - 1];
mul1[0].push_back(1), mul2[0].push_back(1);
for(int i = 1; pw[i][0]; i++)
mul1[i] = mul1[i - 1] * f[i][0];
for(int i = 1; pw[0][i]; i++)
mul2[i] = mul2[i - 1] * f[0][i];
}
void BM(LL n, Poly a, Poly b)
{
while(n)
{
if(a.size() > n + 1) a.resize(n + 1);
if(b.size() > n + 1) b.resize(n + 1);
Poly c = a;
for(int i = 1; i < c.size(); i += 2)
c[i] = -c[i];
Poly t1 = a * c, t2 = b * c;
for(int i = 0; i < a.size(); i++)
a[i] = t1[i << 1];
b.resize((t2.size() - 1 - (n & 1)) / 2 + 1);
for(int i = 0; i < b.size(); i++)
b[i] = t2[i << 1 | n & 1];
n >>= 1;
}
ans = (ans + b[0].x) % mod;
}
int main()
{
freopen("ex_6.in", "r", stdin);
cin >> n >> flag[1] >> flag[2] >> flag[3] >> flag[4] >> flag[5];
init();
p[0][0].push_back(1);
for(int i = 0; pw[i][0]; i++)
for(int j = 0; pw[i][j]; j++)
{
if(i)
{
Poly g = mul[i - 1][j];
for(int k = 1; k < g.size(); k += 2)
g[k] = -g[k];
Poly t = g * p[i - 1][j];
int d = n / pw[i - 1][j] % 2;
Poly p1((t.size() - 1 - d) / 2 + 1);
for(int k = 0; k < p1.size(); k++)
p1[k] = t[k << 1 | d];
p[i][j] += p1 * mul2[j];
}
if(j)
{
Poly g1 = mul[i][j - 1], g2 = mul[i][j - 1];
Complex pw1 = 1;
for(int k = 0; k < g1.size(); k++, pw1 *= omega)
g1[k] *= pw1;
pw1 = 1;
for(int k = 0; k < g2.size(); k++, pw1 *= omega2)
g2[k] *= pw1;
Poly t = g1 * g2 * p[i][j - 1];
int d = n / pw[i][j - 1] % 3;
Poly p1((t.size() - 1 - d) / 3 + 1);
for(int k = 0; k < p1.size(); k++)
p1[k] = t[k * 3 + d];
p[i][j] += p1 * mul1[i];
}
BM(n / pw[i][j] - 1, mul[i][j], p[i][j]);
}
cout << ans << endl;
cerr << "\ntimes: " << clock() << "ms" << endl;
return 0;
}