Luogu 3321 [SDOI2015]序列统计

时间:2024-11-09 16:34:02

BZOJ 3992

点开这道题之后才发现我对原根的理解大概只停留在$998244353$的原根是$3$……

关于原根: 点我

首先写出$dp$方程,设$f_{i, j}$表示序列长度为$i$当前所有数乘积模$m$为$j$的方案数,有转移

$$f_{i, x * y \mod m} = \sum_{y \in s} f_{i - 1, x}$$

把$x$和$y$取个对数就可以变成卷积的形式了。

然而在模意义下,我们可以用原根的$k$次方来代替原来的数,这样子就达到了取对数的效果。

注意到每一次转移形式都是相同的,我们可以用快速幂的方式来优化。

时间复杂度$O(mlogmlogn)$。

然而我的代码只有在有$c++11$的时候才是对的,哪位大佬如果知道了为什么教教我呗。

Code:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector <ll> poly; const int N = ; int n, m, tar, fac[N], buc[N]; namespace Poly {
const int L = << ;
const ll P = 1004535809LL; int lim, pos[L]; template <typename T>
inline void inc(T &x, T y) {
x += y;
if (x >= P) x -= P;
} inline ll fpow(ll x, ll y, ll mod = P) {
ll res = ;
for (; y > ; y >>= ) {
if (y & ) res = res * x % mod;
x = x * x % mod;
}
return res;
} inline void prework(int len) {
int l = ;
for (lim = ; lim < len; lim <<= , ++l);
for (int i = ; i < lim; i++)
pos[i] = (pos[i >> ] >> ) | ((i & ) << (l - ));
} inline void ntt(poly &c, int opt) {
c.resize(lim, );
for (int i = ; i < lim; i++)
if (i < pos[i]) swap(c[i], c[pos[i]]);
for (int i = ; i < lim; i <<= ) {
ll wn = fpow(, (P - ) / (i << ));
if (opt == -) wn = fpow(wn, P - );
for (int len = i << , j = ; j < lim; j += len) {
ll w = ;
for (int k = ; k < i; k++, w = w * wn % P) {
ll x = c[j + k], y = w * c[j + k + i] % P;
c[j + k] = (x + y) % P, c[j + k + i] = (x - y + P) % P;
}
}
} if (opt == -) {
ll inv = fpow(lim, P - );
for (int i = ; i < lim; i++) c[i] = c[i] * inv % P;
}
} poly operator * (const poly x, const poly y) {
poly u = x, v = y, res;
prework(x.size() + y.size() - );
ntt(u, ), ntt(v, );
for (int i = ; i < lim; i++) res.push_back(u[i] * v[i] % P);
ntt(res, -);
res.resize(x.size() + y.size() - );
return res;
} inline void adj(poly &c) {
for (int i = ; i < m - ; i++) inc(c[i], c[i + m - ]);
c.resize(m - );
} inline poly pow(poly x, int y) {
poly res;
res.resize(m - );
res[] = ;
for (; y; y >>= ) {
if (y & ) res = res * x, adj(res);
x = x * x, adj(x);
}
return res;
} } using Poly :: P;
using Poly :: fpow;
using Poly :: pow; template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for (; ch > ''|| ch < ''; ch = getchar())
if (ch == '-') op = -;
for (; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int getRoot() {
int cnt = ;
for (int i = ; i <= m - ; i++)
if ((m - ) % i == ) fac[++cnt] = i;
for (int i = ; ; i++) {
bool flag = ;
for (int j = ; j <= cnt; j++)
if (fpow(i, fac[j], m) == ) {
flag = ;
break;
}
if (flag) return i;
}
} int main() {
/* #ifndef ONLINE_JUDGE
freopen("8.in", "r", stdin);
#endif */ int s, root;
read(n), read(m), read(tar), read(s); root = getRoot();
for (int i = ; i <= m - ; i++) buc[fpow(root, i, m)] = i; poly trans;
trans.resize(m - );
for (int x, i = ; i <= s; i++) {
read(x);
if (!x) continue;
trans[buc[x]] = ;
}
poly ans = pow(trans, n); printf("%lld\n", ans[buc[tar]]);
return ;
}