@@ -27,7 +27,7 @@ the group. For instance:
27
27
$\rightarrow3\cdot900=2700$ digits total
28
28
- And so on...
29
29
30
- To find which group the number that contains the $k$- th digit is in, we can
30
+ To find which group the number that contains the $k$th digit is in, we can
31
31
iterate through groups until the sum of the total number of digits from all the
32
32
groups we've processed becomes greater than or equal to $k$. In other words, we
33
33
continue increasing the number of groups we look at (starting from $1$-digit
37
37
\sum^{\text{\# of groups}}_{n=1}{n\cdot9\cdot10^{n-1}\geq k}
38
38
$$
39
39
40
- Once the condition becomes true, we know that the $k$- th digit must fall into
41
- the last group used in the sum. We can now calculate the $k$- th digit's position
40
+ Once the condition becomes true, we know that the $k$th digit must fall into
41
+ the last group used in the sum. We can now calculate the $k$th digit's position
42
42
in the group by subtracting $k$ by the total number of digits from all groups
43
43
before it. We'll call this relative position $j$:
44
44
48
48
49
49
Note that we subtract $1$ from the result in order to make $j$ $0$-based (which
50
50
makes it more convenient to solve the rest of the problem). For example, if
51
- $j=0$, that would mean the $k$- th digit is the first digit in the group. If
52
- $j=100$, that would mean the $k$- th digit is the $99$-th digit in the group,
51
+ $j=0$, that would mean the $k$th digit is the first digit in the group. If
52
+ $j=100$, that would mean the $k$th digit is the $99$-th digit in the group,
53
53
etc. From there, we can divide $j$ by $n$ (the length of the numbers in the
54
- group), which gives us the position of the number that contains the $k$- th digit
54
+ group), which gives us the position of the number that contains the $k$th digit
55
55
within the group (also $0$-based). Similarly as before, a position of $0$ means
56
56
that the number is the first number in the group, and a position of $100$ means
57
57
that the number is the $99$-th number in the group.
58
58
59
- To calculate the exact number the $k$- th digit is in, we can add the position of
59
+ To calculate the exact number the $k$th digit is in, we can add the position of
60
60
the number within the group (which we just found) to the value of the first
61
61
number of the group, which is just $10^{ n - 1 } $ (you can find this pattern from the
62
62
beginning example). Now, all that's left is finding the position of the $k$-th
@@ -79,10 +79,10 @@ position that's $1$ plus a multiple of $3$, meaning its position modulo $3$
79
79
equals $1$. Finally, the third digit in each number is always $2$ plus a
80
80
multiple of $3$, meaning that its position modulo $3$ equals $2$. This pattern
81
81
extends for numbers of all sizes, and it is thus why we can use the modulo
82
- operator to determine the $k$- th digit's $0$-based position within the number
82
+ operator to determine the $k$th digit's $0$-based position within the number
83
83
it's inside.
84
84
85
- Now, we can calculate our answer by converting the number that the $k$- th digit
85
+ Now, we can calculate our answer by converting the number that the $k$th digit
86
86
is in into a string and indexing it at the position we've just found.
87
87
88
88
## Implementation
@@ -111,7 +111,7 @@ int main() {
111
111
ll k ;
112
112
cin >> q ;
113
113
114
- for (int i = 0 ; i < q ; i ++ ) {
114
+ for (int i = 0 ; i < q ; i ++ ) {
115
115
cin >> k;
116
116
/*
117
117
* Subtract k by sizes of groups until k becomes smaller than the size
0 commit comments