Fzu_noip 1032 infinite number (carry determination)

Source: Internet
Author: User

Infinite Duration: 1 s memory: 32 M★Problem description: we generate two infinite numbers. The first number is a number that links all natural numbers. The second number is a number that connects the square of all natural numbers. Sum the two numbers as follows: 123456789101112131415161718192021... + 149162536496481100121144169196225... = 272619325597593231536305887388246... now I will give you an integer k. What is the number of k digits from left to right?★Data input: there are multiple groups of input data. Each group of data is input in one row, with a k number. For 100% of data, k <= 2147483647★Result output: for each group of data, an integer N is output, which is the k-digit number from left to right. Input sample output example 56781932 first calculates the k-bit Ai and Bi, then adds them, and determines whether to overhead them. Next, use search_fi (k: longint) to determine whether the k-th digit will make the previous carry. Then, [delphi] const a: array [1 .. 19, 1 .. 2] of int64 = (100,316), (317,999 ), ), (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;} 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. infinite Duration: 1 s memory: 32 M★Problem description: we generate two infinite numbers. The first number is a number that links all natural numbers. The second number is a number that connects the square of all natural numbers. Sum the two numbers as follows: 123456789101112131415161718192021... + 149162536496481100121144169196225... = 272619325597593231536305887388246... now I will give you an integer k. What is the number of k digits from left to right?★Data input: there are multiple groups of input data. Each group of data is input in one row, with a k number. For 100% of data, k <= 2147483647★Result output: for each group of data, an integer N is output, which is the k-digit number from left to right. Input sample output example 56781932 first calculates the k-bit Ai and Bi, then adds them, and determines whether to overhead them. Next, use search_fi (k: longint) to determine whether the k-th digit will make the previous carry. Then, [delphi] const a: array [1 .. 19, 1 .. 2] of int64 = (100,316), (317,999 ), ), (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;} 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.