首页 > 代码库 > ZOJ 3647 Gao the Grid(居然是暴力)

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

A n * m grid as follow:

a n*m grid(n=4,m=3)

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).

a trianglenot a triangle

Input

The input consists of several cases. Each case consists of two positive integersn 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;
}






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