-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10427.cpp
50 lines (47 loc) · 991 Bytes
/
P10427.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int digits(long x) {
int ret = 1;
while(x > 9) {
x /= 10;
++ret;
}
return ret;
}
long startingIndexOfNumber(long n) {
int digitsInN = digits(n);
long ret = 0;
long firstOfLength = 1;
for(int length = 1; length < digitsInN; ++length) {
long ofThisLength = 9*firstOfLength;
ret += length*ofThisLength;
firstOfLength*=10;
}
ret += digitsInN*(n-firstOfLength);
return ret;
}
void printDigit(long digit, long N) {
int len = digits(N);
for(int i = 0; i < len-1-digit; ++i) {
N/=10;
}
cout << N%10 << endl;
}
int main() {
long N;
while(cin >> N) {
// Binary search:
long low = 1;
long high = N+1;
while(low+1 < high) {
long mid = (low+high)/2;
long idxMid = startingIndexOfNumber(mid);
if(idxMid >= N)
high = mid;
else
low = mid;
}
long idx = startingIndexOfNumber(low);
//cerr << N << " => " << low << " starting at " << idx << endl;
printDigit(N-1-idx, low);
}
return 0;
}