Uva10706-number sequence (query rule)

Source: Internet
Author: User

Question: uva10706-number sequence (query rule)


There is such a series of 11212312341234512345612345671234567812345678912345678910123456789101112345678910..., ask the value of the I position.

1 2 3 4 5 6 7...

Solution: the rule needs to be discovered. At the beginning, I still read the wrong question. The rule can be understood only after reading the answers of others.

Here we define s [I] to represent the position of the I-th sequence. For example, the numbers below the string sequence represent the sequence to which the string belongs. (Position 1, 3, and 6 ...)

Num [I] indicates the length of the I-th sequence. Note that the number is 10 characters long.

Recursive Formula: s [I] = s [I-1] + s [I-1]-s [I-2] + wi (number of digits of I ).

To find the position of the I sequence, we need to start from the position of the I-1 sequence + the length of the I sequence. S [I-1]-s [I-2]: The position of the I-1 sequence, minus the position of the I-2 sequence, is the length of the I-1 sequence, that is, num [I-1 ]. Then, as long as the length of I-1 is added to the position of the I-1 sequence, the length difference between the I sequence and the I-1 sequence is the number of I digits.

Here we need to pre-process the arrays S and num, and then find the corresponding values by traversing these two arrays. The size of the array can be increased to 1 E5, which is more than 2147483647.

To find the value at the position I, you must first find the sequence in which it is located. Traverse the S array and find s [k-1] <= I. Subtract I from S [k-1] (position of the k-1 sequence) it is the length of the I position in the K sequence.

Then the length of each sequence also follows the regular rule: sequence 3: 123 length 3

Sequence 4: 1234 length 4

The length difference between sequence I and sequence I-1 is actually the number of digits of sequence I, and it is incremental.

Find num [k-1]> = I <= num [k] In the num array, which means that the value of I is one of the K digits.


Code:

# Include <stdio. h> # include <math. h> typedef long ll; const ll n = 2147483647; const int W [] = {0, 10,100,100 0, 10000,100 000 }; // (0, 1, 2 ...) number of BITs dividing value const int M = 100000; ll s [m]; ll num [m]; // pre-processing void Init () {s [0] = num [0] = 0; s [1] = num [1] = 1; int cur = 1; for (INT I = 2; s [I-1] <n; I ++) {if (I> = W [cur]) cur ++; s [I] = 2 * s [I-1]-s [I-2] + cur; num [I] = s [I]-s [I-1] ;}} int main () {Init (); int t; ll N; scanf ("% d", & T); char STR [10]; while (t --) {scanf ("% LLD", & N); int I; for (I = 1; I <m; I ++) if (s [I]> = N) break; n-= s [I-1]; for (I = 1; I <m; I ++) if (Num [I]> = N) break; n-= num [I-1]; sprintf (STR, "% d", I); printf ("% C \ n", STR [n-1]);} 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.