首先普通 $DP$ 式子很好列

令 $f[i][j]$ 表示 $i$ 个小朋友分 $j$ 个糖果且每人至少一颗的答案,则有
$$
f[i][j] = \sum\limits_{k = 1}^j f[i - 1][j - k](Ok^2 + Sk + U)
$$
接下来用生成函数优化该式

令 $g[k] = Ok^2 + Sk + U$,则
$$
f_i(x) = f_{i - 1}(x)g(x)
$$
其中 $f_0(x) = 1$

所以答案即为
$$
[x^M] \sum\limits_{i = 0}^A f_i(x) = [x^M] \sum\limits_{i = 0}^A (g(x))^i = [x^M] \frac{1 - (g(x))^{A + 1}}{1 - g(x)}
$$

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

typedef long long LL;

const int MAXN = 1e05 + 10;

const double PI = acos (- 1.0);

int N, M;
LL MOD;
LL O, S, U;
LL a[MAXN]= {0}, b[MAXN]= {0};

struct mcomplex {
double a, b;

mcomplex (double fa = 0.0, double fb = 0.0) :
a (fa), b (fb) {}

mcomplex operator + (const mcomplex& p) const {
return mcomplex (a + p.a, b + p.b);
}
mcomplex operator - (const mcomplex& p) const {
return mcomplex (a - p.a, b - p.b);
}
mcomplex operator * (const mcomplex& p) const {
return mcomplex (a * p.a - b * p.b, a * p.b + b * p.a);
}
} ;

int oppo[MAXN << 2];
int limit;
void FFT (mcomplex* a, int inv) {
for (int i = 0; i < limit; i ++)
if (i < oppo[i])
swap (a[i], a[oppo[i]]);
for (int mid = 1; mid < limit; mid <<= 1) {
mcomplex omega = mcomplex (cos (PI / (double) mid), inv * sin (PI / (double) mid));
for (int n = mid << 1, j = 0; j < limit; j += n) {
mcomplex x = mcomplex (1.0, 0);
for (int k = 0; k < mid; k ++, x = x * omega) {
mcomplex a1 = a[j + k], xa2 = x * a[j + mid + k];;
a[j + k] = a1 + xa2;
a[j + k + mid] = a1 - xa2;
}
}
}
}
void aclimit (int maxl) {
int n, lim;
for (n = 1, lim = 0; n <= maxl; n <<= 1, lim ++);
for (int i = 0; i < n; i ++)
oppo[i] = (oppo[i >> 1] >> 1) | ((i & 1) << (lim - 1));
limit = n;
}
mcomplex a1[MAXN << 2], a2[MAXN << 2];
void multiply (LL* A, LL* B, int n, int m) {
aclimit (n + m);
for (int i = 0; i < limit; i ++)
a1[i] = a2[i] = mcomplex ();
for (int i = 0; i <= n; i ++)
a1[i] = mcomplex ((double) A[i], 0);
for (int i = 0; i <= m; i ++)
a2[i] = mcomplex ((double) B[i], 0);
FFT (a1, 1), FFT (a2, 1);
for (int i = 0; i < limit; i ++)
a1[i] = a1[i] * a2[i];
FFT (a1, - 1);
for (int i = 0; i <= n + m; i ++)
A[i] = (LL) (a1[i].a / limit + 0.5) % MOD;
}
LL inv[MAXN << 2]= {0};
LL temp[MAXN << 2]= {0};
void inverse (int deg, LL* A, LL* B) {
if (deg == 1) {
B[0] = 1;
return ;
}
inverse ((deg + 1) >> 1, A, B);
aclimit (deg << 1);
for (int i = 0; i < limit; i ++)
temp[i] = 0;
for (int i = 0; i < deg; i ++)
temp[i] = A[i];
multiply (temp, B, deg, deg);
multiply (temp, B, deg, deg);
for (int i = 0; i < deg; i ++)
B[i] = (2 * B[i] % MOD - temp[i] + MOD) % MOD;
}
LL ans[MAXN << 2]= {0};
void power (LL* A, int p) {
ans[0] = 1;
while (p) {
if (p & 1)
multiply (ans, a, M, M);
multiply (a, a, M, M);
p >>= 1;
}
}

int getnum () {
int num = 0;
char ch = getchar ();
int isneg = 0;

while (! isdigit (ch)) {
if (ch == '-')
isneg = 1;
ch = getchar ();
}
while (isdigit (ch))
num = (num << 3) + (num << 1) + ch - '0', ch = getchar ();

return isneg ? - num : num;
}

int main () {
M = getnum (), MOD = getnum (), N = getnum ();
O = getnum (), S = getnum (), U = getnum ();
a[0] = 0;
for (int i = 1; i <= M; i ++)
a[i] = (O * i * i + S * i + U) % MOD;
for (int i = 1; i <= M; i ++)
b[i] = (- a[i] + MOD) % MOD;
b[0] = 1;
power (ans, N + 1);
for (int i = 0; i <= M; i ++)
ans[i] = (- ans[i] + MOD) % MOD;
ans[0] = (ans[0] + 1) % MOD;
inverse (M + 1, b, inv);
multiply (ans, inv, M, M);
cout << ans[M] << endl;

return 0;
}

/*
4 100
4
1
0
0
*/