Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- long long countZeroes(long long n){
- long long c = 0;
- while(n){
- n/=5;
- c+=n;
- }
- return c;
- }
- long long trailing_zeroes(long long n){
- // cout << n << endl;
- long long one = 1, sixty_two = 62;
- long long long_max = (one << sixty_two) - one + (one << sixty_two);
- // cout << long_max << endl;
- long long i =0, j = 5*n; //j = long_max;
- while(i < j){
- long long mid = i + (j - i)/2 ;
- long long count = countZeroes(mid);
- if(count == n){
- i = mid;
- j = mid;
- break;
- }
- else if(count < n){
- i = mid + 1;
- }
- else{
- j = mid - 1;
- }
- }
- if(countZeroes(i) == n || countZeroes(j) == n){
- return 5;
- }
- else{
- return 0;
- }
- }
- int main() {
- /* Enter your code here. Read input from STDIN. Print output to STDOUT */
- int Q;
- cin >> Q;
- while(Q--){
- long long n;
- cin >> n;
- if( n == 0 || n ==1 || n ==2){
- cout << 5 << endl;
- }
- else{
- long long ans = trailing_zeroes(n);
- cout << ans << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment