Normalizing Display Aspect Ratios
A device-analytics pipeline collects the screen resolution of every device that reports in, written
as WxH (width by height in pixels), for example 1920x1080. The dashboard groups resolutions by
aspect ratio, which is the ratio W:H reduced to lowest terms. So 1920x1080 and 1280x720 both
go in the 16:9 bucket, while 1080x1920 (a phone held upright) goes in 9:16.
Given the n reported resolutions, print the aspect ratio of each one. Then report the most common
aspect ratio and how many resolutions have it. If several ratios tie for most common, pick the one
with the smallest reduced width a, and among those the one with the smallest reduced height b.
Input
The first line contains n (1 ≤ n ≤ 2·10^5), the number of reported resolutions.
Each of the next n lines contains one resolution in the form WxH: two integers W and H
(1 ≤ W, H ≤ 10^9) separated by a lowercase letter x, with no spaces.
Output
Print n lines. The i-th line is the aspect ratio of the i-th resolution, written as a:b, where
g is the greatest common divisor of W and H, a = W / g and b = H / g.
Then print one more line: the most common ratio a:b, a space, and the number of resolutions that
have that ratio.
Notes
In the first example, 1920x1080, 1280x720 and 3840x2160 all reduce to 16:9, so 16:9 wins with
3 resolutions.
In the second example, 4:3 and 1:1 each occur twice. 1:1 has the smaller width, so it wins
the tie. Also note that 1000x1000 reduces to 1:1, and 7x5 is already in lowest terms.