首页 > 代码库 > poj1019

poj1019

说是组合数学但是根本不是嘛╮(╯▽╰)╭
讲连续的1234.。。看成一组那么我们可以大概得出m组(m*m/2 < 2147483647),那开到100000稳稳的
然后减掉最后一组之前的那些组的sum_of_lenth值问题就转变成了一个形如123456789101112.。。的第n个字符.

技术分享
#include<stdio.h>#include<algorithm>#include<iostream>using namespace std;const long long int maxa = 100005;long long int a[maxa], sum[maxa];int main(){    //freopen("in.cpp", "r", stdin);    long long int l = 1;    long long int m = 1;    a[1] = 1;    sum[1] = 1;    for(long long int i = 2; i < maxa; i++){        if(m*10 == i){            m = m*10;            l++;        }        a[i] = a[i-1]+l;        sum[i] = sum[i-1]+a[i];    }    long long int n, t;    cin>>t;    while(t--){        cin>>n;        long long int m;        for(long long int i = 0; i < maxa; i++){            if(sum[i] < n){                m = i;            }else{                n -= sum[m];                break;            }        }//printf("*%d\n", n);        long long int l = 1;        m = 1;        for(long long int i = 1; i < maxa; i++){            if(m*10 == i){                m *= 10;                l ++;            }//printf("++%d\n", n);            if(n - l <= 0){m = i;break;}            else n -= l;        }        long long int u = l -n+1;//printf("*%d %d\n", m, u);        while(u){            u--;            if(!u){                printf("%d\n", m%10);            }            m/=10;        }    }}
View Code

 

poj1019