How Many Points of Intersection?
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.
Input
Each line in the input will contain two positive integers a ( 0 < a20000) and b ( 0 < b20000). 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-bitsigned integers.
Sample Input
2 2 2 3 3 3 0 0
Sample Output
Case 1: 1 Case 2: 3 Case 3: 9
Problem setter: Md. Bahlul Haider
Special Thanks: Shahriar Manzoor
Miguel Revilla 2004-12-10
这道题目我是这样想的: 假设上面有top个点,下面有bottom个点,对上面的top个点从1到n进行编号 ,那么编号为1的点分别与下面的bottom个点相连,此时有bottom条线,编号为2的点分别与下面的bottom个点相连此时与编号为1产生的线交点的个数为 d=(bottom-1)+(bottom-2)+........+2+1;编号为3的点产生的交点数d(与编号1交线产生的)+d(与编号2交线产生的)=2*d ;同理 编号为4的点产生的交点数为3*d ......... 交点总数为 d+2d+3d+4d+.....+(top-1)d .(top>1) (等差数列) 程序实现如下:
#include <stdio.h>
#include <string.h>
int main()
{
long long int i,n,m,s,t,d,a,top,bottom;
i=1;
while(scanf("%lld %lld",&top,&bottom)!=EOF)
{
if(top==0&&bottom==0)
{
break;
}
s=0;
d=(bottom-1)+(bottom-1)*(bottom-2)/2;
n=top-1;
a=d;
s=n*a+(n-1)*n/2*d;
printf("Case %lld: %lld\n",i++,s);
}
return 0;
}