题目链接:uva 12508 - Triangles in the Grid
题目大意:给出n,m,A和B,要求计算在(n+1)?(m+1)的矩阵上,可以找出多少个三角形,面积在AB之间。
解题思路;首先枚举矩阵,然后计算有多少个三角形以该矩阵为外接矩阵,并且要满足体积在AB之间。然后对于每个矩阵,要确定在大的范围内可以确定几个。
枚举矩阵的内接三角形可以分为三类:
1.三角型的两点在一条矩阵边上的顶点,另一点在该边的对边上(不包括顶点)
2.以对角线为三角形的一边
3.三角形一点在矩形顶点上,另外两点在对应的边上
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline ll max(ll a, ll b) {
return a > b ? a : b;
}
inline ll min(ll a, ll b) {
return a < b ? a : b;
}
ll N, M, A, B;
ll solve (ll k) {
if (k < 0)
k = 0;
if (N > M)
swap(N, M);
ll ans = 0;
for (ll n = 1; n <= N; n++) {
for (ll m = 1; m <= M; m++) {
ll cnt = 0;
if (n * m <= k)
cnt += 2 * (n + m - 2);
ll l, r;
for (ll x = 0; x <= n; x ++) {
r = (m * x + k) / n;
if (r > m)
r = m;
ll t = m * x - k;
if(t <= 0)
l = 0;
else
l = (t - 1) / n + 1;
if(l <= r)
cnt += 2 * (r - l + 1);
}
for (ll x = 1; x < n; x++) {
ll tmp = n * m - x;
if (tmp <= k)
cnt += 4 * (m - 1);
else {
tmp = tmp - k;
ll u = m-1 - min(tmp / x + (tmp % x != 0), m-1);
cnt += 4 * u;
}
}
ans += cnt * (N - n + 1) * (M - m + 1);
}
}
return ans;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%lld%lld%lld%lld", &N, &M, &A, &B);
printf("%lld\n", solve(B*2) - solve(A*2-1));
}
return 0;
}
uva 12508 - Triangles in the Grid(几何+计数),布布扣,bubuko.com
uva 12508 - Triangles in the Grid(几何+计数)
原文:http://blog.csdn.net/keshuai19940722/article/details/35244875