uva 12075

12075 - Counting Triangles

Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in an M x N grid. For example in a (1 x 2) grid there are 18 different lattice triangles as shown in the picture below:

\epsfbox{p3295.eps}

Input 

The input file contains at most 21 sets of inputs.

Each set of input consists of two integers M and N ( 0 < M, N$ \le$1000 ). These two integers denote that you have to count triangles in an(M x N) grid.

Input is terminated by a case where the value of M and N are zero. This case should not be processed.

Output 

For each set of input produce one line of output. This output contains the serial of output followed by the number lattice triangles in the(M x N) grid. You can assume that number of triangles will fit in a 64-bit signed integer.

Sample Input 

1 1
1 2
0 0

Sample Output 

Case 1: 4
Case 2: 18

大意:

  给出一个 n*m 的网格, 求出这个网格中的三角形的个数.

  这个和上一题有异曲同工之妙....

  minus[i][j] 为矩形尺寸为 i,j 的时候的 (i,j) 对共线点对的贡献.

  则 minus[i][j] = minus[i-1][j] + minus[i][j-1] - minus[i-1][j-1] + gcd(i,j) - 1;(指的是非平行(或垂直)共线,所以要另外计算).

  ans[i][j] 为矩形尺寸为 i,j 的时候, 总的重复点对个数.

  则 ans[i][j] = ans[i-1][j] + ans[i][j-1] - ans[i-1][j-1] + minus[i][j];

  然后答案就出来了.

  

 1 #include<cstdlib>
 2 #include<cstdio>
 3 #include<iostream>
 4 using namespace std;
 5 long long n,m,ans;
 6 long long cond[1500][1500],mit[1500][1500];
 7 int kase;
 8 int gcd(int x,int y){ return !y ? x : gcd(y,x%y); }
 9 long long C(long long x){ return x * (x - 1) * (x-2) / 6; }
10 void init(){
11     for(int i = 1; i <= 1000; ++i)
12         for(int j = 1; j <= 1000; ++j) cond[i][j] = cond[i-1][j] + cond[i][j-1] - cond[i-1][j-1] + gcd(i,j) - 1;
13     for(int i = 1; i <= 1000; ++i)
14         for(int j = 1; j <= 1000; ++j) mit[i][j] = mit[i-1][j] + mit[i][j-1] - mit[i-1][j-1] + cond[i][j];
15 }
16 int main()
17 {
18     freopen("count.in","r",stdin);
19     freopen("count.out","w",stdout);
20     init();
21     while(cin >> n >> m, n+m){
22         n++, m++;
23         ans = C(n * m);
24         ans -= m*C(n) + n*C(m);
25         ans -= mit[n-1][m-1] * 2;
26         cout << "Case " << ++kase << ": " << ans << endl;
27     }
28     return 0;
29 }
View Code

 

转载于:https://www.cnblogs.com/Mr-ren/p/4227443.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值