Link: hdu 4803 Poor Warehouse Keeper
A screen can display two values: quantity x and total price y. There are two types of operations: one is to add the total price once to change to x, x + y; the other is to add a quantity, and the total price will also add a price accordingly, to x + 1, y + y/x. The total price is an integer after the integer is obtained. The fractional part is ignored. Given a target x and y, the initial state is. The minimum number of times the target State can be obtained. If the target State cannot be reached, the output is-1.
Solution: if you add the total price once, the unit price will increase. If you add the total price once, the unit price will remain unchanged. All in all, the unit price will only go up rather than down.
Then the number of items must also change from 1 to x, that is to say, at least to add X-1 unit price can be, then if the unit price too large s, s? (X? 1) ≥y + 1 is definitely not allowed. Therefore, for each I (Quantity), the unit price has an upper limit to ensure that the total price will not overflow when the number is increased.
Set the current quantity to I and the critical total price to t.
Y + 1> t? Xi
T <(y + 1 )? Ix
That is, for each quantity, add the total price as much as possible, so that the unit price is as large as possible, and ensure that the amount of plus in the dough is not greater than the upper limit, because the unit price is large, the total price of each addition is faster than the target value.
#include
#include
#include
const double eps = 1e-9;int main () { double x, y; while (scanf("%lf%lf", &x, &y) == 2) { if (x > y) { printf("-1\n"); continue; } double k = (y+1-eps) / x; int cnt = (int)x - 1; double tmp = 1; for (int i = 1; i <= (int)x; i++) { double t = i * k; int u = (int)(t-tmp); tmp += u; tmp = tmp * (i+1) / i; cnt += u; } printf("%d\n", cnt); } return 0;}