无穷数
时限:1s内存:32M
★问题描述:
我们生成两个无穷大的数,第一个数是把所有的自然数链接起来组成的数字;第二个数是把所有自然数的平方连接起来组成的数。对这两个数求和,如下:
123456789101112131415161718192021...
+ 149162536496481100121144169196225...
= 272619325597593231536305887388246...
现在给你一个整数k,问和从左往右数第k位的数码是多少?
★数据输入:
输入数据有多组,每组数据输入一行,有一个数k。对于100%的数据,k<=2147483647
★结果输出:
对于每组数据,输出一个整数N,从左往右数第k位的数码。
输入示例 |
输出示例 |
5 6 7 8 |
1 9 3 2 |
const a:array[1..19,1..2] of int64=((1,3),(4,9),(10,31),(32,99),(100,316),(317,999),(1000,3162),(3163,9999),(10000,31622),(31623,99999),(100000,316227),(316228,999999),(1000000,3162277),(3162278,9999999),(10000000,31622776),(31622777,99999999), (100000000,316227766),(316227767,999999999),(1000000000,2147483647)); var k:longint; function search_ai(k:longint):longint; var i,j,d,k2,g:int64; begin d:=1;i:=9; while (true) do begin if (k-d*i>0) then begin dec(k,d*i); i:=i*10;inc(d); end else break; end; k2:=i div 9+(k-1) div d; g:=(k-1) mod d+1; g:=d-g+1; while (g>1) do begin k2:=k2 div 10;dec(g); end; exit(k2 mod 10); end; { function search_bi(k:longint):longint; var i,j:int64; head:longint; begin head:=1;j:=10; i:=1; while (i<=2147483647 ) do begin if ((i*i) div j>0) then begin write('(',head,',',i-1,')',','); head:=i; j:=j*10; end; inc(i); end; end; } function search_bi(k:longint):longint; var i:longint; g,k2:int64; begin i:=1; while (k>i*(a[i,2]-a[i,1]+1)) do begin dec(k,i*(a[i,2]-a[i,1]+1)); inc(i); end; k2:=(k-1) div i+1; k2:=a[i,1]-1+k2; k2:=k2*k2; g:=(k-1) mod i+1; g:=i-g+1; while (g>1) do begin k2:=k2 div 10;dec(g); end; exit(k2 mod 10); end; function search_fi(k:longint):longint; var i:longint; begin i:=search_ai(k)+search_bi(k); if (i<=8) then exit(0) else if (i>9) then exit(1) else exit(search_fi(k+1)); end; begin // assign(output,'dabiao.out'); // rewrite(output); while not eof do begin readln(k); writeln((search_bi(k)+search_ai(k)+search_fi(k+1)) mod 10); end; // search_bi(1); //close(output); end.