loj138
题意
给 \(n,\ a,\ b,\ c,\ k_1,\ k_2\)。
\(T\) 组询问,问 \(\sum_{x = 0} ^ {n} x ^ {k_1} {\left \lfloor \frac{ax + b}{c} \right \rfloor} ^ {k_2} \bmod\ (10^9\ +\ 7)\)。
\(T = 1000, 1 \le n, a, b, c \le {10} ^ 9, 0 \le k_1 + k_2 \le 10\)。
做法1
令 \(F(k_1,\ k_2,\ a,\ b,\ c,\ n)\ =\ \sum_{x = 0} ^ {n} x ^ {k_1} {\left \lfloor \frac{ax + b}{c} \right \rfloor} ^ {k_2}\)。
当 \(a\ >\ c\) 或 \(b\ >\ c\) 时,令 \(d\ =\ a\ \bmod\ c,\ e\ =\ b\ \bmod\ c\)。则 \(F(k_1,\ k_2,\ a,\ b,\ c,\ n)\ =\ \sum_{x = 0} ^ {n} x ^ {k_1} (\left \lfloor \frac{a}{c} \right \rfloor\ x\ +\ \left \lfloor \frac{b}{c} \right \rfloor\ +\ \left \lfloor \frac{dx + e}{c} \right \rfloor) ^ {k_2}\\=\ \sum_{i\ +\ j\ +\ k\ =\ k_2,\ i,\ j,\ k\ \geq\ 0}\ \binom{k2}{i,\ j,\ k}\ \left \lfloor \frac{a}{c} \right \rfloor^i\ \left \lfloor \frac{b}{c} \right \rfloor^j\ F(k_1\ +\ i,\ k,\ d,\ e,\ c,\ n)\)。
否则 \(F(k_1,\ k_2,\ a,\ b,\ c,\ n)\ =\ \sum_{x\ =\ 0}^n\ x^{k_1}\ \sum_{t\ =\ 0}^{\left \lfloor \frac{ax + b}{c} \right \rfloor\ -\ 1}((t\ +\ 1)^{k_2}\ -\ t^{k_2})\\=\ \sum_{t\ =\ 0}^{\left \lfloor \frac{an + b}{c} \right \rfloor\ -\ 1}\ ((t\ +\ 1)^{k_2}\ -\ t^{k_2})\ \sum_{x\ =\ 0}^n\ [\left \lfloor \frac{ax + b}{c} \right \rfloor\ >\ t]\cdot\ x^{k_1}\\=\ \sum_{t\ =\ 0}^{\left \lfloor \frac{an + b}{c} \right \rfloor\ -\ 1}\ ((t\ +\ 1)^{k_2}\ -\ t^{k_2})\ (\sum_{x\ =\ 0}^n\ x^{k_1}\ -\ \sum_{x\ =\ 0}^{\left \lfloor \frac{tc+c-b-1}{a} \right \rfloor}\ x^{k_1})\\=\ \left \lfloor \frac{an + b}{c} \right \rfloor^{k_2}\ \sum_{x\ =\ 0}^n\ x^{k_1}\ -\ \sum_{t\ =\ 0}^{\left \lfloor \frac{an + b}{c} \right \rfloor\ -\ 1}\ ((t\ +\ 1)^{k_2}\ -\ t^{k_2})\ \sum_{x\ =\ 0}^{\left \lfloor \frac{tc+c-b-1}{a} \right \rfloor}\ x^{k_1}\)。
令 \(\sum_{i\ =\ 0}^{k\ +\ 1}\ s_{k,i}\ n^i\ =\ \sum_{x\ =\ 0}^n\ x^k\)。
则 \(F(k_1,\ k_2,\ a,\ b,\ c,\ n)\ =\ \left \lfloor \frac{an + b}{c} \right \rfloor^{k_2}\ \sum_{x\ =\ 0}^n\ x^{k_1}\ -\ \sum_{i\ =\ 0}^{k_2\ -\ 1}(\sum_{t\ =\ 0}^{\left \lfloor \frac{an + b}{c} \right \rfloor\ -\ 1}\ \binom{k2}{i}\ t^i\cdot\ \sum_{x\ =\ 0}^{\left \lfloor \frac{tc+c-b-1}{a} \right \rfloor}\ x^{k_1})\\=\ \ \left \lfloor \frac{an + b}{c} \right \rfloor^{k_2}\ \sum_{x\ =\ 0}^n\ x^{k_1}\ -\ \sum_{i\ =\ 0}^{k_2\ -\ 1}(\sum_{t\ =\ 0}^{\left \lfloor \frac{an + b}{c} \right \rfloor\ -\ 1}\ \binom{k2}{i}\ t^i\cdot\ \sum_{j\ =\ 0}^{k_1\ +\ 1}\ s_{k,j}\ \left \lfloor \frac{tc+c-b-1}{a} \right \rfloor^j)\\=\ \ \ \left \lfloor \frac{an + b}{c} \right \rfloor^{k_2}\ \sum_{x\ =\ 0}^n\ x^{k_1}\ -\ \sum_{i\ =\ 0}^{k_2\ -\ 1}\ \binom{k2}{i}\ \sum_{j\ =\ 0}^{k_1\ +\ 1}\ s_{k,j}\ F(i,\ j,\ c,\ c\ -\ b\ -\ 1,\ a,\ \left \lfloor \frac{an + b}{c} \right \rfloor\ -\ 1))\)。
实现一个函数 \(G(a,\ b,\ c,\ n)\) 返回所有 \(F(i,\ j,\ a,\ b,\ c,\ n)\ (i\ +\ j\ \leq\ k_1\ +\ k_2)\) 即可。
代码
#include <bits/stdc++.h>
#ifdef __WIN32
#define LLFORMAT "I64"
#else
#define LLFORMAT "ll"
#endif
using namespace std;
constexpr int mod = 1e9 + 7, maxM = 10;
int main() {
constexpr auto pow_mod = [&](int x, int n) {
int y = 1;
while(n) {
if(n & 1) y = (long long) y * x % mod;
x = (long long) x * x % mod;
n >>= 1;
}
return y;
};
int m = maxM;
vector<vector<int> > s(m + 1);
auto gets = [&](int k) { // sum x^k = sum s[k][i]*x^i
vector<int> f(k + 2), p(1, 1);
for (int s = 0, x = 0; x <= k + 1; ++x) {
s = (pow_mod(x, k) + s) % mod;
f[x] = s;
vector<int> q(p.size() + 1, 0);
for (int i = 0; i < p.size(); ++i) q[i + 1] = p[i];
for (int i = 0; i < p.size(); ++i) q[i] = ((long long) q[i] - (long long) p[i] * x) % mod;
p = q;
}
s[k] = vector<int>(p.size() - 1, 0);
for (int x = 0; x <= k + 1; ++x) {
vector<int> q(p.size() - 1, 0);
q.back() = p.back();
for (int i = q.size() - 2; ~i; --i) q[i] = ((long long) q[i + 1] * x + p[i + 1]) % mod;
int coef = 1;
for (int y = 0; y <= k + 1; ++y) if(x != y) coef = (long long) coef * (x - y) % mod;
coef = (long long) pow_mod(coef, mod - 2) * f[x] % mod;
for (int i = 0; i < q.size(); ++i) s[k][i] = ((long long) q[i] * coef + s[k][i]) % mod;
}
};
for (int i = 0; i <= m; ++i) gets(i);
vector<vector<int> > binom(m + 1, vector<int>(m + 1, 0));
for (int i = 0; i <= m; ++i) for (int j = binom[i][0] = 1; j <= i; ++j) binom[i][j] = (binom[i - 1][j - 1] + binom[i - 1][j]) % mod;
auto S = [&](int n, int k) {
int ret = 0, x = 1;
for (int y: s[k]) ret = ((long long) y * x + ret) % mod, x = (long long) x * n % mod;
return ret;
};
function<vector<vector<int> >(int, int, int, int)> F = [&](int a, int b, int c, int n) {
vector<vector<int> > ret(m + 1);
for (int i = 0; i <= m; ++i) ret[i] = vector<int>(m - i + 1, 0);
if(n < 0) return ret;
if(a >= c || b >= c) {
int d = a % c, e = b % c, p = a / c, q = b / c;
auto foo = F(d, e, c, n);
for (int k1 = 0; k1 <= m; ++k1) for (int k2 = 0; k1 + k2 <= m; ++k2) {
int &t = ret[k1][k2];
for (int pwp = 1, i = 0; i <= k2; ++i) {
int nk2 = k2 - i;
for (int pwpq = pwp, j = 0; j <= nk2; ++j) {
int k = nk2 - j;
t = ((long long) pwpq * foo[k1 + i][k] % mod * binom[k2][i] % mod * binom[nk2][j] + t) % mod;
pwpq = (long long) pwpq * q % mod;
}
pwp = (long long) pwp * p % mod;
}
}
return ret;
}
int N = ((long long) a * n + b) / c;
auto foo = F(c, c - b - 1, a, N - 1);
for (int k1 = 0; k1 <= m; ++k1) for (int k2 = 0; k1 + k2 <= m; ++k2) {
int &t = ret[k1][k2];
t = (long long) pow_mod(N, k2) * S(n, k1) % mod;
for (int i = 0; i < k2; ++i) for (int j = 0; j <= k1 + 1; ++j) {
t = ((long long) t - (long long) foo[i][j] * s[k1][j] % mod * binom[k2][i]) % mod;
}
}
return ret;
};
auto solve = [&]() {
int n, a, b, c, K1, K2;
cin >> n >> a >> b >> c >> K1 >> K2;
m = K1 + K2;
auto ans = F(a, b, c, n);
cout << (ans[K1][K2] + mod) % mod << endl;
return;
};
int T;
cin >> T;
while(T--) solve();
return 0;
}