BZOJ2655: calc(dp Lagrange interpolation)

Keywords: C++

meaning of the title

Title Link

Sol

It's not hard to think of a dp first

Set \ (f[i][j] \) to indicate that \ (I \) strictly increasing schemes with the maximum number of \ (j \) are selected

When transferring, judge whether the last position is \ (j \)

\[f[i][j] = f[i][j - 1] + f[i - 1][j - 1] * j\]

for(int i = 0; i <= A; i++) f[0][i] = 1;
for(int i = 1; i <= N; i++)
    for(int j = 1; j <= A; j++) 
        f[i][j] = add(f[i][j - 1], mul(f[i - 1][j - 1], j));
cout << mul(f[N][A], fac[N]);

It's still hard to find out. Take the transfer apart

\(f[i][j] = \sum_{k = 0}^{j - 1} f[i - 1][k] * (k + 1)\)

This transfer is very interesting

If we regard \ (i \) as a column and \ (k \) as a row, then in fact, when we transfer, we first multiply a coefficient \ (k \) on the \ (k \) row and then sum it

If we regard the \ (i - 1 \) column as a \ (t \) degree polynomial, it is obvious that the \ (i \) column is a \ (t+2 \) degree polynomial (sum once, multiply once)

In this way, the \ (i \) column is a polynomial of the highest \ (2i+1 \) degree

Just plug in

// luogu-judger-enable-o2
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 10001;
int A, N, Lim, mod, f[501][MAXN], fac[MAXN], y[MAXN];
int add(int x, int y) {
    if(x + y < 0) return x + y + mod;
    return x + y >= mod ? x + y - mod : x + y;
}
void 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 *y, int k) {
    static int x[MAXN], ans = 0;
    for(int i = 1; i <= Lim; i++) x[i] = i;
    for(int i = 0; i <= Lim; i++) {
        int up = y[i], down = 1;
        for(int j = 0; j <= Lim; j++) {
            if(i == j) continue;
            up = mul(up, add(k, -x[j]));
            down = mul(down, add(x[i], -x[j]));
        }
        add2(ans, mul(up, fp(down, mod - 2)));
    }
    return ans;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("a.in", "r", stdin);
   // freopen("a.out", "w", stdout);
#endif
    cin >> A >> N >> mod; Lim = 2 * N + 1;
    fac[0] = 1; for(int i = 1; i <= N; i++) fac[i] = mul(i, fac[i - 1]);
    for(int i = 0; i <= Lim; i++) f[0][i] = 1;
    for(int i = 1; i <= N; i++) {
        for(int j = 1; j <= Lim; j++) {
            f[i][j] = add(f[i][j - 1], mul(f[i - 1][j - 1], j));
        }
    }
    for(int i = 0; i <= Lim; i++) y[i] = f[N][i];
    cout << mul(Large(y, A), fac[N]);
    return 0;
}

Posted by Alzir on Sat, 07 Dec 2019 12:02:55 -0800