BZOJ 3505[Cqoi2014]数三角形

题面:

3505: [Cqoi2014]数三角形

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1590  Solved: 977
[Submit][Status][Discuss]

Description

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

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

Input

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

Output

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

Sample Input

2 2

Sample Output

76

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

HINT

一共$\binom{n*m}{3}$种,减去三点共线的情况就好了。

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

 

posted @ 2017-08-12 16:06  avancent  阅读(140)  评论(0编辑  收藏  举报