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.
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 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.
- */
- 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 long)minexpof2with(prefix) << '/n';
- return 0;
- }