Max_Leb

Untitled

Jan 29th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. int good(unsigned long long x, unsigned long long key)
  5. {
  6. if (x * x > key || x == 4294967296)
  7. return 1;
  8. return 0;
  9. }
  10.  
  11. unsigned long long int bin(unsigned long long key)
  12. {
  13. unsigned long long int left, right, mid;
  14. left = 0;
  15. right = 1;
  16. while (right * 2 <= 4294967296) {
  17. right *= 2;
  18. if (good(right, key))
  19. break;
  20. }
  21. while (right - left != 1) {
  22. mid = (left + right) / 2;
  23. if (good(mid, key))
  24. right = mid;
  25. else if (!good(mid, key))
  26. left = mid;
  27. }
  28. return left;
  29. }
  30.  
  31.  
  32. int main()
  33. {
  34. std::ifstream fin("input.txt");
  35. std::ofstream fout("output.txt");
  36. unsigned long long int input;
  37. while (fin >> input){
  38. fout << bin(input) << std::endl;
  39. }
  40.  
  41. fin.close();
  42. fout.close();
  43.  
  44. return 0;
  45. }
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment