Ultraviolet A 10706-Number Sequence

Source: Internet
Author: User

There is an array s [1] = 1, s [2] = 1 2 ,....... s [k] = 1 .... k. a given n is required to represent the nth digit of the array and the nth digit is required. For example, when n is 1, when 1 n is 2, it is 1, and when n is 3, it is 2.

Solution: 1: Train of Thought: prepare tables and search
2: The data given by the question can discover some patterns
S [1]: 1 Total number of digits 1
S [2]: 1 2 Total digits 1 + 2 = 3
S [3]: 1 2 3 Total digits 1 + 2 + 3 = 6
.
.
.
S [9]: 1 2 3 4 5 6 7 8 9 total digits 1 + 2 + 3 +... + 9 = 45
S [10]: 1 2 3 4 5 6 7 8 9 10 Total digits 45 + 1 + 2 + 3 +... + 9 + 1 + 0 = 56
S [K]: 1 2 3 4... k total number of digits num [k-1] + 1 + 2 + ......
Use num [k] To save the total number of digits when the value is k.
Therefore, if we can obtain all the data in advance to form a table, then we can directly search for the data in the input position, and then find the position. Due to the increasing trend above, I can infer that when k to 100000, it will exceed int, So you can open such a large array.
3: During the table creation process, I used to enumerate the number of digits of the current value. For example, if the number of digits is 1, there are 9 digits in total from 1 to 9, if the number of digits is 2, there will be 10-99 Public 90 ....... assume that the current value is n, then num [n] is obtained based on the above rule, and so on.
4: search, O (n) time complexity, as long as find num [I-1] <n, num [I]> = n, then we can know that this number exists in s [n], subtract n minus num [I-1], we can know the number of the required number in s [n, then let's judge them one by one. Note that n may be multiple digits such as 1234, so you need to split it first, that is, the maximum bit.
5. Note: Because the n value of this question is 2147483647 at most, we need to open the num array as long, and some processing in the middle should be long, otherwise there will be a lack of precision.

Code:
[Cpp]
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <string>
# Include <vector>
# Include <cstdio>
# Include <stack>
# Include <queue>
# Include <cmath>
# Include <set>
Using namespace std;
# Define maxn100000
 
Int t, n;
Long num [MAXN]; // total number of values that are saved
 
// Initialize the table
Void init (){
Long I, j, k, tmp; // long note
Memset (num, 0, sizeof (num); // Initialization
For (I = 1; I <= 5; I ++) {// The maximum number of enumerative digits.
For (j = pow (10, I-1); j <pow (10, I); j ++ ){
Num [j] = num [J-1]; tmp = (j-pow (10, I-1) + 1) * I;
For (k = 1; k <I; k ++)
Tmp + = (pow (10, k)-pow (10, k-1) * k;
Num [j] + = tmp;
}
}
}
 
Void solve (){
Int I, j, k, pos, ans;
// Locate the pos position
For (I = 1; I <MAXN; I ++ ){
If (n> num [I-1] & n <= num [I]) {
Pos = I; break;
}
}
// Search
Int cnt = n-num [pos-1]; int sum = 0;
Int len, tmp, tmp_j;
For (j = 1; j <= pos; j ++ ){
Tmp_j = j;
For (I = tmp_j, len = 0; I! = 0; I/= 10) len ++;
For (k = len-1; k> = 0; k --){
Ans = tmp_j/pow (10, k); sum ++;
If (sum = cnt ){
Printf ("% d \ n", ans );
Return;
}
Tmp = pow (10, k); tmp_j % = tmp;
}
}
}
 
Int main (){
// Freopen ("input.txt", "r", stdin );
Init (); scanf ("% d", & t );
While (t --){
Scanf ("% d", & n); solve ();
}
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.