2013秋13级预备队集训练习4 --D - 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-bit signed integers.

Sample Input 

2 2
2 3
3 3
0 0

Sample Output 

Case 1: 1
Case 2: 3
Case 3: 9
 
 
上面的线有a个点 下面的线有b个点 我的做法是先将下面的线上的点固定 然后上面线上的点逐个增加 分别对每个点计算交叉点。例
从上面1点开始 无交叉点, 然后增加点2 和1点的交叉为 2 + 1 所以总共为3
假设上面的线还有点3  那么它和点1的交叉点为2 + 1 ; 和点2的交叉点为2 + 1 ; 共6
所以每增加一个点都会增加 1+2+3+4…b-1   共有1+2+3+4…a-1次  得总和
#include <stdio.h>
int main()
{
    int a , b , count = 0 , i ;
    long long int sum1 , sum2 ;
    while(scanf("%d %d", &a, &b)!=EOF)
    {
        count++;
        if(a==0 && b==0) break;
        if(b==1 || a==1)
            printf("Case %d: 0\n", count);
        else
        {
            sum2 = sum1 = 0 ;
            for(i = 1 ; i < b ; i++)
                sum1 += i ;
            for(i = 1 ; i < a ; i++)
                sum2 += i ;
            printf("Case %d: %lld\n", count , sum2*sum1);
        }
    }
}

你可能感兴趣的:(2013秋13级预备队集训练习4 --D - How Many Points of Intersection?)