E1 - Numerical Sequence (easy version) 《 Codeforces Round #587 (Div. 3) 》

 

 

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 (1q5001≤q≤500) — the number of queries.

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

Output

Print qq lines. In the ii-th line print one digit xixi (0xi9)(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.

 

 

 

#include <bits/stdc++.h>
 
using namespace std;
 
#define pb push_back
#define ff first
#define ss second
 
typedef long long ll;
typedef pair<int, int> pii;
 
const int MAXN = 2 * 100 * 1000 + 17;
 
ll q, a[MAXN];
 
int main() {
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    for (int i = 1; i < MAXN; i++)
        a[i] = a[i - 1] + log10(i) + 1;
    cin >> q;
    while (q--) {
        int k;
        cin >> k;
        int x = 1;
        while (k > a[x])
            k -= a[x], x++;
        int t = upper_bound(a + 1, a + MAXN, k) - a;
        k -= a[t - 1];
        string s;
        s += to_string(t - 1).back();
        s += to_string(t);
        cout << s[k] << '\n';
    }
    return 0;
}

 

#include <bits/stdc++.h>
using namespace std;
// Prioridade
typedef long long   ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for(int i = a; i < (int)(b); i++)
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define all(x) x.begin(),x.end()
#define MOD 1000000007
#define endl '\n'
#define mdc(a, b) (__gcd((a), (b)))
#define mmc(a, b) (((a)/__gcd(a, b)) * b)
#define W(x) cerr << "\033[31m"<<  #x << " = " << x << "\033[0m" << endl;
// fim da Prioridade
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int q;
    cin >> q;
    while(q--){
        ll k;
        cin >> k;
        ll x = k;
        ll ans = 0;
        REP(i,1,x+10000){
            ll logg = 0;
            ll p = i;
            while(p != 0LL) p/=10LL, logg++;
            ans += logg;
            //cout << ans << endl;
            if(k <= ans){
                x = 1;
                string s;
                while((int)s.size() < k){
                    s += to_string(x);
                    x++;
                }
                cout << s[k-1] << endl;
                break;
            }else{
                k -= ans;
            }
        }
    }
    
    return 0;
}

 

二分

#include<bits/stdc++.h>
#define mid ((l+r)/2)
using namespace std;
long long sum[50010],l[50010];
long long len(int i){
    int re=0;
    while(i){
        re++;
        i/=10;
    }
    return re;
}
long long n,k=1,t;
char z[5000100];
void strint(long long n,char re[]){
    int le=0;
    char te[6];
    while(n){
        te[le++]=n%10;
        n/=10;
    }
    for(int i=0;i<le;i++)
        re[i]=te[le-i-1]+'0';
    k+=le;
}
int main(){
    for(int i=1;i<=50000;i++){
        l[i]=l[i-1]+len(i);
        sum[i]=sum[i-1]+l[i];
    }
    int num=1;
    while(k<5000000){
        strint(num,z+k);
//        printf("%d",num);
        num++;
    }
    z[1]='1';
//    printf("\n");
//    for(int i=1;i<=5000000;i++)
//        printf("%c",z[i]);
//    return 0;
    
//    for(int i=1;i<1000;i++)
//        printf("%d ",sum[i]);
//    return 0;
    
    scanf("%d",&n);
    while(n--){
        scanf("%d",&t);
        int l=0,r=50000;
        while(l<r-1){
            if(sum[mid]<t)
                l=mid;
            else
                r=mid;
        }
        t-=sum[l];
        printf("%c\n",z[t]);
    }
    return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/Shallow-dream/p/11581072.html