数三角形
// 数三角形.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
http://ybt.ssoier.cn:8088/problem_show.php?pid=1655
https://loj.ac/p/2240
给定一个 n×m 的网格,请计算三点都在格点上的三角形共有多少个。下图为 4×4 的网格上的一个三角形。
注意:三角形的三点不能共线。
【输入】
输入一行,包含两个空格分隔的正整数 m 和 n。
【输出】
输出一个正整数,为所求三角形数量。
【输入样例】
2 2
【输出样例】
76
【提示】
数据范围与提示:
对于所有数据,1≤m,n≤1000。
2 3
200
6 9
52758
10 5
43934
*/
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int n, m;
const int N = 1010;
long long C(int a, int b)
{
return 1ll * a * (a - 1) * (a - 2) / 6;
}
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
cin >> n >> m;
n++; m++;
long long ans = C(n*m,3);
ans -= (1LL*n * C(m, 3) + 1LL*m * C(n, 3));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
ans -= 2ll * (n - i) * (m - j)*(gcd(i,j)-1);
}
}
cout << ans << endl;
return 0;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力

