sum记录从1到1 12 123 。。。123...i 的总位数
然后用二分搜k是在1到几的序列中然后再遍历一遍就行
AC代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
long long sum[50000];
int init_sum(){
long long pre = 1;
sum[0] = 0;
sum[1] = 1;
for( int i = 2; i < 50000; i++ ){
pre += log10((double)i) + 1;
sum[i] = sum[i-1] + pre;
}
return 0;
}
long long find( long long N ){
long long l = 1, r = 50000, mid;
while( l <= r ){
mid = ( l + r ) / 2;
if( sum[mid] <= N ){
l = mid + 1;
}else{
r = mid - 1;
}
}
return l - 1;
}
int main(){
init_sum();
long long T, N;
cin >> T;
while( T-- ){
cin >> N;
long long M = find( N );
if( N == sum[M] ){
cout << M % 10 << endl;
}else{
N -= sum[M];
long long i;
for( i = 1; i <= M + 1; i++ ){
int temp = log10((double)i) + 1;
if( N <= temp ){
break;
}
N -= temp;
}
long long temptot = 0;
long long tempnum[10000];
long long k = i;
while( k ){
tempnum[temptot++] = k % 10;
k /= 10;
}
cout << tempnum[temptot-N] << endl;
}
}
return 0;
}