12508 - Triangles in the Grid

题目链接

题意:给定一个nm格子的矩阵,然后给定A,B。问能找到几个面积在A到B之间的三角形。

思路:枚举每一个子矩阵,然后求[0,A]的个数减去[0,B]的个数就是答案,然后对于每一个子矩阵个数非常好求为(nr+1)∗(mc+1)。

关键在于怎么求每一个子矩阵的符合个数。

想了好久,參考别人题解才想出来。分3种情况讨论:

1、一个点在矩形顶点。另外两点相应在顶点的另外两边上。

2、两个点在顶点上。另外一点在对边上。

3、三个点都在顶点上

然后分别去计算求和。详细的过程比較麻烦,在纸上多画画一边就能得到一个o(N)的方法。大概是枚举一个在竖直边上的位置,横的位置利用公式运算一步求解,这样时间复杂度是能够接受的

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int t;
long long n, m, a, b; long long solve(long long limit) {
long long ans = 0;
for (long long r = 1; r <= n; r++) {
for (long long c = 1; c <= m; c++) {
long long count = 0;
long long up, down;
if (r * c <= limit) count += 2 * (r - 1 + c - 1);
for (long long x = 0; x <= r; x++) {
up = min(c, (x * c + limit) / r);
long long tmp = x * c - limit;
if (tmp <= 0) down = 0;
else down = (tmp - 1) / r + 1;
if (down <= up) count += 2 * (up - down + 1);
}
for (long long x = 1; x < r; x++) {
long long s = (r * c - x);
if (s <= limit) count += 4 * (c - 1);
else count += 4 * ((c - 1) - min((s - limit) / x + ((s - limit) % x != 0), c - 1));
}
ans += count * (n - r + 1) * (m - c + 1);
}
}
return ans;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%lld%lld%lld%lld", &n, &m, &a, &b);
a <<= 1; b <<= 1; if (a == 0) a = 1;
printf("%lld\n", solve(b) - solve(a - 1));
}
return 0;
}

UVA 12508 - Triangles in the Grid(计数问题)的更多相关文章

  1. uva 12508 - Triangles in the Grid(几何+计数)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/35244875 题目链接:uva 12508 ...

  2. UVA 12651 Triangles

    You will be given N points on a circle. You must write a program to determine how many distinctequil ...

  3. UVA 12075 - Counting Triangles(容斥原理计数)

    题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...

  4. uva 10671 - Grid Speed(dp)

    题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,如今给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et ...

  5. Cheerleaders UVA - 11806 计数问题

    In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...

  6. uva 11605 - Lights inside a 3d Grid(概率)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=2652" style=""& ...

  7. UVA 12382 Grid of Lamps 贪心

    题目链接: C - Grid of Lamps Time Limit:1000MSMemory Limit: 0KB 问题描述 We have a grid of lamps. Some of the ...

  8. UVA 11916 Emoogle Grid(同余模)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. uva 11529 - Strange Tax Calculation(计数问题)

    题目链接:uva 11529 - Strange Tax Calculation 题目大意:给出若干个点,保证随意三点不共线.随意选三个点作为三角行,其它点若又在该三角形内,则算是该三角形内部的点.问 ...

随机推荐

  1. hdoj--2709--Sumsets(数位dp)

    Sumsets Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  2. java9新特性-6-多版本兼容jar包

    1.官方Feature 238: Multi-Release JAR Files 2.使用说明 当一个新版本的Java出现的时候,你的库用户要花费数年时间才会切换到这个新的版本.这就意味着库得去向后兼 ...

  3. Golang 在 Mac、Linux、Windows 下交叉编译

    Golang 支持在一个平台下生成另一个平台可执行程序的交叉编译功能. Mac下编译Linux, Windows平台的64位可执行程序: CGO_ENABLED= GOOS=linux GOARCH= ...

  4. 14个优秀 JS 前端框架、库、工具及其使用时机

    这篇文章主要描述现今流行的一些 Javascript web 前端框架,库以及它们的适用场景. 新的 Javascript 库层出不穷,从而Web 社区愈发活跃.多样.在多方面快速发展.详细去描述每一 ...

  5. UI Framework-1: Aura Layout Managers

    Layout Managers LayoutManager is a convenient abstraction that separates messy layout heuristics fro ...

  6. UI Framework-1: Aura Graphics Architecture

    Graphics Architecture Overview Each Aura Window owns a corresponding compositor layer. The layer tre ...

  7. WLAN 基础架构功能

    WLAN 基础架构功能 Android WLAN 框架可帮助用户连接到优质 WLAN 网络(在有可用 WLAN 网络且需要连接到这类网络的情况下).Android 可通过多种方式来实现这一点: 打开网 ...

  8. WebRTC Native APIs

    WebRTC Native APIs The WebRTC Native APIs implementation is based on W3C’s WebRTC 1.0: Real-time Com ...

  9. caioj 1069 动态规划入门(二维一边推2:顺序对齐)(最长公共子序列拓展总结)

    caioj 1068是最长公共子序列裸体,秒过, 就不写博客了 caioj 1069到1071 都是最长公共字序列的拓展,我总结出了一个模型,屡试不爽    (1) 字符串下标从1开始,因为0用来表示 ...

  10. 转载-- Qt Creator编译时make: arm-linux-g++: command not found 错误!

    前提是已经配置好交叉编译器,但是qt creator找不到. 解决方法: 修改 /usr/local/Trolltech/QtEmbedded-4.7.0-arm/mkspecs/qws/linux- ...