zoukankan      html  css  js  c++  java
  • uva 10790 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 andb, 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 aand 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(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
    

    Problem setter: Md. Bahlul Haider
    Special Thanks: Shahriar Manzoor

    Miguel Revilla 2004-12-10
     

    这道题目我也没做出来,看了别人的做法才想明白。方法是找规律:

    设上面一行为A,有a个点,下面一行为B,有b个点。

    A行的第1个点与B行的第b个点相连,这条线上有(a - 1) * (b - 1)个点;

    A行的第1个点与B行的第b - 1个点相连,这条线上有(a - 1) * (b - 2)个点;

    A行的第1个点与B行的第b - 2个点相连,这条线上有(a - 1) * (b - 3)个点;

    ………………………………………………

    A行的第1个点与B行的第2个点相连,这条线上有(a - 1) * (1)个点;

    共(a - 1) * (b - 1) * b / 2个点;

    同理,

    A行的第2个点与B行的第b个点相连,这条线上有(a - 2) * (b - 1)个点;

    A行的第2个点与B行的第b - 1个点相连,这条线上有(a - 2) * (b - 2)个点;

    ………………………………………………

    共(a - 2) * (b - 1) * b / 2个点;

    以此类推,

    A行的第a - 1个点与B行的第b个点相连,这条线上有(1) * (b - 1)个点;

    A行的第a - 1个点与B行的第b - 1个点相连,这条线上有(1) * (b - 2)个点;

    ………………………………………………

    A行的第a - 1个点与B行的第2个点相连,这条线上有(1) * (1)个点;

    共1 * (b - 1) * b / 2个点;

    累加,提取公因式(b - 1) * b / 2, 得到总和:(a - 1) * a * (b - 1) * b / 2.

    拙劣的代码
     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 int main(void)
     8 {
     9     long long int a, b, t;
    10 
    11     t = 1;
    12     while (cin >> a >> b)
    13     {
    14         if (!a && !b)    break;
    15         cout << "Case " << t << ": "
    16         << a * b * (a - 1) * (b - 1) / 4 << endl;
    17         t++;
    18     }
    19 
    20     return 0;
    21 }
  • 相关阅读:
    RMAN参考使用手册2[转载]
    硬盘故障的解决
    控制文件和重做日志文件
    设置DBID
    让虚拟机从U盘启动[转载]
    windows7桌面预览不出现的解决方法
    让.Net程序脱离.Net Framework框架运行
    抗辐射多吃六种食物《转》
    ASPX.NET学习记录(一)
    饮食影响人的七情六欲《转》
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/2758012.html
Copyright © 2011-2022 走看看