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
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.
Code:
- /*************************************** **********************************
- * Copyright (c) 2008 by liukaipeng *
- * Liukaipeng at gmail dot com *
- **************************************** *********************************/
- /* @ Judge_id 00000 701 C ++ "the archaeologist's Dilemma "*/
- # Include <algorithm>
- # Include <cmath>
- # Include <cstdio>
- # Include <cstring>
- # Include <deque>
- # Include <fstream>
- # Include <iostream>
- # Include <list>
- # Include <map>
- # Include <queue>
- # Include <set>
- # Include <stack>
- # Include <string>
- # Include <vector>
- Using namespace STD;
- /*
- 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
- 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.
- */
- Long Double minexpof2with (Long Double prefix)
- {
- Long double lower = log2l (prefix );
- Long Double upper = log2l (prefix + 1 );
- Long double F = log2l (10 );
- Long Double T = ceill (log10l (prefix + 0.5) + 1; // avoid log10l (1) = 0
- For (; ceill (lower + T * f )! = Floorl (upper + T * F); t + = 1 ){}
- Return ceill (lower + T * F );
- }
- Int main (INT argc, char * argv [])
- {
- # Ifndef online_judge
- Freopen (string (argv [0]) + ". In"). c_str (), "r", stdin );
- Freopen (string (argv [0]) + ". Out"). c_str (), "W", stdout );
- # Endif
- Long Double prefix;
- While (CIN> prefix)
- Cout <(long) minexpof2with (prefix) <'/N ';
- Return 0;
- }