UVA - 12075 Counting Triangles

Description

Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN
grid. For example in a (1x 2) grid there are 18 different lattice triangles as shown in the picture below:

Input

The input file contains at most 21 sets of inputs.

Each set of input consists of two integers M andN (
0 < M, N1000 ). These two integers denote that you have to count triangles in an
(MxN) grid.

Input is terminated by a case where the value of M andN are zero. This case should not be processed.

Output

For each set of input produce one line of output. This output contains the serial of output followed by the number lattice triangles in the(MxN) grid. You can assume that number of triangles will fit in
a 64-bit signed integer.

Sample Input

1 1
1 2
0 0

Sample Output

Case 1: 4
Case 2: 18
题意: 给你n*m的网格,问你能有几个三角形。
思路: 我们先计算任意三个点组成的可能,然后排除同一水平,同一垂直的,同一斜线的,前两个比较好计算,同一斜线的稍复杂。
同一斜线的和上一题UVA - 1393 Highways一样也是要用容斥原理,首先我们动手计算一下,可能发现每次多的是gcd(i, j)-1,然后再去重,dp[i][j]代表从左上角[0,0]
到这个点[i,j]并以这两个点为端点枚举三点共线的个数,最后还要递推一次,得到n*m的网格三点共线的个数,当然这也要*2,感觉怪怪的,瞎搞过的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
typedef long long ll;
using namespace std;
const int maxn = 1010;

ll dp[maxn][maxn], ans[maxn][maxn];
int n, m;

int gcd(int a, int b) {
	return b==0?a:gcd(b, a%b);
}

void init() {
	memset(dp, 0, sizeof(dp));
	memset(ans, 0, sizeof(ans));

	for (int i = 1; i < maxn; i++)
		for (int j = 1; j < maxn; j++)
			dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + gcd(i, j) - 1;

	for (int i = 1; i < maxn; i++)
		for (int j = 1; j < maxn; j++)
			ans[i][j] = ans[i-1][j] + ans[i][j-1] - ans[i-1][j-1] + dp[i][j];
}

ll cal(int x) {
	if (x < 3)
		return 0;
	return (ll) x * (x - 1) * (x - 2) / 6;
}

int main() {
	init();
	int cas = 1;
	while (scanf("%d%d", &n, &m) != EOF && n + m) {
		printf("Case %d: ", cas++);
		ll sum = cal((n+1)*(m+1)) - (m+1)*cal(n+1) - (n+1)*cal(m+1) - ans[n][m] * 2;
		printf("%lld\n", sum);
	}
	return 0;
}

UVA - 12075 Counting Triangles,布布扣,bubuko.com

时间: 2024-08-02 06:58:24

UVA - 12075 Counting Triangles的相关文章

uva 12075 - Counting Triangles(容斥原理)

题目链接:uva 12075 - Counting Triangles 题目大意:一个n?m的矩阵,求说有选任意三点,可以组成多少个三角形. 解题思路:任意选三点C(3(n+1)?(m+1))但是有些组合是不可行得,即为三点共线,除了水平和竖直上的组合,就是斜线上的了,dp[i][j]即为ij情况下的斜线三点共线. #include <cstdio> #include <cstring> typedef long long ll; const int N = 1005; ll dp

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

题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对于两点,求他们的gcd - 1,得到的就是他们之间有多少个点,那么情况数就可以求了,然后还是利用容斥原理去计数,然后累加出答案 代码: #include <stdio.h> #include <string.h> #include <algorithm> using nam

UVA 1393 Highways,UVA 12075 Counting Triangles —— (组合数,dp)

先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[i][j]代表在这个矩形中终点是到(i,j)这个点的满足题意的直线条数,那么,用dp的话就可以得出递推关系:由长和宽分别小1的左右两个矩形中满足题意的线的条数减去他们共有的矩形中满足的线的条数(容斥减去重复部分),之后还要判断从最左上角的点(1,1)到(i,j)是否可以组成一条线,这个条件是gcd(

Uva 12075 Counting Triangles(容斥)

/* 思路:1.先选三个点 2.去掉同行情况 3.去掉同列情况 4.枚举矩形大小去掉重复情况 */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue&g

uva 12075

12075 - Counting Triangles Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in an M x

hdu 1396 Counting Triangles (递推)

Counting Triangles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2012    Accepted Submission(s): 966 Problem Description Given an equilateral triangle with n the length of its side, program to

UVA - 10574 Counting Rectangles

Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3Seconds   Given n points on the XY plane, count how many regular rectanglesare formed. A rectangle is regular if and only if its sides are all paralle

uva 1436 - Counting heaps(计数)

题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,现在为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一只的uva11174,最后问题只和树德结构有直接关系,f(root)=(s(root)?1)!(s(1)?s(2)???s(n) 但是给定的取模数不是质数,所以不能用逆元做,只能将分子分母分别拆分成质因子,然后对质因子进制约分.因为最后的答案一定是正整数,所以对于每个质因子,分子分解出的因子个数一定大于

LightOJ 1307 Counting Triangles 二分查找

[题解整理]二分题 题目类型: 二分查找: 二分答案. 大致解题思路: 查找注意有序和返回值: 浮点数注意精度: 整数注意返回值,建议另外维护一个变量,用于储存可行解. 题目 分类 传送门 WA点 poj 2785 二分查找 题解 lightoj 1088 二分查找 题解 lightoj 1307 二分查找 题解 longlong poj 2456 整数二分答案 题解 poj 3104 整数二分答案 题解 poj 3258 整数二分答案 题解 poj 3273 整数二分答案 题解 lightoj