洛谷P4593 [TJOI2018]教科書般的褻瀆(拉格朗日插值)

自為風月馬前卒發表於2018-12-03

題意

題目連結

Sol

打出暴力不難發現時間複雜度的瓶頸在於求(sum_{i = 1}^n i^k)

老祖宗告訴我們,這東西是個(k)次多項式,插一插就行了

上面的是(O(Tk^2))

下面是(O(Tk^3))

// luogu-judger-enable-o2
#include<bits/stdc++.h>
#define LL long long 
using namespace std;
const int MAXN = 66, mod = 1e9 + 7;
inline LL read() {
    char c = getchar(); LL x = 0, f = 1;
    while(c < `0` || c > `9`) {if(c == `0`) f = -1; c = getchar();}
    while(c >= `0` && c <= `9`) x = x * 10 + c - `0`, c = getchar();
    return x * f;
}
LL N, M, a[MAXN], tot, inv[3601];
LL add(LL x, LL y) {
    if(x + y < 0) return x + y + mod;
    return x + y >= mod ? x + y - mod : x + y;
}
void add2(LL &x, LL y) {
    if(x + y < 0) x = x + y + mod;
    else x = (x + y >= mod ? x + y - mod : x + y);
}
LL mul(LL x, LL y) {
    return 1ll * x * y % mod;
}
LL fp(LL a, LL p) {
    LL base = 1;
    while(p) {
        if(p & 1) base = mul(base, a);
        a = mul(a, a); p >>= 1;
    }
    return base;
}
LL x[MAXN], y[MAXN], fac[MAXN], ifac[MAXN], pre[MAXN], suf[MAXN];
LL get(LL N, LL M) {// sum_{i=1}^n i^m
    //printf("%d %d
", N, M);
    LL Lim = M + 1, ans = 0; memset(y, 0, sizeof(y));
    for(int i = 1; i <= Lim; i++) add2(y[i], add(y[i - 1], fp(i, M)));
    pre[0] = N; suf[Lim + 1] = 1; 
    for(int i = 1; i <= Lim; i++) pre[i] = mul(pre[i - 1], add(N, -i));
    for(int i = Lim; i >= 1; i--) suf[i] = mul(suf[i + 1], add(N, -i));
    for(int i = 0; i <= Lim; i++) {
        LL up = mul(y[i], mul(pre[i - 1], suf[i + 1])),
           down = mul(ifac[i], ifac[Lim - i]);
        if((Lim - i) & 1) down = mod - down;
        //printf("%d %d
", up, down);
        ans = add(ans, mul(up, down));
    }
    return ans;
}
void solve() {
    N = read(); M = read();
    memset(a, 0, sizeof(a));
    LL ans = 0;
    for(LL i = 1; i <= M; i++) a[i] = read(); a[++M] = ++N;
    sort(a + 1, a + M + 1);
    for(LL i = 1; i <= M; i++) {
        for(LL j = i; j <= M; j++) ans = add(ans, add(get(a[j] - 1, M ), -get(a[j - 1], M)));
        for(LL j = i + 1; j <= M; j++) a[j] = add(a[j], -a[i]); a[i] = 0;
    }
    printf("%lld
", ans);
}   
int main() {
    inv[1] = 1; for(int i = 2; i <= 3600; i++) inv[i] = mul((mod - mod / i), inv[mod % i]);
    fac[0] = 1; for(int i = 1; i <= 60; i++) fac[i] = mul(i, fac[i - 1]);
    ifac[60] = fp(fac[60], mod - 2);
    for(int i = 60; i >= 1; i--) ifac[i - 1] = mul(ifac[i], i);
    //cout << get(10, 2) << endl;
    for(LL T = read();T--; solve());
    return 0;
}
/*
1
1044536146 2
883276404
640705454

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

相關文章