BZOJ3453: tyvj 1858 XLkxc(拉格朗日插值)
阿新 • • 發佈:2018-12-03
題意
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 */