「LOJ 138」类欧几里得算法

给出 $T$ 组询问,每组用 $n, a, b, c, k_1, k_2$ 来描述。对于每组询问,请你求出

$$
\sum_{x = 0} ^ {n} x ^ {k_1} {\left \lfloor \frac{ax + b}{c} \right \rfloor} ^ {k_2}
$$

对 $1000000007$ 取模。

链接

LOJ 138

题解

当 $a = 0$ 时,原式为

$$\lfloor \frac{b} {c} \rfloor ^ {k_2} \sum_{x = 0} ^ n x ^ {k_1}$$,此时就是一个自然数幂和,令 $B$ 表示伯努利数,$S_k(n) = \sum\limits_{x = 0} ^ n x ^ k$,则 $$S_k(n) = \frac {1} {k + 1}\sum_{j = 0} ^ k (-1) ^ j \binom{k + 1} {j} B_j n ^ {k + 1 - j}$$

令 $P_{i,j} = (-1) ^ j\binom{i + 1} {j} B_j$


当 $a \geq b$ 时,令 $a = qc + r, 0 \leq r \lt b$,原式为

$$\begin{aligned}\sum_{x = 0} ^ n x ^ {k_1} \lfloor \frac {ax + b} {c} \rfloor &= \sum_{x = 1} ^ n x ^ {k_1}(qx + \lfloor \frac{rx + b} {c} \rfloor) ^ {k_2} \\ &= \sum_{i = 0} ^ {k_2} \binom {k_2} {i}q ^ i \sum_{x = 0} ^ n x ^ {k_1 + i}\lfloor \frac{rx + b} {c} \rfloor ^ {k_2 - i}\end{aligned}$$。

当 $a \lt b$ 时,令 $y = \lfloor \frac{ax + b} {c} \rfloor$,则

$$\lfloor \frac{cy - b - 1} {a} \rfloor \lt x \leq \lfloor \frac{c(y + 1) - b - 1} {a} \rfloor $$

令 $m = \frac {an + b} {c}$,则

$$\sum_{x = 0} ^ n x ^ {k_1} \lfloor \frac{ax + b} {c} \rfloor ^ {k_2} = S_{k_1}(n)m ^ {k_2} + \sum_{y = 1} ^ m \big( (y - 1) ^ {k_2} - y ^ {k_2} \big)S_{k_1}(\lfloor \frac{cy - b - 1} {a} \rfloor)$$

$$(y - 1) ^ {k_2} - y ^ {k_2} = \sum_{i = 0} ^ {k_2 - 1}\binom{k_2} {i}(-1) ^ {k_2 - i}y ^ i$$

$$\sum_{x = 0} ^ {n}x ^ {k_1}\lfloor \frac {ax + b} {c} \rfloor ^ {k_2} = S_{k_1}(n)m ^ {k_2} + \sum_{i = 0} ^ {k_2 - 1}\sum_{h = 0} ^ {k_1 + 1}\big(\binom{k_2} {i}(-1) ^ {k_2 - i}P_{k_1, h}\sum_{y = 1} ^ m y ^ i\lfloor \frac{cy - b - 1} {c} \rfloor ^ h \big)$$

时间复杂度为 $O((k_1 + k_2) ^ 3\log(\min\{a, c\}))$。

代码

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright (c) 2016-2018, xehoth
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* 「LOJ 138」类欧几里得算法 18-05-2018
*
* @author xehoth
*/
#include <bits/stdc++.h>

const int MAXK = 10;
const int MOD = 1e9 + 7;

using uint = unsigned int;
using ull = unsigned long long;

inline int add(int x, int v) { return x + v >= MOD ? x + v - MOD : x + v; }

inline int dec(int x, int v) { return x - v < 0 ? x - v + MOD : x - v; }

int C[MAXK + 3][MAXK + 3], B[MAXK + 1], inv[MAXK + 3];
// P_{i,j} = (-1) ^ j\binom{i + 1} {j} B_j
int P[MAXK + 1][MAXK + 3];

inline int get(int k, int n) {
int ret = 0;
for (int i = 0; i < k + 2; i++) ret = ((ull)ret * n + P[k][i]) % MOD;
return (ull)ret * inv[k + 1] % MOD;
}

