Algorithm


problem Link :  https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&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. 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

Code Examples

#1 Code Example with C Programming

Code - C Programming

 #include <bits/stdc++.h>
using namespace std;
int fastio() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(nullptr); return 0; }

int __fastio = fastio();

typedef long long ll;


void solve() {
	ll a, b;
	int tc= 1;
	while(cin >> a >> b, (a+b)) {
		ll nc2_a = a*(a-1)/2, nc2_b = b*(b-1)/2; // number of pairs in each row
		// 1 pair from a & 1 pair from b = 1 intersection
		cout << "Case " << tc++ << ": ";
		cout << nc2_a * nc2_b << "\n";
	}
}

int main() {
    int t=1;
    while(t--) {
        solve();
    }
}
 
Copy The Code & Try With Live Editor

Input

x
+
cmd
2 2
2 3
3 3
0 0

Output

x
+
cmd
Case 1: 1
Case 2: 3
Case 3: 9
Advertisements

Demonstration


UVA Online Judge solution - 10790-How Many Points of Intersection - UVA Online Judge solution in C,C++,java  

Previous
UVA Online Judge solution - 10785-The Mad Numerologist - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 108-Maximum Sum - UVA Online Judge solution in C,C++,java