Codeforces Round #587 (Div. 3) E

http://codeforces.com/contest/1216/problem/E1

E2. Numerical Sequence (hard version)

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≤10^18) — 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.

思路:

 

1,可以先二分查找,寻找k在哪个数字的列中。

2,再进行一次二分查找,寻找k具体在哪一个数字。

3,在该数字中找到k。

有:

1,n的范围,假设每个数字的长度都为1(例如10,11长度为1),则(n+1)*n/2<=k;即n<√K。

2,长度为len的数字的个数有10^len-10^(len-1)。

可以确定一个数组sum[ i ],表示到i这个数(包括i)的总长度,str[ i ]表示第i个数的长度。例如:sum[ 5 ]=15,str[ 5 ]=5;

可以得出:

sum[ X(是10^i-1由特殊到一般) ] = sum[ 10^(i-1)-1 ] + i*cnt*(cnt+1)/2 + l * cnt;(i为当前数的位数,cnt为位数为i的数的个数,l 为 str[ 10^(i-1)-1 ])

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
ll gets(long long mm,int opt)
{
    ll sum=0,cnt,add=0,len=0,exam=1;
    while(1)
    {
        exam*=10;
        len++;
        if(mm>exam-1)
        {
            cnt=exam-exam/10;
            if(opt)
            {
                sum+=add*cnt+cnt*(cnt+1)/2*len;
                add+=cnt*len;
            }
            else
            {
                sum+=cnt*len;
            }
        }
        else
        {
            cnt=mm-exam/10+1;
            if(opt)
            {
                sum+=add*cnt+cnt*(cnt+1)/2*len;
                add+=cnt*len;
            }
            else
            {
                sum+=cnt*len;
            }
            break;
        }
    }
    return sum;
}
int main()
{
    int q;
    ll k;
    ll ans=0;
    scanf("%d",&q);
    while(q--)
    {
        ans=0;
        scanf("%lld",&k);
        ll l=0,r=1e9;
        while(l<=r)
        {
            ll mid=(l+r)>>1;
            if(gets(mid,1)<k)
            {
                l=mid+1;
                ans=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        k-=gets(ans,1);
        l=0;
        r=ans+1;
        while(l<=r)
        {
            ll mid=(l+r)>>1;
            if(gets(mid,0)<k)
            {
                l=mid+1;
                ans=mid;
            }
            else
            {
                r=mid-1;
            }
        }
        k-=gets(ans,0);
        ans+=1;
        //printf("ans=%lld,k=%lld\n",ans,k);
        cout<<to_string(ans)[k-1]<<endl;
    }
}
View Code

 

转载于:https://www.cnblogs.com/switch-waht/p/11567568.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值