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).
The input consists of several cases. Each case consists of two positive integers n and m (1 ≤ n, m ≤ 1000).
For each case, output the total number of triangle.
1 1 2 2
4 76
hint for 2nd case: C(9, 3) - 8 = 76
題目意思 : 給你矩形長度,求在裡面取3個點,問可以組成三角形的個數?
一共有(n+1)*(m+1)個點,去3個
然後減去同行取3個,同列取3個;
最後減去左斜和右斜的,這種情況居然是枚舉三角形的長高!!!!!
#include#include #include #include #include #include #include #include #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