... HAVE U TRIED .... AT LEAST 3 TIMES ... OK U CAN SEE THIS ...

Monday, January 6, 2014

LOJ-1138 :: Trailing Zeroes (III)

//Problem link>>http://lightoj.com/volume_showproblem.php?problem=1138


#include <bits/stdc++.h>
using namespace std ;

int rez (int x)
{
    int sum=0 ;
    while (x!=0)
    {
        x=x/5 ;
        sum+=x  ;
    }
    return sum ;
}

int n ;
int go ()
{
    int low=0 ,high=1000000000 ,mid ,ans=0 ;
    while (low<=high)
    {
        mid=(low+high)/2 ;
        if (rez(mid) < n) low=mid+1 ;
        else if (rez(mid) > n) high = mid-1 ;
        else
        {
            ans =mid ;
            high=mid-1;
        }
    }
    return ans ;
}

int main ()
{
    int cas, i ,ret ;
    cin>>cas ;
    for (i=1 ;i <=cas ; i++)
    {
        cin>>n ;
         ret=go () ;

        if (ret==0) cout<<"Case "<<i<<": "<<"impossible"<<endl ;
        else cout<<"Case "<<i<<": "<<ret<<endl ;
    }

    return 0 ;
}

1 comment:

  1. How high becomes 1000000000, plz explain....how can we get maximum 10^18 trailing zeros from this value, isn't it much smaller than maximum number of zeros that means 10^18?

    ReplyDelete