Tuesday, January 31, 2012

UVA 701 Exclusive solution

<script type="text/javascript"> ch_fluidH = 1; ch_nump = "4"; ch_client = "faltu"; ch_width = 550; ch_height = "auto"; ch_type = "mpu"; ch_sid = "Chitika Default"; ch_color_site_link = "0000CC"; ch_color_title = "0000CC"; ch_color_border = "FFFFFF"; ch_color_text = "000000"; ch_color_bg = "FFFFFF"; </script> <script src="http://scripts.chitika.net/eminimalls/amm.js" type="text/javascript"> </script> Let P denotes the prefix, T denotes the # of lost digits. We are searching for N, that the prefix of 2^N is P.
We have an inequlity of
P*10^T <= 2^N < (P+1)*10^T
thus log2(P*10^T) <= log2(2^N) < log2((P+1)*10^T),
which is log2(P)+T*log2(10) <= N < log2(P+1)+T*log2(10).
Also, we know that P < 10^(T-1),
that is T > log10(P)+1.
Then, we can brute force on T and find the minmum N.


#include<iostream>
#include<stdio.h>
#include<math.h>

using namespace std;



long double ab(long double prf ){
    long double lo,up,f,t;
    lo=log2l(prf);
    up=log2l(prf+1);
    f=log2l(10);
    t=ceill(log10l(prf+0.5))+1;
    for (; ceill(lo+t*f) != floorl(up+t*f); t += 1) {}
    return ceill(lo+t*f);

}



int main(){

    long double prf;
    while(cin>>prf){

        printf("%lld\n",(long long)ab(prf));
        }

    return 0;
    } 
 

No comments:

Post a Comment