Description
序列 \(123456789101112131415161718192021222324252627282930313233343536...\) 是无穷无尽的,现在你要输出它的第 \(k\) 项。\(k \le 10^{12}\)
Solution
分步处理
- 找到答案所在数的位数
- 找到答案所在数在当前位数中排第几
- 找到答案在答案所在数是第几位
#include <bits/stdc++.h>
using namespace std;
#define int long long
int k;
signed main() {
cin>>k;
if(k<10) {
cout<<k;
return 0;
}
int len=1,cnt=9,pos=0;
while(len*cnt+pos < k) {
pos+=len*cnt;
++len;
cnt*=10;
}
k-=pos+1;
int val=k/len+pow(10,len-1);
cout<<(val/(int)pow(10,len-k%len-1))%10;
}