POJ1019 -- Number Sequence (large Number processing)

Source: Internet
Author: User
Tags integer numbers

Number Sequence


Description
A single positive integer I is given. write a program to find the digit located in the position I in the sequence of number groups S1S2... sk. each group Sk consists of a sequence of positive integer numbers ranging from 1 to k, written one after another.
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by one line for each test case. the line for a test case contains the single integer I (1 ≤ I ≤2147483647)
Output
There shoshould be one output line per test case containing the digit located in the position I.
Sample Input
2
8
3
Sample Output
2
2

Question:

Given a string, the structure is as follows:

1121231234... 123456789101112... 12345678910111213... N

Ask the number of I-bits in the string.

Solution:

Divides a string into N segments.

1 12 123 1234... 1234567891011... 123456789101112... N

Then for the length of the K segment len [k] = len [k-1] + K the length of the number. Len [k] = len [k-1] + log10 (k) + 1.

Then define sum [k] = sum [k-1] + len [k]. You can locate the field where I is located by comparing the size of sum [] and I.

Suppose I is in the k field (1234567... t... k), then I-sum [k-1] represents the position of I in the k field.

The formula log10 (j) + 1 can be used to determine the t where I is located and the position pos in t.

Ans = t/pow (10, pos) % 10.

Code:

1 /************************************** * *********************************** 2> File Name: poj1019.cpp 3> Author: Enumz 4> Mail: [email protected] 5> Created Time: 6 ******************************* **************************************** */7 8 # include <iostream> 9 # include <cstdio> 10 # include <cstdlib> 11 # include <string> 12 # include <cstring> 13 # include <list> 14 # include <queue> 15 # include <stack> 16 # include <map> 17 # include <set> 18 # include <algorithm> 19 # include <cmath> 20 # include <bitset> 21 # include <climits> 22 # define MAXN 4000023 using namespace std; 24 long len [MAXN], sum [MAXN]; 25 void init () 26 {27 for (int I = 1; I <MAXN; I ++) 28 {29 len [I] = len [i-1] + (int) log10 (double) I) + 1; 30 sum [I] = sum [i-1] + len [I]; 31} 32} 33 int solve (int N) 34 {35 int I = 1; 36 while (sum [I] <N) 37 I ++; 38 N-= sum [i-1]; 39 int len_k = 0, t; 40 for (t = 1; len_k <N; t ++) 41 len_k + = (int) log10 (double) t) + 1; 42 int pos = len_k-N; 43 return (T-1)/(int) pow (double) 10, pos) % 10; 44} 45 int main () 46 {47 int T; 48 cin> T; 49 int N; 50 init (); 51 while (T --) 52 {53 cin> N; 54 cout <solve (N) <endl; 55} 56 return 0; 57}

 

POJ1019 -- Number Sequence (large Number processing)

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.