Ultraviolet-12075 Counting Triangles

Source: Internet
Author: User

Ultraviolet-12075 Counting Triangles

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 inMXNGrid. For example in a (1X2) grid there are 18 different lattice triangles as shown in the picture below:

Input

The input file contains at most 21 sets of inputs.

Each set of input consists of two integersMAndN(0 <M,N1000). These two integers denote that you have to count triangles in (MXN) Grid.

Input is terminated by a case where the valueMAndNAre zero. This case shocould 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 (MXN) Grid. You can assume that number of triangles will fit in a 64-bit signed integer.

Sample Input
1 11 20 0
Sample Output
Case 1: 4 Case 2: 18 question: Give you an n * m mesh and ask how many triangles you can have. Idea: First, we calculate the possibility of any three points, and then exclude the same horizontal, vertical, and diagonal lines. The first two are better computed, and the same diagonal lines are slightly more complex. The principle of rejection is also used for the same diagonal line and the previous question, such as the Ultraviolet-1393 Highways. First, we can calculate it and find that gcd (I, j)-1, then we need to repeat it. dp [I] [j] indicates [0, 0] from the upper left corner to this point [I, j], and enumerating the number of three-point collinearity with these two points as the endpoint, finally, we need To Recursive it to get the number of three-point colons in the n * m mesh. Of course, this also requires * 2, which is strange.
#include 
 
   #include 
  
    #include 
   
     #include  #include
    typedef long long ll; using namespace std; const int maxn = 1010; ll dp[maxn][maxn], ans[maxn][maxn]; int n, m; int gcd(int a, int b) { return b==0?a:gcd(b, a%b); } void init() { memset(dp, 0, sizeof(dp)); memset(ans, 0, sizeof(ans)); for (int i = 1; i < maxn; i++) for (int j = 1; j < maxn; j++) dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + gcd(i, j) - 1; for (int i = 1; i < maxn; i++) for (int j = 1; j < maxn; j++) ans[i][j] = ans[i-1][j] + ans[i][j-1] - ans[i-1][j-1] + dp[i][j]; } ll cal(int x) { if (x < 3) return 0; return (ll) x * (x - 1) * (x - 2) / 6; } int main() { init(); int cas = 1; while (scanf("%d%d", &n, &m) != EOF && n + m) { printf("Case %d: ", cas++); ll sum = cal((n+1)*(m+1)) - (m+1)*cal(n+1) - (n+1)*cal(m+1) - ans[n][m] * 2; printf("%lld\n", sum); } return 0; }
   
  
 





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.