题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3647
题意:给一个n * m的矩形,求出三个点都落在该矩形格点上的三角形的个数
思路:先算出任取三点的数目,再减去两种共线的情况:
1.三个点落在同一行或者同一列
2.三个点落在斜线上,可以枚举矩形,然后固定原点和(i, j)点(相当于这条线段的始末点)再用pick定理算出这条线段上整点数目,统计和这条线段平行的线段在整个矩形中出现的次数即可
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <utility>
#include <functional>
#include <string>
#include <set>
#include <map>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = 50010;
typedef long long ll;
int n, m;
int gcd(int x, int y)
{
return y ? gcd(y, x % y) : x;
}
ll C(ll x)
{
if (x <= 2) return 0;
return x * (x - 1) * (x - 2) / 6;
}
int main()
{
while (cin >> n >> m)
{
ll ans = C((n + 1) * (m + 1));
ans -= (m + 1) * C(n + 1) + (n + 1) * C(m + 1);
for (int i = 2; i <= n; i++)
{
for (int j = 2; j <= m; j++)
{
int t = gcd(i, j) - 1;
ll ttt = t * (m + 1 - j) * (n + 1 - i);
ans -= ttt * 2;
}
}
cout << ans << endl;
}
return 0;
}