權限題BZOJ4332php
容易想到\(dp\)
設\(g[i][j]\)表示前\(i\)人分到\(j\)顆糖的全部方案的乘積之和
設\(f(x) = Ox^2 + Sx + U\)
\[g[i][j] = \sum\limits_{k = 1}^{j - 1}g[i - 1][k]f(j - k)\]
是一個卷積的形式
\[g_n = f^{n}\]
但咱們的答案是
\[F_n = \sum\limits_{i = 1}^{n} g_{i,m}\]
有關係
\[F_n = F_x + F_{n - x}f^{x}\]
考慮倍增
\[F_n = F_{\frac{n}{2}} + F_{\frac{n}{2}}f^{\frac{n}{2}}\]
當\(n\)爲奇數時,在多算一個\(g_{n}\)即\(f^{n}\)便可ios
複雜度\(O(nlog^2n)\)spa
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<cmath> #include<map> #define LL long long int #define REP(i,n) for (int i = 1; i <= (n); i++) #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt) #define cls(s,v) memset(s,v,sizeof(s)) #define mp(a,b) make_pair<int,int>(a,b) #define cp pair<int,int> using namespace std; const int maxn = 40005,maxm = 100005,INF = 0x3f3f3f3f; inline int read(){ int out = 0,flag = 1; char c = getchar(); while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();} while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();} return flag ? out : -out; } const int G = 3,P = 998244353; int R[maxn],c[maxn]; inline int qpow(int a,int b){ int re = 1; for (; b; b >>= 1,a = 1ll * a * a % P) if (b & 1) re = 1ll * re * a % P; return re; } void NTT(int* a,int n,int f){ for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]); for (int i = 1; i < n; i <<= 1){ int gn = qpow(G,(P - 1) / (i << 1)); for (int j = 0; j < n; j += (i << 1)){ int g = 1,x,y; for (int k = 0; k < i; k++,g = 1ll * g * gn % P){ x = a[j + k],y = 1ll * g * a[j + k + i] % P; a[j + k] = (x + y) % P,a[j + k + i] = (x + P - y) % P; } } } if (f == 1) return; int nv = qpow(n,P - 2); reverse(a + 1,a + n); for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P; } int md; void conv(int* a,int* b,int* t,int deg1,int deg2){ int n = 1,L = 0; while (n <= (deg1 + deg2)) n <<= 1,L++; for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1)); for (int i = 0; i <= deg1; i++) t[i] = a[i]; for (int i = deg1 + 1; i < n; i++) t[i] = 0; for (int i = 0; i <= deg2; i++) c[i] = b[i]; for (int i = deg2 + 1; i < n; i++) c[i] = 0; NTT(t,n,1); NTT(c,n,1); for (int i = 0; i < n; i++) t[i] = 1ll * t[i] * c[i] % P; NTT(t,n,-1); for (int i = 0; i < n; i++) t[i] = t[i] % md; } int g[maxn],f[maxn],g1[maxn],tmp[maxn]; int M,N,O,S,U; void work(int k){ if (k == 1){ for (int i = 0; i <= M; i++) f[i] = g[i] = g1[i]; return; } work(k >> 1); conv(f,g,tmp,M,M); conv(g,g,g,M,M); for (int i = 0; i <= M; i++) f[i] = (f[i] + tmp[i]) % md; if (k & 1){ conv(g,g1,g,M,M); for (int i = 0; i <= M; i++) f[i] = (f[i] + g[i]) % md; } } int main(){ M = read(); md = read(); N = read(); O = read(); S = read(); U = read(); f[0] = g[0] = 1; for (int i = 1; i <= M; i++) g1[i] = (1ll * i * i * O % md + 1ll * i * S % md + U) % md; work(N); printf("%d\n",f[M]); return 0; }