uva10790

题目大意:给出上下两条线上点的个数,求出上面的点和下面所有点连线后交点的个数。 不会有两条以上的线交于同一个点。

原题:

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(ab), 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.

epsfbox{p10790.eps}

Input 

Each line in the input will contain two positive integers a ( 0 < a$ le$20000) and b ( 0 <b$ le$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 ofP(ab). 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

先是正确的代码:

  思路 :    从 up(1) ->down(b)  =   (a-1)* ( b-1)

           up(2)-> down(b) = (a-2)*(b-1)

           up(3)->down(b) = (a-3)*(b-1)

        .....

        从   up(1)->down(b-1) = (a-1)*(b-2)

          up(2)->down(b-1) = (a-2)*(b-2)

        .....

        以此类推 求和  sum = a*(a-1)/2  * b*(b-1)/2

      代码比较简单

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int tst=1;
 7     long long a,b;
 8     while(cin>>a>>b && a||b)
 9     {
10         cout<<"Case "<<tst<<":"<<" "<<a*(a-1)*b*(b-1)/4<<endl;
11         tst++;
12     }
13     
14     return 0;
15 }

我第一次提交的思路  up(n) -> down (m) =  (n-1)*(b-m) + (a-n)*(m-1)

           然后双重for 循环 累加 但是数据比较大所以超时了

        

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     long long top,bottom;
 7     int tst=1;
 8     while(cin>>top>>bottom)
 9     {
10         if(top==0 && bottom==0)break;
11         
12         if(top>bottom)
13         {
14             int temp;
15             temp=top;
16             top=bottom;
17             bottom=temp;
18         }
19         long long ans=0;
20         for(int up=1;up<=top;up++)
21         {
22             for(int down=1;down<=bottom;down++)
23             {
24                 ans+=(up-1)*(bottom-down)+
25                     (top-up)*(down-1);
26             }
27         }
28         cout<<"case "<<tst<<":"<<" "<<ans/2<<endl;
29         
30     }
31     
32     
33     return 0;
34 }
原文地址:https://www.cnblogs.com/doubleshik/p/3414362.html