题目链接 : ZOJ3647
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
Author: WU, Yingxin
啊 这题挂了好多次 组合数学确实不好搞
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
int gcd(int a,int b)
{
int c=a%b;
while(c)
{
a=b;b=c;
c=a%b;
}
return b;
}
long long C(int a)
{
long long temp=a;
return temp*(temp-1)*(temp-2)/6;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
m++;n++;
long long ans=C(n*m)-C(n)*m-C(m)*n,temp;
m--;n--;
for(int i=2;i<=n;i++)
for(int j=2;j<=m;j++)
{
temp=gcd(i,j);
ans-=(temp-1)*(n-i+1)*(m-j+1)*2;
}
printf("%lld\n",ans);
}
return 0;
}