Gallup
Time limit1sMemory limit128 MB
Given reported percentages rounded to a fixed number of decimals, find the smallest population whose counts round to exactly those values.
- Level
Medium6 of 10
- Topics
- Math, Brute force, Implementation, Binary search
- Solved
- No attempts yet
Problem
We often see the results of opinion polls reported like this:
Prefer red: 3.5%
Prefer green: 4.5%
Prefer yellow: 22.0%
Prefer blue: 70.0%
This makes you wonder how many people were actually asked. When the numbers are simple — say 20%, 40%, and 40% — you can tell that 5 people were asked (or 10, or 15, or more; we want the minimum number of people).
Every respondent chooses exactly one option, so the individual counts always add up to the total number of people. Each reported percentage is that option's count divided by the total, expressed as a percentage and then rounded to a fixed number of decimal places.
Write a program that reads sets of percentages and, for each set, computes the smallest number of people that could have produced exactly those percentages. This number is always less than .
Input
The input consists of several sets of percentages, one set per line.
Each line begins with an integer (), the number of percentages in the set. If , it is followed by the percentages. Every percentage in a set is written with the same number of decimal places, from to (if there are no decimals, there is no decimal point). The percentages add up to roughly , allowing for small rounding errors.
Digits are removed by rounding half up: when the first removed digit is or greater, the last kept digit is rounded up. For example, becomes , , or depending on how many decimals are kept.
A line consisting of a single (that is, ) marks the end of the input and produces no output.
Output
For each set of data, print one line in the form:
Case i: X
where i is the data set's number (starting from ) and X is the computed number of people. If there is no valid answer in the range to , print error in place of the number.