Big Division
Time limit5sMemory limit128 MB
Cancel the common factors of two huge products given as factor lists and print the reduced fraction.
- Level
Medium4 of 10
- Topics
- Number theory, Math
- Solved
- No attempts yet
Problem
Theoretical physicists did not believe that the ultimate answer to the question of life, the universe, and everything is 42. (That is the answer given in The Hitchhiker's Guide to the Galaxy.) They decided the real answer sits in the quotient of two positive integers and .
Dividing by is harder than it looks. Each of the two numbers is given as a product of many integers, and writing those products out in full can run to hundreds of digits, which an old computer takes an enormous amount of time to divide.
You are a clever programmer, so help the theoretical physicists out.
Input
The first line contains the number of test cases . ()
The first line of each test case contains two integers and . is the length of sequence and is the length of sequence . ()
The second line contains the elements of sequence separated by spaces, and the third line contains the elements of sequence in the same format. Every element is an integer between and .
The two numbers being divided are the products of the sequences, so and .
Output
For each test case, print one line in the form Case #i: X / Y. Here is the test case number starting from 1, and is divided by written as a fraction in lowest terms. Put exactly one space on each side of the slash.
A fraction is in lowest terms when the numerator and denominator have been divided by their greatest common divisor and nothing cancels any further. When the quotient is a whole number, print the denominator anyway. Both and fit in a 32-bit integer.