[JZOJ6403] [11.04] a simulation NOIP2019

Subject to the effect

From \ ((0,0,0) \) come \ ((n-, n-, n-) \) , each step from \ ((x, y, z ) \) come \ ((x + 1, y, z) \) or \ ((x, y + 1 , z) \) or \ ((X, Y, Z +. 1) \) , gives \ (m \) number of points allowed through, seek the number of such programs go.
\ (n \ leq 100000, m \ leq 5000 \)

Solution

Classic title.

Enumeration \ (I \) points and then must pass through the inclusion and exclusion, the complexity of the \ (O (m!) \) .

Consider this process onto the recursion set \ (F_i \) represents the \ ((0,0,0) \) reach the first \ (I \) point, without passing through other points of the program number; note \ (j \ to i \) represents \ (J \) can reach \ (I \) , \ (G (A, B, C) \) represents the \ ((0,0,0) \) in any manner go \ ((a, b, c ) \) of the program number, has:
\ [F_i = \ sum_ {J \ I} -f_j to G * (x_j-x_i, y_i-y_j, z_i-z_j) \]
how to push? \ (f_j \) in all paths through \ (i \) after all one more point, so inclusion and exclusion must be multiplied by the coefficient \ (--1 \) .

This done \ (O (m ^ 2) \) .

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long ll;
const int N = 300017, M = 5017;
const ll P = 1000000007;

int n, m, tot;
ll fac[N], inv[N], f[M];
struct note {
    int x, y, z;
} a[M];
int cmp(note p, note q) {
    return p.x == q.x ? (p.y == q.y ? p.z < q.z : p.y < q.y) : p.x < q.x;
}
int operator==(note p, note q) { return p.x == q.x && p.y == q.y && p.z == q.z; }

void pre() {
    fac[0] = 1;
    for (int i = 1; i <= 300000; ++i) fac[i] = fac[i - 1] * i % P;
    inv[0] = inv[1] = 1;
    for (int i = 2; i <= 300000; ++i) inv[i] = inv[P % i] * (P - P / i) % P;
    for (int i = 2; i <= 300000; ++i) inv[i] = inv[i] * inv[i - 1] % P;
}
ll C(int n, int m) {
    return fac[n] * inv[m] % P * inv[n - m] % P;
}
ll calc(int dx, int dy, int dz) {
    return C(dx + dy + dz, dx) * C(dy + dz, dy) % P;
}

int main() {
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    pre();
    scanf("%d%d", &n, &m);
    a[++tot] = (note){0, 0, 0};
    for (int i = 1; i <= m; ++i) ++tot, scanf("%d%d%d", &a[tot].x, &a[tot].y, &a[tot].z);
    a[++tot] = (note){n, n, n};
    sort(a + 1, a + tot + 1, cmp);
    tot = unique(a + 1, a + tot + 1) - a - 1;
    f[1] = P - 1;
    for (int i = 2; i <= tot; ++i)
        for (int j = 1; j < i; ++j)
            if (a[j].x <= a[i].x && a[j].y <= a[i].y && a[j].z <= a[i].z)
                f[i] = (f[i] - calc(a[i].x - a[j].x, a[i].y - a[j].y, a[i].z - a[j].z) * f[j] % P + P) % P;
    printf("%lld\n", f[tot]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zjlcnblogs/p/11795265.html