UVa 10790 How Many Points of Intersection?(组合数学)

本文介绍了一个计算二维平面上由两行点之间的连线产生的交叉点数量的问题。通过组合数学的方法,给出了具体的计算公式,并提供了一个使用 C 语言实现的示例代码。

10790 - How Many Points of Intersection?

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1731

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.

\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

题意:

如图,找交点(不存在多于两条线交于一点),此题是这道题的简单版。

思路:

每个点都由两条线上的两个点得到,所以ans=C(a,2) * C(b,2)=ab(a-1)(b-1)/4,最大值(精确)为:3.99960001*1016 

用long long搞定。

完整代码:

#include <cstdio>

int main()
{
	int a, b, t = 1;
	while (scanf("%d%d", &a, &b), a)
		printf("Case %d: %lld\n", t++, (long long)a * b * (a - 1) * (b - 1) / 4);
	return 0;
}

### Java Sets.intersection 方法的功能与用法 `Sets.intersection` 是 Guava 库中的一个方法,用于计算两个集合的交集。该方法返回一个新的集合,其中包含两个输入集合中共有的元素[^3]。 以下是 `Sets.intersection` 的详细说明和使用示例: #### 方法签名 ```java public static <E> Set<E> intersection(Set<? extends E> set1, Set<? extends E> set2) ``` - 参数: - `set1`:第一个集合。 - `set2`:第二个集合。 - 返回值:返回一个新的不可变集合,表示两个集合的交集。 #### 注意事项 - 返回的集合是不可变的(immutable),因此不能对其进行修改操作。 - 如果任意一个集合为空,则返回空集合。 - 输入集合可以是任意类型的集合,但返回的交集是一个不可变集合。 #### 使用示例 以下代码展示了如何使用 `Sets.intersection` 计算两个集合的交集: ```java import com.google.common.collect.Sets; import java.util.HashSet; import java.util.Set; public class Main { public static void main(String[] args) { // 创建两个集合 Set<Integer> set1 = new HashSet<>(); set1.add(1); set1.add(2); set1.add(3); Set<Integer> set2 = new HashSet<>(); set2.add(2); set2.add(3); set2.add(4); // 计算交集 Set<Integer> intersection = Sets.intersection(set1, set2); // 输出结果 System.out.println("Set1: " + set1); System.out.println("Set2: " + set2); System.out.println("Intersection: " + intersection); // 输出 [2, 3] } } ``` 在上述示例中: - `set1` 包含 `{1, 2, 3}`。 - `set2` 包含 `{2, 3, 4}`。 - 调用 `Sets.intersection(set1, set2)` 后,返回的结果为 `{2, 3}`,这是两个集合的交集。 #### 性能特点 - `Sets.intersection` 的性能取决于输入集合的实现方式。如果输入集合是基于哈希表的(如 `HashSet` 或 `LinkedHashSet`),则交集计算的时间复杂度接近 O(n)。 - 如果输入集合是排序集合(如 `TreeSet`),则时间复杂度可能更高。 #### 与其他库的对比 与 C++ 中的 `set_intersection` 不同,Java 的 `Sets.intersection` 返回的是一个不可变集合,而 C++ 的 `set_intersection` 将结果存储到目标容器中[^2]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值