Address
https://www.lydsy.com/JudgeOnline/problem.php?id=4558
Solution
先不考虑被删掉的点。
可以先枚举下图中的外正方形的边长从
1
1
到 :
易得,边长为
i
i
的外正方形有 个。
一个边长为
i
i
的外正方形内嵌(包括重叠)有 个正方形。
所以,
K=0
K
=
0
时答案为:
∑i=1min(n,m)(n−i+1)×(m−i+1)×i
∑
i
=
1
min
(
n
,
m
)
(
n
−
i
+
1
)
×
(
m
−
i
+
1
)
×
i
再考虑去掉包含 1 1 个、 个、 3 3 个、 个删掉的点的正方形。
2 2 个、 个、 4 4 个比较好计算。 枚举两个点 A,B A , B 后判断以 AB A B 为边或对角线的正方形即可。但一个正方形会被多对点枚举到,因此需要在统计完之后除以一个数,达到去重的目的。
而求包含 1 1 个点的正方形,可以考虑求出包含至少 个点的正方形个数,
然后减去(包含 2 2 个点的正方形 )再减去(包含 3 3 个点的正方形 )再减去(包含 4 4 个点的正方形 )
至于为什么要乘以 2,3,4 2 , 3 , 4 , 会在后面提到。
先枚举这一个点。先求出与 x 轴和 y 轴平行的正方形,然后考虑下面一张图:
![这里写图片描述](https://i-blog.csdnimg.cn/blog_migrate/b847d55fc872f71b166694532c26a104.png)
这里的 x,y∈N+ x , y ∈ N + 满足三个不等式:
⎧⎩⎨x≤ly≤rx+y≤u
{
x
≤
l
y
≤
r
x
+
y
≤
u
分类讨论即可得出解的组数。
看到这个算法过程,就可以得出:一个包含 x x 个删去点的正方形,会被这 个点各统计一次,共 x x 次。这就是为什么减去包含 个点的正方形时要乘以 2,3,4 2 , 3 , 4 。
ps :要写个 hash 表 / map 判断一个点是否被删。
Code
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define For(i, a, b) for (i = a; i <= b; i++)
using namespace std;
inline int read() {
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
typedef long long ll; const int N = 2005, PYZ = 1e8 + 7, ZZQ = 999997;
int n, m, K, X[N], Y[N], ans, c[5], ecnt, nxt[N], adj[ZZQ]; ll go[N];
ll which(int x, int y) {return 1ll * x * (m + 1) + y;}
void add_edge(int x, int y) {
ll v = which(x, y); int u = v % ZZQ;
nxt[++ecnt] = adj[u]; adj[u] = ecnt; go[ecnt] = v;
}
bool vis(int x, int y) {
ll p = which(x, y); int orz = p % ZZQ;
for (int e = adj[orz]; e; e = nxt[e])
if (go[e] == p) return 1; return 0;
}
int LP(int a, int b, int c) {
if (a + b <= c) return 1ll * a * b % PYZ;
if (c > max(a, b))
return (1ll * a * b - 1ll * (a + b - c) * (a + b - c + 1) / 2) % PYZ;
if (c <= min(a, b) + 1) return 1ll * c * (c - 1) / 2 % PYZ;
if (c < 2) return 0;
return 1ll * min(a, b) * (c - min(a, b) + c - 1) / 2 % PYZ;
}
int main() {
int i, j; n = read(); m = read(); K = read();
For (i, 1, K) X[i] = read(), Y[i] = read(), add_edge(X[i], Y[i]);
For (i, 1, min(n, m))
ans = (ans + 1ll * (n - i + 1) * (m - i + 1) % PYZ * i % PYZ) % PYZ;
For (i, 1, K) For (j, i + 1, K) {
int dx = X[i] - X[j], dy = Y[i] - Y[j];
int ax = X[i] - dy, ay = Y[i] + dx, bx = X[j] - dy, by = Y[j] + dx;
if (ax >= 0 && ax <= n && ay >= 0 && ay <= m
&& bx >= 0 && bx <= n && by >= 0 && by <= m)
c[2 + vis(ax, ay) + vis(bx, by)]++;
ax = X[i] + dy; ay = Y[i] - dx; bx = X[j] + dy; by = Y[j] - dx;
if (ax >= 0 && ax <= n && ay >= 0 && ay <= m
&& bx >= 0 && bx <= n && by >= 0 && by <= m)
c[2 + vis(ax, ay) + vis(bx, by)]++;
ax = X[i] + X[j] + Y[i] - Y[j]; ay = Y[i] + Y[j] - X[i] + X[j];
bx = X[i] + X[j] - Y[i] + Y[j]; by = Y[i] + Y[j] + X[i] - X[j];
if ((ax & 1) || (ay & 1) || (bx & 1) || (by & 1)) continue;
ax >>= 1; ay >>= 1; bx >>= 1; by >>= 1;
if (ax >= 0 && ax <= n && ay >= 0 && ay <= m
&& bx >= 0 && bx <= n && by >= 0 && by <= m
&& !vis(ax, ay) && !vis(bx, by)) c[2]++;
}
c[3] /= 2; c[4] /= 4; c[1] = (-c[2] * 2 - c[3] * 3 - c[4] * 4 + PYZ) % PYZ;
For (i, 1, K) {
int l = Y[i], r = m - Y[i], u = X[i], d = n - X[i];
c[1] = (c[1] + min(l, u)) % PYZ; c[1] = (c[1] + min(l, d)) % PYZ;
c[1] = (c[1] + min(r, d)) % PYZ; c[1] = (c[1] + min(r, u)) % PYZ;
c[1] = (c[1] + LP(l, r, u) + LP(l, r, d) +
LP(u, d, l) + LP(u, d, r)) % PYZ;
}
For (i, 1, 4) ans = (ans - c[i] + PYZ) % PYZ; cout << ans << endl; return 0;
}