POJ 1019 Problem Solving report

Source: Internet
Author: User

Question address: http://poj.org/problem? Id = 1019

 

A common ACM question, but some problems can be seen from it. The question is very simple, that is, to give you a string of numbers, the rule is:

1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112 ······

Enter the position I to calculate the digit I of this string. Note that it is a number, not a number. For example, 12345678910 of 10th bits are 1, not 10, and 11th bits are 0, not 10.

 

Then we can easily say that the length of a numeric k is calculated using: int (log10 (double (num) + 1, and then the variable sk adds the length of k each time, set another sum and the variable sum. It adds the sk value each time until sum is greater than index. Then, find the number in sk Based on the remaining length, which is very simple.

 

There is a problem here: "The line for a test case contains the single integer I (1 ≤ I ≤ 2147483647)", according to The OJ convention, test cases may be extreme, extremely large or extremely small, so I = 2147483647 must be considered. If the processing is not good, timeout may occur. Unfortunately, I timed out.

 

A little strange, in line 48th of the Code, the comment said: If sum <index is used, it times out. Because sum <index is used in the while clause at the beginning, each time I determine whether the length of the current count is smaller than the Length index given in the question. However, it is a pity that I = 2147483647 is very slow. After inserting the computing time code, it is found that this step takes about 60 seconds.

 

Then I found other people's code on the internet, basically using the accumulate method, such:

Const int size = 31269;

Void play_table (void) {a [1] = s [1] = 1; for (int I = 2; I <size; I ++) {a [I] = a [I-1] + (int) log10 (double) I) + 1; // log10 (I) + 1 represents the number of digits in group I that are longer than the number in the I-1 group s [I] = s [I-1] + a [I]; // The length of the front I group s [I] is equal to the length of the front I-1 group s [I-1] + the length of the first I group a [I]} // log () is an overload function. You must forcibly convert int I to determine the parameter type return ;}

Although the array tabulation method is much faster in the case of a large number of test cases, the accumulation process is the same and the time difference is so large, I think, it may be in the judgment statement.

His judgment statement is "I <31269", while mine is "sum <2147483647". The problem may be that my smaller number is used to compare the numbers at both ends. It turns out that this is the case. After I accumulate conditions similar to him, the time consumed becomes 0 s, then I changed the while criterion from sum <index to index-sum> 0, and the time consumed was also 0 s (this 0s was tested on the local machine using the time function, for non-OJ running time, other functions are required to be precise to milliseconds as OJ does ).

 

Why is the time-consuming change so large as a small change? Maybe we have to consider it at the underlying layer. int variables take up 4 bytes, that is, 32bit, the two large 32-bit numbers are more time-consuming than the two smaller ones. It may be better understood by combining the knowledge in the Assembly, but unfortunately I have forgotten it...

The following is the complete code, passed on POJ, 268 K 16 MS. (If the table is typed, the memory usage will increase, but the time will be reduced, and the space will be used for the time. In this case, the table will not be typed)

 #include <iostream> #include <cmath> #include <fstream> #include <time.h>      size(       (log10((num))) + ;     digit( num,       length = size(num) -     (length--          num /=       num %=       num;                                                           cin >>     (t--          cin >>                      sum =          k =          sk =                       (index - sum > )               k++             sk +=             sum +=                                sum = index - sum +                       num =           length =          (length <              num++             length +=            length = sum - length +                               cout <<digit(num, length) <<        }

 

 

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.