UVa 10790-How Many Points of Intersection

问题描述】

    We have two rows. There are a dots on the top row and b dots on the bottom row. We draw line segments connecting every dot on the top row with every dot on the bottom row. The dots are arranged in such a way that the number of internal intersections among the line segments is maximized. To achieve this goal we must not allow more than two line segments to intersect in a point. The intersection points on the top row and the bottom are not included in our count; we can allow more than two line segments to intersect on those two rows. Given the value of a and b, your task is to compute P(a, b), the number of intersections in between the two rows. For example, in the following figure a = 2 and b = 3. This figure illustrates that P(2, 3) = 3.

UVa 10790-How Many Points of Intersection_第1张图片

    Input 

    Each line in the input will contain two positive integers a (0 < a ≤ 20000) and b (0 < b ≤ 20000). Input is terminated by a line where both a and b are zero. This case should not be processed. You will need to process at most 1200 sets of inputs. 

    Output 

    For each line of input, print in a line the serial of output followed by the value of P(a, b). Look at the output for sample input for details. You can assume that the output for the test cases will fit in 64-bit signed integers.

    Sample Input

2 2
2 3
3 3
0 0

    Sample Output

Case 1: 1
Case 2: 3
Case 3: 9



【解题思路

    如3线交于一点,则一定可以通过左右移动一个点使其交点分开,上面线段上的两点与下面线段上的两点可以产生一个交点。按照乘法原理,可求出p(a,b)。

    错误思路:先将对应值求出存在数组里,输入时查询输出。因为这里a,b最大值较大,所以数组和a,b均应用long long int,因此如果先运算,数组中存的值会溢出。

    正确思路:找出值与坐标的规律可知:num = (a - 1)*a*b*(b - 1) / 4;   


【具体实现

#include<iostream>

#define minNum 0
#define maxNum 20000+5
#define testNum 1200

using namespace std;

long long int num, a, b;
long long int ANS[maxNum][maxNum] = { 0 };

void Set(){
	ANS[2][2] = 1;

	for (long long int i = 3; i < maxNum; ++i){
		ANS[i][2] = ANS[i - 1][2] + (i - 1);
		ANS[2][i] = ANS[2][i - 1] + (i - 1);
		for (long long int j = 3; j < maxNum; ++j)
			ANS[i][j] = ANS[i - 1][j] + (i - 1)*(j - 1);
	}
}

int main(){
	int test_num = 0;
	while (cin >> a >> b){
		if (!a && !b)
			break;
		cout << "Case " << ++test_num << ": "
			<< ANS[a][b] << endl;
	}

	return 0;
}

       正确解法:

#include<iostream>

using namespace std;

long long int num, a, b;;

long long int Fun(long long int a, long long int b){
	return num = (a - 1)*a*b*(b - 1) / 4;	
}

int main(){
	int test_num = 0;
	while (cin >> a >> b ){
		if (!a && !b)
			break;
		cout << "Case " << ++test_num << ": "
			<< Fun(a,b) << endl;
	}

	return 0;
}

    

  【额外补充

    这道题就是一道纯公式题目,公式推导出来就AC,注意题目要求说的要用long long int。



你可能感兴趣的:(UVa 10790-How Many Points of Intersection)