Div2 Medium: DivisibleSetDiv2
|
This is a mathematical problem which is very simple to implement but hard to prove. It turns out that the answer is "Possible" if and only if ∑i=0n−11bi≤1∑i=0n-11bi≤1. You can find a detailed proof below.
We are asked if there exists a sequence of powers of two (a0,a1,...,an−1)(a0,a1,...,an-1) that for every i:
Finally, we should avoid losing accuracy in order to use the perfect formula. Casuing the range of bi is [1, 10], we can multiply a constant C which equal to LCM(1,...,10).
public string isPossible(vector<int> b) { int LCM = 2520, sum = 0; for (int bi : b) sum += LCM / bi; // sum up 1/b multiplied by LCM to avoid floats return (sum <= LCM) ? "Possible" : "Impossible"; }
Recently, I had met two mathematical problems. The last one is hihoCoder_5 using log to get the maximum value.
____+++++____