Description
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 anMxN grid. For example in a (1x 2) grid there are 18 different lattice triangles as shown in the picture below:
The input file contains at most 21 sets of inputs.
Each set of input consists of two integers M andN ( 0 < M, N1000 ). These two integers denote that you have to count triangles in an (MxN) grid.
Input is terminated by a case where the value of M andN are zero. This case should not be processed.
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(MxN) grid. You can assume that number of triangles will fit in a 64-bit signed integer.
1 1 1 2 0 0
Case 1: 4 Case 2: 18 題意: 給你n*m的網格,問你能有幾個三角形。 思路: 我們先計算任意三個點組成的可能,然後排除同一水平,同一垂直的,同一斜線的,前兩個比較好計算,同一斜線的稍復雜。 同一斜線的和上一題UVA - 1393 Highways 一樣也是要用容斥原理,首先我們動手計算一下,可能發現每次多的是gcd(i, j)-1,然後再去重,dp[i][j]代表從左上角[0,0] 到這個點[i,j]並以這兩個點為端點枚舉三點共線的個數,最後還要遞推一次,得到n*m的網格三點共線的個數,當然這也要*2,感覺怪怪的,瞎搞過的
#include#include #include #include #include