Description
题目链接
给定一串有规律的数:11212312341234512345612345671234567812345678912345678910123456789101112345678910,问从左往右数第 $i$ 个数字是多少?
$1\leq i \leq 2147483647$
Solution
直接把这串数字分组:$1,12,123,1234,12345,123456\dots$
暴力打表求出每一组的数字的个数,对于每一个询问,只要先扫一遍,判断出是哪一组哪一个数字即可。
Code
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
| #include<algorithm> #include<cstdio> #include<cmath> #include<vector> #include<cstring> #include<iostream> #include<queue> #define W while #define I inline #define gc getchar #define D isdigit(c=gc()) #define Cn const #define pc(c) putchar((c)) #define int long long using namespace std; I int read(){int x=0,f=1;char c;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);return x*f;} I void write(int x){x<0&&(pc('-'),x=-x),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} const int N=100010,sz=35000; int T,n,a[sz+10],sum[sz+10],pw[20]; I void init(){a[1]=sum[1]=pw[0]=1;for(int i=1;i<=11;i++) pw[i]=pw[i-1]*10;for(int i=2;i<=sz;i++) a[i]=a[i-1]+log10(i)+1,sum[i]=sum[i-1]+a[i];} signed main(){ init();T=read();W(T--){ n=read();register int i=1;W(sum[i]<n) i++;--i; int p=n-sum[i],j=0;for(i=1;j<p;i++) j+=log10(i)+1; write((i-1)/pw[j-p]%10),pc('\n'); }return 0; }
|