UV problem solution: 701-the archaeologist's Dilemma

Source: Internet
Author: User

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:
  1. /*************************************** **********************************
  2. * Copyright (c) 2008 by liukaipeng *
  3. * Liukaipeng at gmail dot com *
  4. **************************************** *********************************/
  5. /* @ Judge_id 00000 701 C ++ "the archaeologist's Dilemma "*/
  6. # Include <algorithm>
  7. # Include <cmath>
  8. # Include <cstdio>
  9. # Include <cstring>
  10. # Include <deque>
  11. # Include <fstream>
  12. # Include <iostream>
  13. # Include <list>
  14. # Include <map>
  15. # Include <queue>
  16. # Include <set>
  17. # Include <stack>
  18. # Include <string>
  19. # Include <vector>
  20. Using namespace STD;
  21. /*
  22. Let p denotes the prefix, T Denotes the # of lost digits. We are searching
  23. For N, that the prefix of 2 ^ N is P.
  24. We have an inequlity
  25. P * 10 ^ t <= 2 ^ n <(p + 1) * 10 ^ t
  26. Thus
  27. Log2 (p * 10 ^ t) <= log2 (2 ^ N) <log2 (p + 1) * 10 ^ t ),
  28. Which is
  29. Log2 (p) + T * log2 (10) <= n <log2 (p + 1) + T * log2 (10 ).
  30. Also, we know that
  31. P <10 ^ (T-1 ),
  32. That is
  33. T> log10 (p) + 1.
  34. Then, we can brute force on T and find the minmum n.
  35. */
  36. Long Double minexpof2with (Long Double prefix)
  37. {
  38. Long double lower = log2l (prefix );
  39. Long Double upper = log2l (prefix + 1 );
  40. Long double F = log2l (10 );
  41. Long Double T = ceill (log10l (prefix + 0.5) + 1; // avoid log10l (1) = 0
  42. For (; ceill (lower + T * f )! = Floorl (upper + T * F); t + = 1 ){}
  43. Return ceill (lower + T * F );
  44. }
  45. Int main (INT argc, char * argv [])
  46. {
  47. # Ifndef online_judge
  48. Freopen (string (argv [0]) + ". In"). c_str (), "r", stdin );
  49. Freopen (string (argv [0]) + ". Out"). c_str (), "W", stdout );
  50. # Endif
  51. Long Double prefix;
  52. While (CIN> prefix)
  53. Cout <(long) minexpof2with (prefix) <'/N ';
  54. Return 0;
  55. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.