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.

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(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

有两条线,输入第一条线上点n个,和第二条线上点m个。 输出如果把n,m上每两点都相连,交点有几个。。

解法:加入n有3个,m有3个。假设从m这条线找点去连接m, 第一点连过去3条一个交点都没。第二点连过去。m2连n1的时候将会交第一点连过去的m1n2、m1n3,m2连n2的时候将会交第一点和第二点连过去的m1n3。。为1 + 2;第三点连过去将会交第一点和第二点连过去的几条。为 2 * (1 + 2)。。

找到规律可以推出。。交点个数x为,求出一个sum = (1 + 2 + ... + n - 1)。 然后x = sum * 1 + sum * 2 + ... + m - 1;

输出x。

代码:

#include <stdio.h>
#include <string.h> int a, b; int main()
{
int t = 1;
while (scanf("%d%d", &a, &b) != EOF && a || b)
{
long long num1 = 0;
long long sum = 0;
for (int i = 1; i < a; i ++)
num1 += i;
for (int i = 1; i < b; i ++)
sum += num1 * i;
printf("Case %d: %lld\n", t ++, sum);
}
return 0;
}

UVA 10790 How Many Points of Intersection?的更多相关文章

  1. 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 segment ...

  2. UVA 10790 (13.08.06)

     How Many Points of Intersection?  We have two rows. There are a dots on the toprow andb dots on the ...

  3. How Many Points of Intersection?

    uva10790:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  4. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  5. Volume 1. Maths - Misc

    113 - Power of Cryptography import java.math.BigInteger; import java.util.Scanner; public class Main ...

  6. Coursera Robotics系列课心得

    Robotics Perception Professor Kostas and Jianbo Shi week 1: camera model 凸透镜成像原理:凸透镜焦点与焦距是固定的,这是物理性质 ...

  7. [算法]A General Polygon Clipping Library

    A General Polygon Clipping Library Version 2.32    http://www.cs.man.ac.uk/~toby/alan/software/gpc.h ...

  8. [非官方]ArcGIS10.2 for Desktop扩展工具包——XTools Pro

    XTools Pro 是一套为ArcGIS平台设计的矢量空间分析. 形状转换和表管理扩展工具,大大增强了 ArcGIS 的功能,使用该工具能够提高 ArcGIS 用户的效率和性能. XTools Pr ...

  9. Circles and Pi

    Circles and Pi Introduction id: intro-1 For as long as human beings exist, we have looked to the sky ...

随机推荐

  1. github 推送时can't be established.

    http://www.xuebuyuan.com/2095099.html 飞凡@FANZ /e/learngit (master)$ git push origin masterThe authen ...

  2. Google Guava的splitter用法

    google的guava库是个很不错的工具库,这次来学习其spliiter的用法,它是一个专门用来 分隔字符串的工具类,其中有四种用法,分别来小结 1 基本用法: String str = " ...

  3. ios NSKeyedArchiver 保存对象与对象数组

    废话不说,直接上代码 // // CommunityTool.h // SmartCommunity // // Created by chenhuan on 15/9/2. // Copyright ...

  4. JQUERY的应用

    JQUERY的应用,以及和JS的对比: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...

  5. 编写自己的javascript功能库之Ajax(仿jquery方式)

    本人学习的是php,所以就用php跟js来演示代码了,主要是锻炼自己写js的能力,练练手而已. 下面这是我编写的操作ajax的代码功能,勉强让我称之为库吧.. js代码实例(tool.ajax.js) ...

  6. 一个Hadoop难以查找的错误

    This script is Deprecated. Instead use start-dfs.sh and start-yarn.sh Starting namenodes on [Master1 ...

  7. IDA pro 的Python环境变量设置

    推荐使用IDA PRO6.1+Python2.6 安装完毕Python2.6后,添加如下的环境变量: PYTHONHOME=C:\Python26PATH=%PATH%;C:\Python26LIB= ...

  8. ImageButton自定义按钮的按下效果的高效实现方法(非一般)

    通常情况下,我们可以采用如下方式实现: <?xml version="1.0" encoding="UTF-8"?> <selector xm ...

  9. 算法导论(第三版)习题Exercises4.3(第四章三节)算法导论的一个印刷错误

    本节系列证明都可见4.5节需要说明的有4.3-8,4.3-9两题 4.3-8(本题有误) T(n)=4T(n/2)+n2根据4.5理论,结果为Θ(n2lgn) 4.3-9 m = lgn T(2m) ...

  10. WustOJ 1575 Gingers and Mints(快速幂 + dfs )

    1575: Gingers and Mints Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 24 ...