NC222469. [USACOJan2020S]LoanRepayment
描述
输入描述
The only line of input contains three space-separated positive integers N, K, and M satisfying K⋅M<N.
输出描述
Output the largest positive integer X such that Farmer John will give Bessie at least N gallons using the above procedure.
示例1
输入:
10 3 3
输出:
2
说明:
For the first test case, when X=2 Farmer John gives Bessie 5 gallons on the first day and M=3 gallons on each of the next two days.C++ 解法, 执行用时: 39ms, 内存消耗: 412K, 提交时间: 2022-01-04 21:54:26
// USACO 2020 Jan, S P2. Loan Repayment #include <bits/stdc++.h> typedef long long LL; using namespace std; int valid(LL N, LL K, LL M, LL x) { LL g = 0; while (K > 0 && g < N) { LL y = (N - g) / x; if (y < M) return (N - g + M - 1) / M <= K; LL days = min((N - x * y - g) / y + 1, K); g += y * days, K -= days; } return g >= N; } int main() { LL N, K, M; ios::sync_with_stdio(false), cin.tie(0); cin >> N >> K >> M; LL L = 1, R = 1e12; while (L < R) { LL m = (L + R + 1) / 2; valid(N, K, M, m) ? (L = m) : (R = m - 1); } printf("%lld\n", L); }