码迷,mamicode.com
首页 > 其他好文 > 详细

poj1019

时间:2014-06-13 15:32:12      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:class   code   com   string   cti   name   

给定数列:1121231234...

求:某一位对应的数值

打表+二分

******************************

#include<cstdio>
#include<cstring>
#include<cmath>
#define MAXN 111111
#define P 31268
#define N 2147483647
using namespace std;

int a[10];
long long s[MAXN],sum[MAXN];


int cal(int x)
{
    return (int)log10(x)+1;
}

void init()
{
    int i=1;
    long long cur=0;
    while(cur<N)
    {
        s[i]=s[i-1]+cal(i);
        cur=sum[i]=sum[i-1]+s[i];
        i++;
    }
}

int find(long long x)
{
    int l=1,r=P;
    while(l<=r)
    {
        int m=(l+r)>>1;
        if(sum[m]>x)r=m-1;
        else if(sum[m]==x)return m-1;
        else l=m+1;
    }
    return l-1;
}

int find1(long long x)
{
    int l=1,r=P;
    while(l<=r)
    {
        int m=(l+r)>>1;
        if(s[m]>x)r=m-1;
        else if(s[m]==x)return m-1;
        else l=m+1;
    }
    return l-1;
}

int main()
{
    //freopen("in.txt","r",stdin);

    init();

    int t;
    scanf("%d",&t);

    while(t--)
    {
        long long n;
        scanf("%lld",&n);

        int p=find(n);
        n-=sum[p];

        p=find1(n);
        n-=s[p];

        p++;
        int tot=0;
        while(p)
        {
            a[++tot]=p%10;
            p/=10;
        }
        int ans=a[tot+1-n];
        printf("%d\n",ans);
    }
    return 0;
}

******************************

poj1019,布布扣,bubuko.com

poj1019

标签:class   code   com   string   cti   name   

原文地址:http://www.cnblogs.com/MobileRobot/p/3784763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!