cf 1216e1

https://codeforc.es/problemset/problem/1216/E1

 

求1121231234...序列里面第k个数字,k不超过10亿。 

我们只要预处理一个sum数组,然后每次二分一下(其实不二分也可以) 

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int const inf = 1e9;
 4 int q, sum[100000], pw[10];
 5 int num(int x)
 6 {
 7     int res = 0;
 8     while (x)
 9     {
10         x /= 10;
11         res++;
12     }
13     return res;
14 }
15 int query(int t, int x)
16 {
17     while (1)
18     {
19         x--;
20         if (x == 0)
21             return t % 10;
22         t /= 10;
23     }
24 }
25 int main()
26 {
27     scanf("%d", &q);
28     int s = 0, n;
29     pw[0] = 1;
30     for (int i = 1; i < 10; i++)
31         pw[i] = pw[i - 1] * 10;
32     for (int i = 1;; i++)
33     {
34         s += num(i);
35         sum[i] = sum[i - 1] + s;
36         if (sum[i] >= inf)
37         {
38             n = i;
39             break;
40         }
41     }
42     while (q--)
43     {
44         int x;
45         scanf("%d", &x);
46         int l = 1, r = n;
47         while (l < r)
48         {
49             int mid = (l + r) / 2;
50             if (sum[mid] >= x)
51                 r = mid;
52             else
53                 l = mid + 1;
54         }
55         x -= sum[r - 1];
56         for (int i = 1; i < 10; i++)
57         {
58             int tmp = (pw[i] - pw[i - 1]) * i;
59             if (x > tmp)
60                 x -= tmp;
61             else
62             {
63                 int t = pw[i - 1] + (x - 1) / i;
64                 x = (x - 1) % i + 1;
65                 printf("%d\n", query(t, i - x + 1));
66                 break;
67             }
68         }
69     }
70     return 0;
71 }
View Code

 

posted @ 2019-09-27 22:44  zjxxcn  阅读(172)  评论(0编辑  收藏  举报