Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- typedef int64_t Int;
- int main() {
- Int nCandy, nMan, maxX, maxCountPerMan;
- std::cin >> nCandy >> nMan >> maxX >> maxCountPerMan;
- Int answ = 0;
- for (Int countPerMan = 1; countPerMan <= maxCountPerMan; ++countPerMan) {
- std::function<Int(Int)> calcAnsw = [&](Int x) {
- return x > maxX ? 0 : x * ((nCandy + x * nMan-1) / (x * nMan));
- };
- std::function<Int(Int)> calcPerMan = [&](Int x) {
- return (nCandy - nCandy % x + nMan*x-1) / (nMan * x);
- };
- // Какой x выбрать чтобы Аркадий получил на руки ровно countPerMan раз
- // countPerMan * x * k >= nCandy
- Int x = (nCandy + countPerMan * nMan-1) / (countPerMan * nMan);
- //std::cout << "countPerMan = " << countPerMan << ", x = " << x << std::endl;
- answ = std::max(answ, calcAnsw(x));
- // Бинпоиск на x:
- Int low = x+1, high = nCandy+1;
- while (high - low > 1) {
- Int mid = (high + low) / 2;
- if (calcPerMan(mid) == countPerMan) {
- low = mid;
- } else {
- high = mid;
- }
- }
- if (calcPerMan(low) == countPerMan) {
- answ = std::max(answ, calcAnsw(low));
- }
- }
- assert(answ != 0);
- std::cout << answ << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement