CF1216E1 Numerical Sequence (easy version)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_40758751/article/details/101202831

E1. Numerical Sequence (easy version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

The only difference between the easy and the hard versions is the maximum value of kk.

You are given an infinite sequence of form "112123123412345……" which consist of blocks of all consecutive positive integers written one after another. The first block consists of all numbers from 11 to 11, the second one — from 11 to 22, the third one — from 11 to 33, ……, the ii-th block consists of all numbers from 11 to ii.

So the first 5656 elements of the sequence are "11212312341234512345612345671234567812345678912345678910". Elements of the sequence are numbered from one. For example, the 11-st element of the sequence is 11, the 33-rd element of the sequence is 22, the 2020-th element of the sequence is 55, the 3838-th element is 22, the 5656-th element of the sequence is 00.

Your task is to answer qq independent queries. In the ii-th query you are given one integer kiki. Calculate the digit at the position kiki of the sequence.

Input

The first line of the input contains one integer qq (1≤q≤5001≤q≤500) — the number of queries.

The ii-th of the following qq lines contains one integer kiki (1≤ki≤109)(1≤ki≤109) — the description of the corresponding query.

Output

Print qq lines. In the ii-th line print one digit xixi (0≤xi≤9)(0≤xi≤9) — the answer to the query ii, i.e. xixi should be equal to the element at the position kiki of the sequence.

Examples

input

Copy

5
1
3
20
38
56

output

Copy

1
2
5
2
0

input

Copy

4
2132
506
999999999
1000000000

output

Copy

8
2
9
8

Note

Answers on queries from the first example are described in the problem statement.

思路:

首先暴力打了个表,将用完1位的序号,用完2位的序号,用完3位的序号....都打出来,然后根据n进行分类讨论。用二分查找在当前位下,已经填充到多少了,最后暴力求解答案即可。

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int maxn = 2e5+50;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 1e9+7;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
int gcd(int x, int y){
    return x%y==0? y : gcd(y, x%y);
}
int arr[maxn];
int ttll[10] = {0, 9,189,2889,38889,488889};
int main()
{
    //freopen("out.txt", "w", stdout);
//    freopen("out.txt", "r", stdin);
//    string t;
//    cin >> t;
//    cout << t.size() << endl;
//    cout << 9*(1+9)/2 << endl;
//    cout << 90*(1+90)+90*(9*(1+9)/2) << endl;
//    ll temp = 0;
//    ll base = 1;
//    ll tail = 0;
//    rep(i, 1, 5){
//        //cout << 9*base*(1+9*base)/2*i + 9*base*tail + temp << endl;
//        temp = 9*base*(1+9*base)/2*i + 9*base*tail + temp;
//        ttll[i] = tail = tail + 9*base*i;
//        cout << ttll[i] << ",";
//        base *= 10;
//    }
//    rep(i, 1, 999){
//        rep(j, 1, i){
//            cout << j;
//        }
//    }
    ll T, n;
    cin >> T;
    while(T--){
        cin >> n;
        if(n > 189414495){//5bit
            n -= 189414495;
            ll l = 0, r = 90000;
            while(l != r){
                ll md = (l+r)/2;
                ll num = md*(1+md)/2*5+md*ttll[4];
                if(n > num) l = md+1;
                else
                    r = md;
            }
            l--;
            n -= l*(1+l)/2*5 + l*ttll[4];
            if(n <= 9){
                cout << n << endl;
            }else if(n <= 189){
                n -= 9;
                rep(i, 10, 99){
                    if(n > 2){
                        n-=2;
                    }else{
                        if(n == 1){
                            cout << i/10%10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }else if(n <= 2889){
                n -= 189;
                rep(i, 100, 999){
                    if(n > 3){
                        n-=3;
                    }else{
                        if(n == 1){
                            cout << i/100%10 << endl;
                        }else if(n == 2){
                            cout << i/10%10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }else if(n <= 38889){
                n -= 2889;
                rep(i, 1000, 9999){
                    if(n > 4){
                        n-=4;
                    }else{
                        if(n == 1){
                            cout << i/1000%10 << endl;
                        }else if(n == 2){
                            cout << i/100%10 << endl;
                        }else if(n == 3){
                            cout << i/10%10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }else{
               n -= 38889;
                rep(i, 10000, 99999){
                    if(n > 5){
                        n-=5;
                    }else{
                        if(n == 1){
                            cout << i/10000%10 << endl;
                        }else if(n == 2){
                            cout << i/1000%10 << endl;
                        }else if(n == 3){
                            cout << i/100%10 << endl;
                        }else if(n == 4){
                            cout << i/10%10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }
        }else if(n > 1395495){//4bit
            n -= 1395495;
            ll l = 0, r = 9000;
            while(l != r){
                ll md = (l+r)/2;
                ll num = md*(1+md)/2*4+md*ttll[3];
                if(n > num) l = md+1;
                else
                    r = md;
            }
            l--;
            n -= l*(1+l)/2*4 + l*ttll[3];
            if(n <= 9){
                cout << n << endl;
            }else if(n <= 189){
                n -= 9;
                rep(i, 10, 99){
                    if(n > 2){
                        n-=2;
                    }else{
                        if(n == 1){
                            cout << i/10%10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }else if(n <= 2889){
                n -= 189;
                rep(i, 100, 999){
                    if(n > 3){
                        n-=3;
                    }else{
                        if(n == 1){
                            cout << i/100%10 << endl;
                        }else if(n == 2){
                            cout << i/10%10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }else{
                n -= 2889;
                rep(i, 1000, 9999){
                    if(n > 4){
                        n-=4;
                    }else{
                        if(n == 1){
                            cout << i/1000%10 << endl;
                        }else if(n == 2){
                            cout << i/100%10 << endl;
                        }else if(n == 3){
                            cout << i/10%10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }
        }else if(n > 9045){//3bit
            n -= 9045;
            ll l = 0, r = 900;
            while(l != r){
                ll md = (l+r)/2;
                ll num = md*(1+md)/2*3+md*ttll[2];
                if(n > num) l = md+1;
                else
                    r = md;
            }
            l--;
            n -= l*(1+l)/2*3 + l*ttll[2];
            if(n <= 9){
                cout << n << endl;
            }else if(n <= 189){
                n -= 9;
                rep(i, 10, 99){
                    if(n > 2){
                        n-=2;
                    }else{
                        if(n == 1){
                            cout << i/10%10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }else{
                n -= 189;
                rep(i, 100, 999){
                    if(n > 3){
                        n-=3;
                    }else{
                        if(n == 1){
                            cout << i/100%10 << endl;
                        }else if(n == 2){
                            cout << i/10%10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }
        }else if(n > 45){//2bit
            n -= 45;
            ll l = 0, r = 90;
            while(l != r){
                ll md = (l+r)/2;
                ll num = md*(1+md)/2*2+md*ttll[1];
                if(n > num) l = md+1;
                else
                    r = md;
            }
            l--;
            n -= l*(1+l)/2*2 + l*ttll[1];
            if(n <= 9){
                cout << n << endl;
            }else{
                n -= 9;
                rep(i, 10, 99){
                    if(n > 2){
                        n-=2;
                    }else{
                        if(n == 1){
                            cout << i/10 << endl;
                        }else{
                            cout << i%10 << endl;
                        }
                        break;
                    }
                }
            }
        }else{
            ll l = 0, r = 9;
            while(l != r){
                ll md = (l+r)/2;
                ll num = md*(1+md)/2+md*ttll[0];
                if(n > num) l = md+1;
                else
                    r = md;
            }
            l--;
            n -= l*(1+l)/2 + l*ttll[0];
            cout << n << endl;
        }
    }

    return 0;
}
/*
11212312341234512345612345671234567812345678912345678910
1395495

45
9045
1395495
189414495
23939649495
*/

猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/101202831