Find the nth digit (two-point search)

Source: Internet
Author: User
Tags cmath

Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=1597Find the nth digit

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 9517 Accepted Submission (s): 2757


Problem description hypothesis:
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
Now we're going to connect all the strings together.
S = 1121231234.......123456789123456789112345678912 .....
So can you tell me what the nth number is in the s string?

Input inputs are first a number k and represent a K-inquiry.
The next K line has an integer n (1 <= n < 2^31) per line.

Output for each n, the number of the nth corresponding to the outputs S.

Sample Input61234510

Sample Output112124

AUTHOR8600 Solution: You can use two decomposition, you can also use two equation formula solutions, pay attention to using long Long Binary lookup code:
1#include <cstdio>2#include <cstring>3#include <cmath>4 using namespacestd;5 #definell Long Long6 ll F (ll X)7 {8ll ans = x* (x+1)/2 ;9     returnans;Ten } One intMain () A { -     intT; -scanf"%d",&T); the      for(inti =0; i < T; i++) -     { - ll N; -scanf"%lld",&n); +         if(n==1) -         { +Puts"1"); A             Continue; at         } -ll L =1; -ll r =N; -ll mid = (l+r)/2; -         intTM =0; -          while(r-l>1) in         { -             if(f (Mid) <N) to             { +L =mid; -Mid = (l+r)/2; the             } *             Else if(f (Mid) >N) $             {Panax NotoginsengR =mid; -Mid = (l+r)/2; the             } +             Else if(f (mid) = =N) A             { theTM =mid; +                  Break; -             } $         } $         if(tm!=0) -         { -             if(tm%9==0) puts ("9"); the             Else  -printf"%d\n", tm%9);Wuyi         } the         Else  -         { WuLL flag =l; -ll tt = flag* (flag+1)/2; About             intsum = ntt;  $             if(sum%9==0) puts ("9"); -             Else  -printf"%d\n", sum%9); -         } A     } +     return 0; the}

Formula Code:

1#include <cstdio>2#include <cmath>3 using namespacestd;4 #definell Long Long5 #defineEPS 1e-86 7 intMain ()8 {9     intT;Tenscanf"%d",&T); One      for(inti =0; I < t;i++) A     { -         DoubleN; -scanf"%LF",&n); the         Doublem = (sqrt (1+8*n)-1)/2; -ll mm =m; -LL TM = mm* (mm+1)/2; -ll flag = NTM; +         if(Flag = =0){if(mm%9==0) printf ("9\n");Elseprintf"%lld\n", mm%9);} -         Else +         { A             if(flag%9==0) printf ("9\n"); at             Else  -printf"%lld\n", flag%9); -         } -     } -     return 0 ; -}

Find the nth digit (two-point search)

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.