Ultraviolet A Problem 701 the archeologist's dilemma (the troubles of archaeologists)

Source: Internet
Author: User
Tags dot net
// The archeologist's dilemma (archaeological annoyance) // PC/Ultraviolet IDs: 110503/701, popularity: A, success rate: Low Level: 1 // verdict: accepted // submission date: 2011-05-29 // UV Run Time: 0.212 S // All Rights Reserved (c) 2011, Qiu. Metaphysis # Yeah dot net //// Algorithm : The power of brute force computing 2 will soon reach the upper bound of the integer representation, unless calculated using an alternative method. The following shows two methods. One method is to calculate the power of 2 until the given digit is found. This method can calculate the result, however, it is easy to exceed the 10 S // time limit and memory limit. The second method is based on the following inequality (assuming that the dropped number has a total of K bits, the existing number is N, and the minimum power to be // calculated is X ): /// N * 10 ^ k <2 ^ x <(n + 1) * 10 ^ K // (logn + k)/log2) <x <(log (n + 1) + k)/log2) // The second method can obtain the AC, but for a relatively large number, the computing time is still long. Because the power Number of 2 is infinite, // it cannot be determined whether NO x meets the given conditions, so no power of 2 cannot be output ". # Include <iostream> # include <cmath> using namespace STD; void find_smallest_exponent_by_brute_force (long number) {long unsigned exponent = 7; string first; while (number) {first. append (1, '0' + Number % 10); Number/= 10;} string result = "821"; while (result. rfind (first )! = (String: size_type) (result. length ()-first. length () | result. length () <(2 * first. length () + 1) {int carry = 0; For (INT I = 0; I <result. length (); I ++) {carry = 2 * (result [I]-'0') + carry; Result [I] = '0' + carry % 10; carry = carry/10;} If (carry) result. append (1, '1'); exponent ++;} cout <exponent <Endl;} void find_smallest_exponent_by_log (long number) {int digits = 0; long original = number; while (original) {digits ++; original/= 10 ;}for (int K = (digits + 1); k ++) {long down = floor (log10 (number) + k)/log10 (2); long up = floor (log10 (number + 1) + k) /log10 (2); If (up> down) {cout <up <Endl; return ;}} int main (int ac, char * AV []) {long number; while (CIN> Number) find_smallest_exponent_by_log (number); Return 0 ;}

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.