#P2876. Consultant Payment Scheduling
Consultant Payment Scheduling
Consultant Payment Scheduling
Farmer John's cows now face a series of complex problems. They have P problems (where \(1 \le P \le 300\)) and earn exactly M money per month (with \(1 \le M \le 1000\)). In order to solve a problem in a month, they must hire a consultant. Each consultant can solve any problem in a single month, but charges two payments:
- An upfront payment paid at the start of the month when the problem-solving commences.
- A follow‐up payment paid at the start of the month after the problem is solved.
Both payments of a consultant are integers in the range \([1,M]\). The cows can only spend in a given month the money earned in the previous month. Moreover, they are spendthrifts: any money not used in a month is wasted (i.e. they cannot save money from month to month). The problems have an ordering constraint: problem \(i\) must be solved no later than the month in which problem \(i+1\) is solved.
In other words, if in month \(k\) the cows solve \(x_k\) problems (by hiring \(x_k\) consultants), then:
- In the first month, the available funds is the income from the previous month (\(M\)), and the cows pay only the upfront fees for the problems solved. Hence, \(x_1 \le M\).
- For any month \(k \ge 2\), the available funds (\(M\)) must cover both the follow‐up payments for the problems solved in month \(k-1\) and the upfront payments for the new problems solved in month \(k\). Since each fee is at least \(1\), the constraint is \(x_{k-1} + x_k \le M\).
Once a problem is solved, its follow‐up payment is made at the start of the next month. Thus, if the cows finish solving problems in \(T\) months, they will have made their final payments at the start of month \(T+1\). Your task is to determine the minimum number of months required (including the month for the final payments) to solve all \(P\) problems under optimal scheduling.
Hint: Note that if in month \(T\) the cows solve problems, the maximum number of problems that can be solved under the constraints is given by:
- For \(T=1\): \(S(1)=M\).
- For even \(T\) (i.e. \(T=2k\)): \(S(2k)=k \cdot M\).
- For odd \(T\) (i.e. \(T=2k+1\)): \(S(2k+1)= (k+1) \cdot M - 1\).
You need to find the smallest integer \(T\) (number of months during which problems are solved) satisfying \(S(T) \ge P\). The final answer will then be \(T+1\) (since the follow-up payments for problems solved in month \(T\) are made in month \(T+1\)).
inputFormat
The input consists of a single line containing two space-separated integers: P and M, where \(1 \le P \le 300\) and \(1 \le M \le 1000\).
outputFormat
Output a single integer—the minimum number of months required (including the final payment month) to solve all the problems and complete the payments.
sample
5 10
2