题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3505
好题!一定要经常回顾!
那个 一条斜线上的点的个数是其两端点横坐标之差和纵坐标之差的gcd-1 真是很妙。
https://blog.csdn.net/u012288458/article/details/48624859
https://www.cnblogs.com/Var123/p/5377616.html
然而还可以递推?https://www.cnblogs.com/liu-runda/p/5993244.html 反正没管……
#include<iostream> #include<cstdio> #include<cstring> #define ll long long using namespace std; int n,m; ll ans; ll C(int x) { return (ll)x*(x-1)*(x-2)/6; } int gcd(int a,int b){return b?gcd(b,a%b):a;} int main() { scanf("%d%d",&n,&m);n++;m++; ans=C(n*m)-n*C(m)-m*C(n); for(int i=1;i<n;i++)// bh node from 0 to n-1 for(int j=1;j<m;j++) { ll num=gcd(i,j)-1; if(num>=1)ans-=num*(n-i)*(m-j)*2; } printf("%lld",ans); return 0; }