#B3942. Minimum Green Balls Addition
Minimum Green Balls Addition
Minimum Green Balls Addition
You are given n balls, among which k are orange and the rest are green. Your task is to determine the minimum number of additional green balls that must be added in order to ensure that the proportion of orange balls does not exceed \(\frac{p}{q}\).
More formally, if the total number of balls is \(a\) and the number of orange balls is \(b\), then the proportion of orange balls is \(\frac{b}{a}\). Initially, \(a=n\) and \(b=k\). You are only allowed to add green balls. Find the smallest non-negative integer \(x\) such that:
\[ \frac{k}{n+x} \le \frac{p}{q} \]
If the condition is already satisfied, output 0.
inputFormat
The input consists of a single line containing four integers separated by spaces: n, k, p, and q.
\(1 \le k \le n \le 10^9\) and \(1 \le p, q \le 10^9\). It is guaranteed that \(\frac{p}{q}\) is a valid ratio.
outputFormat
Output a single integer, which is the minimum number of green balls that need to be added.
sample
10 4 1 2
0