Jayakrishna14

trailing zeroes modified

Aug 12th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long countZeroes(long long n){
  5. long long c = 0;
  6. while(n){
  7. n/=5;
  8. c+=n;
  9. }
  10. return c;
  11. }
  12.  
  13. long long trailing_zeroes(long long n){
  14. // cout << n << endl;
  15. long long one = 1, sixty_two = 62;
  16. long long long_max = (one << sixty_two) - one + (one << sixty_two);
  17. // cout << long_max << endl;
  18.  
  19. long long i =0, j = 5*n; //j = long_max;
  20. while(i < j){
  21. long long mid = i + (j - i)/2 ;
  22. long long count = countZeroes(mid);
  23. if(count == n){
  24. i = mid;
  25. j = mid;
  26. break;
  27. }
  28. else if(count < n){
  29. i = mid + 1;
  30. }
  31. else{
  32. j = mid - 1;
  33. }
  34. }
  35.  
  36. if(countZeroes(i) == n || countZeroes(j) == n){
  37. return 5;
  38. }
  39. else{
  40. return 0;
  41. }
  42. }
  43.  
  44. int main() {
  45. /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  46. int Q;
  47. cin >> Q;
  48. while(Q--){
  49. long long n;
  50. cin >> n;
  51. if( n == 0 || n ==1 || n ==2){
  52. cout << 5 << endl;
  53. }
  54. else{
  55. long long ans = trailing_zeroes(n);
  56. cout << ans << endl;
  57. }
  58. }
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment