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 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 < a
20000) and b ( 0 < b
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 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
有两条线,输入第一条线上点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?的更多相关文章
- 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 ...
- 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 ...
- How Many Points of Intersection?
uva10790:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- Volume 1. Maths - Misc
113 - Power of Cryptography import java.math.BigInteger; import java.util.Scanner; public class Main ...
- Coursera Robotics系列课心得
Robotics Perception Professor Kostas and Jianbo Shi week 1: camera model 凸透镜成像原理:凸透镜焦点与焦距是固定的,这是物理性质 ...
- [算法]A General Polygon Clipping Library
A General Polygon Clipping Library Version 2.32 http://www.cs.man.ac.uk/~toby/alan/software/gpc.h ...
- [非官方]ArcGIS10.2 for Desktop扩展工具包——XTools Pro
XTools Pro 是一套为ArcGIS平台设计的矢量空间分析. 形状转换和表管理扩展工具,大大增强了 ArcGIS 的功能,使用该工具能够提高 ArcGIS 用户的效率和性能. XTools Pr ...
- Circles and Pi
Circles and Pi Introduction id: intro-1 For as long as human beings exist, we have looked to the sky ...
随机推荐
- php 初学笔记
1.变量定义和使用 php中定义变量名为:$aa 在类中一般定义一个新变量需要添加var字,如var $aaa. 但是过程或函数中是不需要添加var 关键字,如$aaa=$_POST['aaaa'], ...
- Canvas -画图
2014-09-30 09:14:57 <!doctype html> <html> <head> <title> </title> < ...
- 算法导论之python实现插入排序
插入排序的花费时间 c*n2, c 是常数 伪代码 INSERTION-SORT(A) for i to A.length key = A[j] //Insert A[j] into the sor ...
- Python新手学习基础之函数-全局变量和局部变量
全局变量和局部变量 我们通常把定义在函数外的变量成为全局变量,定义在函数内的变量称为局部变量,顾名思义,全局变量的作用域是整个代码段,局部变量的作用域就只是在它所属的代码段,变量只在它的作用域里有效. ...
- C程序设计语言练习题1-2
练习1-2 做个实验,当printf函数的参数字符串中包含\c(其中c是上面的转义字符串序列中未曾列出的某一个字符)时,观察一下会出现什么情况. 代码如下: #include <stdio.h& ...
- 记录使用Hibernate查询bean中字段和数据库列类型不匹配问题
今天在工程中遇到Hibernate查询的时候,bean中的字段和数据库中的字段不符合(bean中有pageTime字段,但是数据库中没有此列)报错问题. 具体问题环境: 在auto_off表中,off ...
- JAVA回调函数ANDROID中典型的回调地方
在计算机中回调函数是指通过函数参数传递到其他代码类的,某一块可执行代码的引用,这以设计允许了底层代码调用者在高层定义的子程序. 在JAVA里面我们使用接口的方式来实现函数的回调. 回调的通俗就是:程序 ...
- sql语句读取所有父子标签
select A.HOSPITAL_ID from T_HOSPITAL A connect by prior A.HOSPITAL_ID=A.PARENT_ID start with A.HOSPI ...
- NSIS检测操作系统x64还是x86的问题。
想共同维护一个NSIS脚本文件的,不想搞两个版本的脚本文件了.开始想到了!if语句,没试过,不知道行不行得通.后来google了一下.可以用两个头文件搞定.参照下面链接 Reference: http ...
- 转:Android 测试 Appium、Robotium、monkey等框架或者工具对比
原文地址:http://demo.netfoucs.com/u012565107/article/details/36419297# 1. Appium测试 (功能测试,用户接受度测试,黑盒测试) - ...