UVA 12508 - Triangles in the Grid(计数问题)
12508 - Triangles in the Grid
题意:给定一个n∗m格子的矩阵,然后给定A,B。问能找到几个面积在A到B之间的三角形。
思路:枚举每一个子矩阵,然后求[0,A]的个数减去[0,B]的个数就是答案,然后对于每一个子矩阵个数非常好求为(n−r+1)∗(m−c+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(计数问题)的更多相关文章
- uva 12508 - Triangles in the Grid(几何+计数)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/35244875 题目链接:uva 12508 ...
- UVA 12651 Triangles
You will be given N points on a circle. You must write a program to determine how many distinctequil ...
- UVA 12075 - Counting Triangles(容斥原理计数)
题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...
- uva 10671 - Grid Speed(dp)
题目链接:uva 10671 - Grid Speed 题目大意:给出N,表示在一个N*N的网格中,每段路长L,如今给出h,v的限制速度,以及起始位置sx,sy,终止位置ex,ey,时间范围st,et ...
- Cheerleaders UVA - 11806 计数问题
In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...
- uva 11605 - Lights inside a 3d Grid(概率)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=2652" style=""& ...
- UVA 12382 Grid of Lamps 贪心
题目链接: C - Grid of Lamps Time Limit:1000MSMemory Limit: 0KB 问题描述 We have a grid of lamps. Some of the ...
- UVA 11916 Emoogle Grid(同余模)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- uva 11529 - Strange Tax Calculation(计数问题)
题目链接:uva 11529 - Strange Tax Calculation 题目大意:给出若干个点,保证随意三点不共线.随意选三个点作为三角行,其它点若又在该三角形内,则算是该三角形内部的点.问 ...
随机推荐
- node07---post请求、表单提交、文件上传
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- hdoj--5569--matrix(动态规划)
matrix Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- User-defined types
We have used many of Python’s built-in types; now we are going to define a new type. As an example, ...
- BZOJ 4269 高斯消元求线性基
思路: 最大: 所有线性基异或一下 次大: 最大的异或一下最小的线性基 搞定~ //By SiriusRen #include <cstdio> #include <algorith ...
- AngularJs轻松入门(九)与服务器交互
AngularJs从Web服务器请求资源都是通过Ajax来完成,所有的操作封装在$http服务中,$http服务是只能接收一个参数的函数,这个参数是一个对象,用来完成HTTP请求的一些配置,函数返回一 ...
- 使用Java操作Redis(一)
Redis是一款基于key-value的数据库服务器,安装完成后我们可以通过redis-cli使用Redis提供的命令完成各种操作.redis-cli实际上就是一款客户端,和redis-server建 ...
- springMVC中跳转问题
在使用SpringMVC时遇到了这个跳转的问题很头疼.现在总结出来,对以后的开发有所帮助. . 1.可以采用ModelAndView: @RequestMapping("test1" ...
- SQL_触发器学习
--触发器学习-------------------------------------------------------------------------------after 触发器----- ...
- uname---用于打印当前系统相关信息
uname命令用于打印当前系统相关信息(内核版本号.硬件架构.主机名称和操作系统类型等). 语法 uname(选项) 选项 -a或--all:显示全部的信息: -m或--machine:显示电脑类型: ...
- C#调用C/C++动态库,封装各种复杂结构体
C#调用C/C++动态库,封装各种复杂结构体. 标签: c++结构内存typedefc# 2014-07-05 12:10 6571人阅读 评论(1) 收藏 举报 分类: C(8) C#(6) ...