problems / 249

Normalizing Display Aspect Ratios

easy gcd hashing math 1000 ms · 256 MB

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.

Example 1

Input

5
1920x1080
1280x720
1080x1920
2560x1080
3840x2160

Output

16:9
16:9
9:16
64:27
16:9
16:9 3

Example 2

Input

5
800x600
1000x1000
7x5
1024x768
1x1

Output

4:3
1:1
7:5
4:3
1:1
1:1 2

Hints · reveal one at a time if you're stuck

Hint 1

1920x1080 and 1280x720 look different but share a ratio. What number can you divide both sides of each by so that they end up as the same pair?

Hint 2

Divide W and H by their greatest common divisor. The reduced pair of integers is an exact key you can count in a hash map, which a floating-point W / H is not.

report an issue Prefer your own editor? sooks prep 249 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.