【bzoj3505】[Cqoi2014]数三角形

【bzoj3505】[Cqoi2014]数三角形

Description

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

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

Input

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

Output

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

 

Sample Input


2 2

Sample Output

76

 

数据范围
1<=m,n<=1000

题解

  就是全部去减,减去在一列的,在一行的,在斜对角的,就好了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #define ll long long
 7 using namespace std;
 8 
 9 int n,m;
10 ll c[1005005][4];
11 ll ans,tmp;
12 
13 int gcd(int a,int b)
14 {
15     return b?gcd(b,a%b):a;
16 }
17 void get_C()
18 {
19      c[0][0]=1;
20      for(int i=1;i<=n*m;i++)
21      {
22          c[i][0]=1;
23          for(int j=1;j<=3;j++)
24              c[i][j]=c[i-1][j-1]+c[i-1][j];
25      }
26 }
27 void solve()
28 {
29      ans=c[n*m][3]-n*c[m][3]-m*c[n][3];
30      for(int i=1;i<n;i++)
31          for(int j=1;j<m;j++)
32          {
33              tmp=gcd(i,j)+1;
34              if(tmp>2) ans-=(tmp-2)*2*(n-i)*(m-j);
35          }
36      printf("%lld",ans);
37 }
38 int main()
39 {
40     scanf("%d%d",&n,&m);n++,m++;
41     get_C();
42     solve();
43 }

 

posted @ 2017-11-01 14:13  Kaiser-  阅读(157)  评论(0编辑  收藏  举报