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

洛谷P4593 [TJOI2018]教科书般的*(拉格朗日插值)

程序员文章站 2022-07-09 20:32:38
题意 "题目链接" Sol 打出暴力不难发现时间复杂度的瓶颈在于求$\sum_{i = 1}^n i^k$ 老祖宗告诉我们,这东西是个$k$次多项式,插一插就行了 cpp // luogu judger enable o2 include using namespace std; const int ......

题意

题目链接

sol

打出暴力不难发现时间复杂度的瓶颈在于求\(\sum_{i = 1}^n i^k\)

老祖宗告诉我们,这东西是个\(k\)次多项式,插一插就行了

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
const int maxn = 103, mod = 1e9 + 7;
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 add(int x, int y) {
    if(x + y < 0) return x + y + mod;
    return 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 n, m, k, f[maxn][maxn], c[maxn][maxn], u[maxn], r[maxn], g[maxn];
int get(int u, int r) {
    memset(g, 0, sizeof(g));
    for(int i = 1; i <= maxn - 1; i++) 
        for(int k = 1; k <= i; k++) 
            g[i] = add(g[i], mul(fp(k, n - r), fp(i - k, r - 1)));
    int ans = 0;
    for(int i = 1; i <= maxn - 1; i++) {
        int up = 1, down = 1;
        for(int j = 1; j <= maxn - 1; j++) {
            if(i == j) continue;
            up = mul(up, add(u, -j));
            down = mul(down, add(i, -j));
        }   
        ans = add(ans, mul(g[i], mul(up, fp(down, mod - 2))));
    }
    return ans;
}
int main() {
    //freopen("a.in", "r", stdin);
    n = read(); m = read(); k = read();
    for(int i = 0; i <= n; i++) {
        c[i][0] = c[i][i] = 1;
        for(int j = 1; j < i; j++) c[i][j] = add(c[i - 1][j - 1], c[i - 1][j]);
    }
    for(int i = 1; i <= m; i++) u[i] = read();
    for(int i = 1; i <= m; i++) r[i] = read();
    f[0][n - 1] = 1;
    for(int i = 1; i <= m; i++) {
        int t = get(u[i], r[i]);
        for(int j = k; j <= n; j++) {
            for(int k = j; k <= n - 1; k++) 
                if(k - j <= r[i] - 1) f[i][j] = add(f[i][j], mul(mul(f[i - 1][k], c[k][k - j]), c[n - 1 - k][r[i] - 1 - (k - j)]));
            f[i][j] = mul(f[i][j], t);
        }
    }
    printf("%d", f[m][k]);
    return 0;
}
/*
100 3 50
500 500 456
13 46 45
*/