跳到正文

题解

题解:AT_agc018_e [AGC018E] Sightseeing Plan

_Z_F_R_约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年6月10日
更新
2026年10月28日
篇幅
约 7 分钟

版本

  1. v1当前

枚举中心点 s,ts, t,那么两边相对独立。 考察式子:

∑p∈[X1,X2]∑q∈[Y1,Y2](s−p+t−qs−p)\sum_{p \in [X_1, X_2]} \sum_{q \in [Y_1, Y_2]} \binom{s - p + t - q}{s - p}

根据恒等式:

∑i≤n(im)=(n+1m+1)\sum_{i \le n} \binom{i}{m} = \binom{n + 1}{m + 1}

进行两次变换,化成四个形如 (s+t−At−B)\binom{s + t - A}{t - B} 的式子的线性组合。

对于另一边进行一样的操作,也可以化成四个形如 (C−s−tD−t)\binom{C - s - t}{D - t} 的式子的线性组合。

拆开,我们要算:

∑s∈[X3,X4]∑t∈[Y3,Y4](s+t−At−B)(C−s−tD−t)\sum_{s \in [X_3, X_4]} \sum_{t \in [Y_3, Y_4]} \binom{s + t - A}{t - B} \binom{C - s - t}{D - t}

对前面的限制容斥掉,拆成四个这样的式子:

∑s≤r1∑t≤r2(s+t−At−B)(C−s−tD−t)\sum_{s \le r_1} \sum_{t \le r_2} \binom{s + t - A}{t - B} \binom{C - s - t}{D - t}

令 s←s−A+B,t←t−Bs \gets s - A + B, t \gets t - B,那么就形如:

∑s≤r1∑t≤r2(s+tt)(P+Q−s−tQ−t)\sum_{s \le r_1} \sum_{t \le r_2} \binom{s + t}{t} \binom{P + Q - s - t}{Q - t}

你相当于,从 (0,0)(0, 0) 走到 (P,Q)(P, Q),贡献是:路径与 [0,r1]×[0,r2][0, r_1] \times [0, r_2] 这个矩形的交中的整点个数。枚举从哪个边界上的点离开即可。

复杂度 O(V)O(V),带恐怖的 6464 倍常数。

代码
C++
// Think TWICE, code ONCE
#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline ll Read() {
    int sig = 1; ll num = 0; char c = getchar();
    while(!isdigit(c)) { if(c == '-') sig = -1; c = getchar(); }
    while(isdigit(c)) num = (num << 3) + (num << 1) + (c ^ 48), c = getchar();
    return num * sig;
}
void Write(ll x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) Write(x / 10);
    putchar((x % 10) ^ 48);
}
const int N = 1000005;
const ll Mod = 1e9 + 7;
ll fact[N << 1], ifact[N << 1];
ll QuickPow(ll x, ll y, ll Mod) {
    ll res = 1ll;
    while(y) {
        if(y & 1) res = res * x % Mod;
        x = x * x % Mod, y >>= 1;
    }
    return res;
}
void Init_fact(int n) {
    int i; fact[0] = ifact[0] = 1; for(i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % Mod;
    ifact[n] = QuickPow(fact[n], Mod - 2, Mod); for(i = n - 1; i; i--) ifact[i] = ifact[i + 1] * (i + 1) % Mod;
}
ll Binom(int x, int y) { if(x < y || y < 0) return 0ll; return fact[x] * ifact[y] % Mod * ifact[x - y] % Mod; }
ll Calc(int x, int y, int p, int q) {
    if(p < 0 || q < 0 || x < 0 || y < 0) return 0ll;   
    if(x <= p && y <= q) return Binom(x + y, x) * (x + y) % Mod;
    int i; ll res = 0;
    for(i = 0; i <= p; i++) res = (res + Binom(x - i + y - q - 1, x - i) * Binom(i + q, i) % Mod * (i + q)) % Mod;
    for(i = 0; i <= q; i++) res = (res + Binom(x - p - 1 + y - i, x - p - 1) * Binom(p + i, p) % Mod * (p + i)) % Mod;
    return res;
}
int x[7], y[7];
int main() {
    int i, j, k, l, p, q; Init_fact(2000000);
    for(i = 1; i <= 6; i++) x[i] = Read();
    for(i = 1; i <= 6; i++) y[i] = Read();
    int A = 0, B = 0, C = 0, D = 0, E, F, coef = 1; ll ans = 0;
    for(i = 0; i < 2; i++) {
        B += i ? y[1] - 1 : y[2], coef *= i ? 1 : -1;
        for(j = 0; j < 2; j++) {
            A += j ? x[1] - 1 : x[2], coef *= j ? 1 : -1;
            for(k = 0; k < 2; k++) {
                D += k ? y[6] + 1 : y[5], coef *= k ? 1 : -1;
                for(l = 0; l < 2; l++) {
                    C += l ? x[6] + 1 : x[5], coef *= l ? 1 : -1;
                    for(p = 0; p < 2; p++) {
                        E = p ? x[4] : x[3] - 1, coef *= p ? 1 : -1;
                        for(q = 0; q < 2; q++) {
                            F = q ? y[4] : y[3] - 1, coef *= q ? 1 : -1;
                            ans = (ans + coef * Calc(C - A, D - B, E - A, F - B) + Mod) % Mod;
                            coef *= q ? 1 : -1;
                        }
                        coef *= p ? 1 : -1;
                    }
                    C -= l ? x[6] + 1 : x[5], coef *= l ? 1 : -1;
                }
                D -= k ? y[6] + 1 : y[5], coef *= k ? 1 : -1; 
            }
            A -= j ? x[1] - 1 : x[2], coef *= j ? 1 : -1;
        }
        B -= i ? y[1] - 1 : y[2], coef *= i ? 1 : -1;
    }
    Write(ans);
}
原文