欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

BZOJ3453: tyvj 1858 XLkxc(拉格朗日插值)

程序员文章站 2022-05-30 21:59:12
题意 "题目链接" 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$多项式,直接差值 式 ......

题意

题目链接

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
*/