UVA 10790 How Many Points of Intersection?

本文介绍了一个有趣的问题:当两行不同数量的点通过线段相互连接时,如何计算这些线段之间的内部交叉点总数。文章提供了问题的具体描述、输入输出样例及一种可行的解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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 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;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值