题意

题目链接

Sol

把式子拆开,就是求这个东西

\[\sum_{i = 0} ^n \sum_{j = 1}^{a + id} \sum_{x =1}^j x^k \pmod P
\]

那么设\(f(x) = \sum_{i = 1}^n i^k\),这是个经典的\(k + 1\)多项式,直接差值

式子就可以化成

\[\sum_{i = 0} ^n \sum_{j = 1}^{a + id} f(j) \pmod P
\]

设\(g(x) = \sum_{i = 1}^n f(x)\)

对\(g\)差分之后实际上也就得到了\(f(x)\),根据多项式的定义,\(g(x)\)是个\(k+2\)次多项式。

同理我们要求的就是个\(k+3\)次多项式

直接暴力插值就行了

时间复杂度:\(O(Tk^3)\)

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1234567891, MAXN = 127;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int T, K, a, N, d, f[MAXN], g[MAXN], x[MAXN];
int add(int x, int y) {
if(x + y < 0) return x + y + mod;
return x + y >= mod ? x + y - mod : x + y;
}
int add2(int &x, int y) {
if(x + y < 0) x = (x + y + mod);
else x = (x + y >= mod ? x + y - mod : x + y);
}
int mul(int x, int y) {
return 1ll * x * y % mod;
}
int fp(int a, int p) {
int base = 1;
while(p) {
if(p & 1) base = mul(base, a);
a = mul(a, a); p >>= 1;
}
return base;
}
int Large(int *a, int k, int N) {
for(int i = 0; i <= k; i++) x[i] = i;
int ans = 0;
for(int i = 0; i <= k; i++) {
int up = a[i], down = 1;
for(int j = 0; j <= k; j++) {
if(i == j) continue;
up = mul(up, add(N, -x[j]));
down = mul(down, add(x[i], -x[j]));
}
add2(ans, mul(up, fp(down, mod - 2)));
}
return ans;
}
signed main() {
#ifndef ONLINE_JUDGE
//freopen("a.in", "r", stdin);freopen("a.out", "w", stdout);
#endif
T = read();
while(T--) {
K = read(), a = read(), N = read(), d = read();
memset(f, 0, sizeof(f)); memset(g, 0, sizeof(g));
/*
for(int i = 1; i <= K + 4; i++) f[i] = add(f[i - 1], fp(i, K));
for(int i = 1; i <= K + 4; i++) g[i] = add(g[i - 1], Large(f, K + 4, a + i * d));//ֱ直接这样写是错的
for(int i = 1; i <= K + 4; i++) f[i] = add(f[i - 1], Large(g, K + 4, i));
printf("%d\n", Large(g, K + 4, N));
*/
for(int i = 1; i <= K + 4; i++) f[i] = add(f[i - 1], fp(i, K));
for(int i = 1; i <= K + 4; i++) f[i] = add(f[i], f[i - 1]);
for(int i = 0; i <= K + 4; i++) g[i] = add(i > 0 ? g[i - 1] : 0, Large(f, K + 4, add(a, mul(i, d))));
printf("%lld\n", Large(g, K + 4, N));
}
return 0;
}
/*
5
123 123456789 456879 132
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
*/
04-26 16:44