Petrozavodsk Winter Camp, Day 8, 2014, Rectangle Count

时间:2023-03-08 17:03:05
Petrozavodsk Winter Camp, Day 8, 2014, Rectangle Count

给一个n*m的格点图,问其中有多少个矩形?

$ \sum_{x=1}^{nm} \sum_{ab=x} [a + b \leq n](n - a - b + 1)\sum_{cd=x} [c + d \leq m](m - c - bd + 1)$

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long long ll;
#define rep(i, j, k) for (int i = int(j); i <= int(k); ++ i)
const int N = ;
LL f[N * N], g[N * N]; int main() {
LL n, m;
cin >> n >> m;
n --; m --;
rep(i, , n) rep(j, , n - i)
if (i * j < N * N) f[i * j] += (LL)(n - i - j + );
rep(i, , m) rep(j, , m - i)
if (i * j < N * N) g[i * j] += (LL)(m - i - j + );
LL ans = ;
rep(i, , N * N - ) {
// if (f[i] * g[i]) cout << i << '\n';
ans += f[i] * g[i];
}
ans += (n * (n + ) / ) * (m * (m + ) / );
cout << ans << '\n';
}