inline void init() {
inv[0] = inv[1] = 1;
for (int i = 2; i <= MAXK + 1; i++)
inv[i] = (MOD - MOD / i) * (ull)inv[MOD % i] % MOD;
for (int i = 0; i <= MAXK + 1; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++)
C[i][j] = add(C[i - 1][j], C[i - 1][j - 1]);
}
B[0] = 1;
for (int i = 1, s; i <= MAXK; i++) {
s = 0;
for (int j = 0; j < i; j++) s = (s + (ull)C[i + 1][j] * B[j]) % MOD;
B[i] = (ull)(MOD - s) * inv[i + 1] % MOD;
}
for (int i = 0; i <= MAXK; i++) {
for (int j = 0; j <= i; j++) {
P[i][j] = (j & 1) ? ((ull)(MOD - C[i + 1][j]) * B[j] % MOD)
: ((ull)C[i + 1][j] * B[j] % MOD);
}
P[i][i + 1] = 0;
}
P[0][1] = 1;
}

/**
* \sum_{x = 0} ^ {n} x ^ {k_1} {\left \lfloor
* \frac{ax + b}{c} \right \rfloor} ^ {k_2}
*/
inline int classEuclid(int n, int a, int b, int c, int k1, int k2) {
using Info = std::tuple<int, int, int, int>;
static std::vector<Info> st;
st.clear();
st.reserve(std::__lg(std::min(a, c)) + 3);
for (;;) {
st.emplace_back(n, a, b, c);
if (n < 0 || a == 0) break;
if (a >= c) {
a %= c;
} else if (b >= c) {
b %= c;
} else {
n = ((long long)a * n + b) / c - 1;
b = c - b - 1;
std::swap(a, c);
}
}

const int S = k1 + k2;
static int buc[2][MAXK + 1][MAXK + 1];
using Ptr = int(*)[MAXK + 1];
Ptr f = buc[0], g = buc[1];
while (!st.empty()) {
std::tie(n, a, b, c) = st.back();
st.pop_back();
if (n < 0) {
continue;
} else if (a == 0) {
int q = b / c;
for (int i = 0, s; i <= S; i++) {
s = get(i, n);
for (int j = 0; j <= S - i; j++) {
g[i][j] = s;
s = (ull)s * q % MOD;
}
}
} else if (a >= c || b >= c) {
int q = (a >= c) ? a / c : b / c, d = (a >= c) ? 1 : 0;
// \sum_{i = 0} ^ {k_2} \binom {k_2} {i}q ^ i \sum_{x = 0} ^ n x ^
// {k_1 + i}\lfloor \frac{rx + b} {c} \rfloor ^ {k_2 - i}
for (int k1 = 0; k1 <= S; k1++) {
for (int k2 = 0, s; k2 <= S - k1; k2++) {
s = 0;
for (int i = 0, p = 1; i <= k2; i++) {
s = (s +
(ull)p * C[k2][i] % MOD * f[k1 + i * d][k2 - i]) %
MOD;
p = (ull)p * q % MOD;
}
g[k1][k2] = s % MOD;
}
}
} else {
// S_{k_1}(n)m ^ {k_2} + \sum_{i = 0} ^ {k_2 - 1}\sum_{h = 0} ^ {k_1
// + 1}\big(\binom{k_2} {i}(-1) ^ {k_2 - i}P_{k_1, h}\sum_{y = 1} ^
// m y ^ i\lfloor \frac{cy - b - 1} {c} \rfloor ^ h \big)
static int h[MAXK + 1][MAXK + 1];
for (int k2 = 0; k2 <= S - 1; k2++) {
for (int k1 = 0, s; k1 <= S - k2 - 1; k1++) {
s = 0;
for (int j = 0; j <= k1 + 1; ++j) {
s = (s + (ull)P[k1][k1 + 1 - j] * f[k2][j]) % MOD;
}
h[k1][k2] = (ull)s * inv[k1 + 1] % MOD;
}
}
const int m = ((ull)a * n + b) / c;
for (int k1 = 0; k1 <= S; k1++) {
int p = get(k1, n);
for (int k2 = 0, t; k2 <= S - k1; k2++) {
t = 0;
for (int i = 0; i < k2; i++)
t = (t + (ull)h[k1][i] * C[k2][i]) % MOD;
g[k1][k2] = dec(p, t);
p = (ull)p * m % MOD;
}
}
}
std::swap(f, g);
}
return f[k1][k2];
}

int main() {
// freopen("sample/1.in", "r", stdin);
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int T, n, a, b, c, k1, k2;
init();
for (std::cin >> T; T--;) {
std::cin >> n >> a >> b >> c >> k1 >> k2;
std::cout << classEuclid(n, a, b, c, k1, k2) << '\n';
}
return 0;
}
#

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×