题目:传送门
题目大意:有一个无限数串是由 S1,S2,S3,...,Sk,... 组成的,其中 Sk 代表 1,2,3,...,k ,给出一个索引值 i(1≤i≤2147483647) ,求此数串中第 i 个数字是什么。
分析:首先我们求出
s[k]={0,s[k−1]+(int)log10k+1,k=0k>0
然后再累加就可以得到 Sk 的起始位置 start[k] 了。剩下的细节具体看代码吧。
ps: i 的取值最大为
代码:
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
const int N=3e4+2000;
int s[N];
int x;
unsigned int start[N];
void calStart(){
s[0]=0;
start[0]=1;
for(int i=1;i<N;++i){
s[i]=s[i-1]+(int)log10(1.0*i)+1;
start[i]=start[i-1]+s[i-1];
}
}
void solve(){
int st;
for(st=1;st<N;++st){
if(start[st]>x) break;
}
st--;
__int64 sum=0;
for(int i=1;true;++i){
sum+=(int)log10(1.0*i)+1;
if(sum+start[st]-1==x){
cout<<i%10<<endl;
return ;
}
if(sum+start[st]-1>x){
int d=sum+start[st]-1-x;
while(d){
i/=10;
d--;
}
cout<<i%10<<endl;
return ;
}
}
}
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
calStart();
while(t--){
cin>>x;
solve();
}
return 0;
}