Advertisement
dmkozyrev

WRONG

Apr 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. typedef int64_t Int;
  4.  
  5. int main() {
  6.     Int nCandy, nMan, maxX, maxCountPerMan;
  7.     std::cin >> nCandy >> nMan >> maxX >> maxCountPerMan;
  8.    
  9.     Int answ = 0;
  10.     for (Int countPerMan = 1; countPerMan <= maxCountPerMan; ++countPerMan) {
  11.        
  12.         std::function<Int(Int)> calcAnsw = [&](Int x) {
  13.             return x > maxX ? 0 : x * ((nCandy + x * nMan-1) / (x * nMan));
  14.         };
  15.        
  16.         std::function<Int(Int)> calcPerMan = [&](Int x) {
  17.             return (nCandy - nCandy % x + nMan*x-1) / (nMan * x);
  18.         };
  19.        
  20.         // Какой x выбрать чтобы Аркадий получил на руки ровно countPerMan раз
  21.         // countPerMan * x * k >= nCandy
  22.         Int x = (nCandy + countPerMan * nMan-1) / (countPerMan * nMan);
  23.         //std::cout << "countPerMan = " << countPerMan << ", x = " << x << std::endl;
  24.         answ = std::max(answ, calcAnsw(x));
  25.         // Бинпоиск на x:
  26.         Int low = x+1, high = nCandy+1;
  27.         while (high - low > 1) {
  28.             Int mid = (high + low) / 2;
  29.             if (calcPerMan(mid) == countPerMan) {
  30.                 low = mid;
  31.             } else {
  32.                 high = mid;
  33.             }
  34.         }
  35.         if (calcPerMan(low) == countPerMan) {
  36.             answ = std::max(answ, calcAnsw(low));
  37.         }
  38.     }
  39.     assert(answ != 0);
  40.     std::cout << answ << std::endl;
  41.    
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement