UVa Problem Solution: 701 - The Archaeologist's Dilemma
2008-11-24 13:29
1131 查看
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;
}
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;
}
相关文章推荐
- UVa Problem Solution: 100 - The 3n+1 problem
- UVa Problem Solution: 10142 - Australian Voting
- UVa Problem Solution: 10082 - WERTYU
- UVa Problem Solution: 10188 - Automated Judge Script
- UVa Problem Solution: 10037 - Bridge
- UVa Problem Solution: 10152 - ShellSort
- UVa Problem Solution: 847 - A Multiplication Game
- UVa Problem Solution: 10006 - Carmichael Numbers
- UVa Problem Solution: 10042 - Smith Numbers
- UVa Problem Solution: 10090 - Marbles
- UVa Problem Solution: 10189 - Minesweeper
- UVa Problem Solution: 10038 - Jolly Jumpers
- UVa Problem Solution: 843 - Crypt Kicker
- UVa Problem Solution: 10044 - Erdos Number
- UVa Problem Solution: 10132 - File Fragmentation
- UVa Problem Solution: 10191 - Longest Nap
- UVa Problem Solution: 10105 - Polynomial Coefficients
- UVa Problem Solution: 10049 - Self-describing Sequence
- UVa Problem Solution: 10089 - Repackaging
- UVa Problem Solution: 10137 - The Trip