首先,输入的i很大,如果想直接不断跑1 12 123这样上去直接找必定是超时的。
可以发现,一个数x,他12345……x一共有多少位我们是可以直接求出来的。所以我们每次直接减去这么多个数字即可。所以我们从1开始枚举x,每次i减去他的长度,直到i小于0,就说明答案就在12345……x中,直接暴力枚举即可得到答案。
那么这个x可能到多大呢?我们可以估算一下,假设x=1e5,直接用等差数列求和公式,那么sum至少是5e9 + 1e5。所以x最大不可能大于1e5,T只有15,这是完全够用的复杂度了。
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
int Count(int s) {
int ans = s, tmp = 10;
while(s >= tmp) {
ans += s - tmp + 1;
tmp *= 10;
}
return ans;
}
int Count2(int x) {
int cnt = 0;
while(x) {
x /= 10;
cnt++;
}
return cnt;
}
stack <int> sta;
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
int T = 10000;
int ans, a;
// int t[10005];
// for(int i = 0; i < 10005; i++)
// t[i] = i + 1;
scanf("%d", &T);
for(int k = 0; k < T; k++) {
scanf("%d", &a);
// a = t[k];
// printf("%d\n", a);
while(!sta.empty())
sta.pop();
int s = 1, cnt;
while(a > 0) {
cnt = Count(s);
a -= cnt;
s++;
}
a += cnt, s--;
for(int i = 1; ; i++) {
if(a <= 0) {
i--;
a += Count2(i);
while(i) {
sta.push(i % 10);
i /= 10;
}
while(a--) {
ans = sta.top();
sta.pop();
}
break;
}
a -= Count2(i);
}
printf("%d\n", ans);
}
return 0;
}