Messed Up Pictures
Time limit3sMemory limit256 MB
For each given image, find the smallest listed-ratio size that covers it directly or after a 90-degree rotation, and report the operation count.
- Level
Medium5 of 10
- Topics
- Math, Brute force
- Solved
- No attempts yet
Problem
Someone messed up the pictures on your friend's computer. Most of the image files were cropped, rotated by 90 degrees, or both, and the rest were left alone. Your friend still remembers the list of aspect ratios of all the original images.
The aspect ratio of an image of size is , where is the greatest common divisor of and .
The contents of an image do not matter here, so a rotation swaps the width and the height. A crop selects a rectangle with integer side lengths inside the image, with its sides parallel to the sides of the image, and the selected rectangle is smaller than the image it is taken from.
Every messed up image was produced from its original by applying at most one crop and at most one rotation, in either order.
You are given the size of every messed up image and the list of possible aspect ratios. For each image, print one possible original size and the number of operations used to mess it up.
Input
The first line contains one integer (), the number of test cases.
Each test case starts with a line containing one integer (), the number of possible aspect ratios. Each of the next lines contains two integers and () describing one aspect ratio. The greatest common divisor of and is always 1, and all aspect ratios inside one test case are distinct.
The next line contains one integer (), the number of messed up images. Each of the next lines contains two integers and (), the size of one messed up image.
Output
For each test case, print a line Case n:, where is the number of the test case counting from 1. Then print lines, one per image, each containing three integers , and separated by single spaces.
and are a possible original size of that image. With the greatest common divisor of and , the pair must equal one of the given aspect ratios. is the number of operations used to mess the image up, so is 0 when nothing changed, 1 when the image was only cropped or only rotated, and 2 when it was both cropped and rotated.
If several answers are possible, print the one with the smallest . If several remain, print the one with the smallest . If several still remain, print the one with the smallest .