当前位置:网站首页>P3166数三角形(容斥+gcd)
P3166数三角形(容斥+gcd)
2022-07-19 01:50:00 【to cling】
Problem
给定一个 N × M N\times M N×M 的网格,请计算三点都在格点上的三角形共有多少个。注意三角形的三点不能共线。
Solution
- 对于网格中的两个点,设其横纵坐标之差分别为:i,j
则这两个点连线上的整数格点的个数为: g c d ( i , j ) − 1 gcd(i, j) - 1 gcd(i,j)−1
证明:两点(0,0)和(6, 9)的横纵坐标之差为(6, 9)。
则点(2, 3),(4, 6),(6, 9)这三个点都在这两个点的连线上。
(由该结论可知:如果两个点坐标之差(i,j)的最大公约数为1,则其连线上无整数格点)
Code
int gcd(int a, int b)
{
if (!b) return a;
return gcd(b, a % b);
}
ll C(ll x)
{
return x * (x - 1) * (x - 2) / 6;
}
int main()
{
IOS;
ll n, m;
cin >> n >> m;
n++; m++;
ll ans = C(n * m);
ans -= n * C(m) + m * C(n);
for (int i = 1; i < n; i++)
for (int j = 1; j < m; j++)
ans -= (n - i) * (m - j) * (gcd(i, j) - 1) * 2;
cout << ans << endl;
return 0;
}
边栏推荐
猜你喜欢
随机推荐
专访铃盛(RingCentral)何必苍:以不断创新的MVP赋能未来混合办公
二——01Day:對象的索引理解,對象上的this指向,對象轉換為字符串,函數的預解析,arguments.callee的用法,
今日直播|Apache Pulsar Meetup:vivo、腾讯云、BIGO、云兴科技实践分享
创未来 享非凡 | GBASE南大通用亮相openGauss Developer Day 2022
剑指 Offer 28. 对称的二叉树
【C语言刷LeetCode】1604. 警告一小时内使用相同员工卡大于等于三次的人(M)
【文獻閱讀】NPE: An FPGA-based Overlay Processor for Natural Language
记一个laravel问题Script @php artisan package:discover handling the post-autoload-dump event returned with
U++ 使用setTimer函数
认证培训|StreamNative Certification 培训第 2 期
020-024多态回顾
DOM operation of JS - event type
Based on yarn1 Sharing of monorepo practice of X
BigDecimal使用
专访铃盛(RingCentral)何必苍:以不断创新的MVP赋能未来混合办公
云主机内网通信ping不通问题处理过程
DOM operation of JS -- event object
请问怎么在网页上让用户建立数据表存储到数据库中
Advanced numbers | [differential calculus of multivariate functions] concept chapter -- the relationship between continuity, partial differentiation and differentiability
带你认识C语言自定义类型——结构体、枚举、联合