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:

Input

The input file contains at most 21 sets of inputs.

Each set of input consists of two integers M and N ( 0 < MN1000 ). 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 }

时间: 2024-11-09 00:31:07

uva 12075的相关文章

UVA - 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 in anMxN grid. For examp

uva 12075 - Counting Triangles(容斥原理)

题目链接:uva 12075 - Counting Triangles 题目大意:一个n?m的矩阵,求说有选任意三点,可以组成多少个三角形. 解题思路:任意选三点C(3(n+1)?(m+1))但是有些组合是不可行得,即为三点共线,除了水平和竖直上的组合,就是斜线上的了,dp[i][j]即为ij情况下的斜线三点共线. #include <cstdio> #include <cstring> typedef long long ll; const int N = 1005; ll dp

UVA 12075 - Counting Triangles(容斥原理计数)

题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对于两点,求他们的gcd - 1,得到的就是他们之间有多少个点,那么情况数就可以求了,然后还是利用容斥原理去计数,然后累加出答案 代码: #include <stdio.h> #include <string.h> #include <algorithm> using nam

UVA 1393 Highways,UVA 12075 Counting Triangles —— (组合数,dp)

先看第一题,有n*m个点,求在这些点中,有多少条直线,经过了至少两点,且不是水平的也不是竖直的. 分析:由于对称性,我们只要求一个方向的线即可.该题分成两个过程,第一个过程是求出n*m的矩形中,dp[i][j]代表在这个矩形中终点是到(i,j)这个点的满足题意的直线条数,那么,用dp的话就可以得出递推关系:由长和宽分别小1的左右两个矩形中满足题意的线的条数减去他们共有的矩形中满足的线的条数(容斥减去重复部分),之后还要判断从最左上角的点(1,1)到(i,j)是否可以组成一条线,这个条件是gcd(

Uva 12075 Counting Triangles(容斥)

/* 思路:1.先选三个点 2.去掉同行情况 3.去掉同列情况 4.枚举矩形大小去掉重复情况 */ #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue&g

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED