ZOJ 3647 Gao the Grid(居然是暴力)

A n * m grid as follow:

Count the number of triangles, three of whose vertice must be grid-points.

Note that the three vertice of the triangle must not be in a line(the right picture is not a triangle).


Input

The input consists of several cases. Each case consists of two positive integers
n and m (1 ≤ n, m ≤ 1000).

Output

For each case, output the total number of triangle.

Sample Input

1 1
2 2

Sample Output

4
76

hint

hint for 2nd case: C(9, 3) - 8 = 76

题目意思 : 给你矩形长度,求在里面取3个点,问可以组成三角形的个数?

一共有(n+1)*(m+1)个点,去3个

然后减去同行取3个,同列取3个;

最后减去左斜和右斜的,这种情况居然是枚举三角形的长高!!!!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>

#define eps 1e-8
using namespace std;
#define N 10005

typedef long long ll;

ll L(ll x)
{
	return (ll)(x*(x-1)*(x-2)/6);
}

ll gcd(ll n,ll m)
{
	if(m==0)  return n;
	return gcd(m,n%m);
}

int main()
{
	ll i,j,n,m;
	while(~scanf("%lld%lld",&n,&m))
	{   n++;
	    m++;
		ll ans=L(n*m)-L(n)*m-L(m)*n;
		for(i=2;i<n;i++)
			for(j=2;j<m;j++)
		{
			ll temp=gcd(i,j)-1;
			temp=temp*(n-i)*(m-j)*2;
			ans-=temp;
 		}
       printf("%lld\n",ans);
	}
    return 0;
}
时间: 2024-08-26 05:14:40

ZOJ 3647 Gao the Grid(居然是暴力)的相关文章

ZOJ 3647 Gao the Grid dp,思路,格中取同一行的三点,经典 难度:3

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4837 三角形的总数=格子中任取3个点的组合数-同一横行任取3个点数目-同一纵行任取3个点数目-同一斜直线上任取3个点数目 同一横行和同一纵行都好求 同一斜行的距离最远的点必然是一个矩形的两个端点,设矩形的长宽为l,w,中间可以取的点数则是(gcd(l,w)-1),左上角或左下角的起点反而不重要. 能够取到该矩形的可能是 (n-l+1)*(m-w+1),因为左上角作为起点或左下

zoj 3647 Gao the Grid

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4837 先求出从所有点随机找出三个点的组合数,然后去掉共线的,平行好去掉,斜线就需要枚举矩形,也就是枚举两个端点形成向量. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 1000 5 #define ll long long 6 using

ZOJ 3781 Paint the Grid Reloaded (最短路)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意: 在n*m矩阵的图定义连通区域为x值或y值相同且颜色相同的连通,连通具有传递性 每次可以把一个连通区域颜色反转(O变X,X变O) 问把所有块的颜色变为X最小的步数 方法: 很巧妙的最短路问题,先建图,然后以每个顶点为起点,找单源最短路的最大的值(也就是最深的深度),然后这个值取min 建图:相邻的块连边权1的边(即:通过1次反转可以使得两个连通块变为一个连通块

zoj 3781 Paint the Grid Reloaded (比较隐含的最短路)

Paint the Grid Reloaded Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N rows and M columns. All cells are painted with either black or white initially. Two cells A and B are called connected if they share an edge and they are

zoj 3780 Paint the Grid Again (拓扑排序)

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or an

ZOJ 3780 Paint the Grid Again(topsort)

ZOJ Problem Set - 3780 Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row

ZOJ 3780 Paint the Grid Again(隐式图拓扑排序)

Paint the Grid Again Time Limit: 2 Seconds      Memory Limit: 65536 KB Leo has a grid with N × N cells. He wants to paint each cell with a specific color (either black or white). Leo has a magical brush which can paint any row with black color, or an

HDU 5073 Galaxy(居然是暴力)

Problem Description Good news for us: to release the financial pressure, the government started selling galaxies and we can buy them from now on! The first one who bought a galaxy was Tianming Yun and he gave it to Xin Cheng as a present. To be fashi

Zoj 3616 Choir III 【有想法的暴力】【容斥】

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3616 题目大意:给出n, m, b, g (0 < n <= 100, 0 < m <= 2000, 0 < b, g <= m * n)这些数字,下面n行,每一行有2*m个数字,第一个数字表示一个同学对于合唱的贡献,第二个数字表示同学的性别,b和g表示在一个矩形内至少有的男生和女生的数量.求一个矩形使得这个矩形内的对合唱的贡献值最大并