# Bzoj3505 [cqoi2014]数三角形

Posted by yjjr's blog on February 6, 2018

Description

Input

Output

Sample Input

2 2

Sample Output

76

1<=m,n<=1000

1.     三点共一条水平直线

2.     三点共一条竖直直线

3.     三点共一条斜线（用gcd判断和计算）

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
#define mem(x,num) memset(x,num,sizeof x)
using namespace std;
inline LL read()
{
LL f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
const int maxn=1e6+6;
LL c[maxn][4],n,m,ans,t;

int main()
{
n=read(),m=read();
n++,m++;
c[0][0]=1;
rep(i,1,n*m){
c[i][0]=1;
rep(j,1,3)c[i][j]=c[i-1][j-1]+c[i-1][j];
}
ans=c[n*m][3];
ans-=n*c[m][3];
ans-=m*c[n][3];
rep(i,1,n-1)
rep(j,1,m-1){
t=gcd(i,j)+1;
if(t>2)ans-=(t-2)*2*(n-i)*(m-j);
}
printf("%lld\n",ans);
return 0;
}