無窮數 時限:1s內存:32M ★問題描述: 我們生成兩個無窮大的數,第一個數是把所有的自然數鏈接起來組成的數字;第二個數是把所有自然數的平方連接起來組成的數。對這兩個數求和,如下: 123456789101112131415161718192021... + 149162536496481100121144169196225... = 272619325597593231536305887388246... 現在給你一個整數k,問和從左往右數第k位的數碼是多少? ★數據輸入: 輸入數據有多組,每組數據輸入一行,有一個數k。對於100%的數據,k<=2147483647 ★結果輸出: 對於每組數據,輸出一個整數N,從左往右數第k位的數碼。 輸入示例 輸出示例 5 6 7 8 1 9 3 2 先算出第k位的Ai和Bi,然後相加,考慮是否加過頭。 接下來用search_fi(k:longint) 判斷第k位是否會令前一位進位,則 [delphi] 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. 無窮數 時限:1s內存:32M ★問題描述: 我們生成兩個無窮大的數,第一個數是把所有的自然數鏈接起來組成的數字;第二個數是把所有自然數的平方連接起來組成的數。對這兩個數求和,如下: 123456789101112131415161718192021... + 149162536496481100121144169196225... = 272619325597593231536305887388246... 現在給你一個整數k,問和從左往右數第k位的數碼是多少? ★數據輸入: 輸入數據有多組,每組數據輸入一行,有一個數k。對於100%的數據,k<=2147483647 ★結果輸出: 對於每組數據,輸出一個整數N,從左往右數第k位的數碼。 輸入示例 輸出示例 5 6 7 8 1 9 3 2 先算出第k位的Ai和Bi,然後相加,考慮是否加過頭。 接下來用search_fi(k:longint) 判斷第k位是否會令前一位進位,則 [delphi] 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.