BZOJ 3505: [Cqoi2014]数三角形 数学

时间:2022-04-25 09:44:47

3505: [Cqoi2014]数三角形

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=3505

Description

给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个。下图为4x4的网格上的一个三角形。

注意三角形的三点不能共线。

Input

输入一行,包含两个空格分隔的正整数m和n。

Output

输出一个正整数,为所求三角形数量。

Sample Input

2 2

Sample Output

76

HINT

1<=m,n<=1000

题意

题解:

任意选择三个不在同一条直线上的三个点即是满足题意的点

考虑补集,随意选择3个点,然后删除同一直线,同一斜线的就好了

直线上的比较简单,只用想斜线的就好了

枚举矩形,矩形的长宽分别为i,j,那么这个矩形的对角线就会经过gcd(i,j)-1个点

然后这个图里面可以放(n-i+1)*(m-j+1)个矩形

然后搞一搞就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 510000
#define mod 10007
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //§ß§é§à§é¨f§³
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** ll gcd(ll a,ll b)
{
return b==?a:gcd(b,a%b);
}
int main()
{
ll n=read(),m=read();
n++,m++;
ll ans = (n*m)*(n*m-)*(n*m-2LL)/6LL;
ans -= n*(m*(m-1LL)*(m-2LL)/6LL);
ans -= m*(n*(n-1LL)*(n-2LL)/6LL);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
ll tmp = gcd(i,j)+1LL;
if(tmp>2LL)
ans-=(tmp-2LL)*2LL*(n-i*1LL)*(m-j*1LL);
}
}
printf("%lld\n",ans);
}