【poj1019】 Number Sequence

http://poj.org/problem?id=1019 (题目链接)

题意:给出一个数:1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910(当然中间是没有空格的)求它从左往右第n位上是多少。

solution 
  水题一道。我们可以发现,这个数可以分成若干串数,记为i,那么每串数i就是从1~i。我们可以用数组x[i]来记录串i所占的空间也就是位数,数组sum[i]来记录串i的末尾位于整个串中的哪个位置。 
  对于每次输入的数n,我们先二分找到它位于哪个串中,之后再递归到当前串中处理。在这里我们发现x[i]=x[i-1]+log10(i)+1,所以就很好做了。

代码:

// poj1019
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<vector>
#define MOD 1000000007
#define inf 2147483640
#define LL long long
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
    LL x=0,f=1;char ch=getchar();
    while (ch>‘9‘ || ch<‘0‘) {if (ch==‘-‘) f=-1;ch=getchar();}
    while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}

LL x[1000000],sum[1000000];
vector<int> v;

int main() {
    int T;scanf("%d",&T);
    sum[0]=x[0]=0;
    for (int i=1;i<=9;i++) x[i]=x[i-1]+1;
    for (int i=10;i<=99;i++) x[i]=x[i-1]+2;
    for (int i=100;i<=999;i++) x[i]=x[i-1]+3;
    for (int i=1000;i<=9999;i++) x[i]=x[i-1]+4;
    for (int i=10000;i<=99999;i++) x[i]=x[i-1]+5;
    for (int i=1;i<=99999;i++) sum[i]=sum[i-1]+x[i];
    while (T--) {
        LL n;scanf("%lld",&n);
        int i=lower_bound(sum,sum+100000,n)-sum;
        n-=sum[i-1];
        i=lower_bound(x,x+i+1,n)-x;
        n-=x[i-1];
        v.clear();
        while (i) {
            v.push_back(i%10);
            i/=10;
        }
        reverse(v.begin(),v.end());
        printf("%d\n",v[n-1]);
    }
    return 0;
}

  

时间: 2024-08-05 11:12:11

【poj1019】 Number Sequence的相关文章

【KMP】Number Sequence

KMP算法 KMP的基处题目,数字数组的KMP算法应用. 主要是模式串的处理,当模式串内有重复时,模式串向左回溯重复的点的位置(next[]). Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a

【HDU 1005】Number Sequence —— 找周期

原题链接 Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 122213    Accepted Submission(s): 29653 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n

【POJ 1019】 Number Sequence

[POJ 1019] Number Sequence 二分水题 放组合数学里...可能有什么正规姿势吧Orz 112123123412345...这种串 分成长度1 2 3 4 5...的串 注意有多位数 把长度累加到一个数组里 注意要累加 因为查询的时候查的是原串中对应位置的数 因此要累加上前一次的长度 然后二分处该串前的总长 用查询的位置-之前串的总长 就是在最长的串中的位置 因此还要打个最长串的表 这些我都写一个循环里了 看着有点乱 可以拆开写... 代码如下: #include <ios

【BZOJ1367】[Baltic2004]sequence 左偏树

[BZOJ1367][Baltic2004]sequence Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Output 13 HINT 所求的Z序列为6,7,8,13,14,15,18.R=13 题解:详见论文 然而本题要求z[i]严格递增,那就让所有t[i]-=i就好了 #include <cstdio> #include <cstring> #include <iostrea

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

【LeetCode】Number of Islands 解题报告

[题目] Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounde

poj1019(Number Sequence)

题目地址:Number Sequence 题目大意: 有一串序列由阿拉伯数字够成11212312341234512345612345671234567812345678912345678910123456789101112345678910.......然后问你第几位数字是什么输出该数字. 解题思路: 排列组合.假如算第79位数字为多少,先计算出它在那个1-n的区间,可以看出79在1-12这段数字的区间,然后计算出前面1.1-2.1-3....1-11所有数的总和有多少位减去之后,再通过模拟在1

【转】oracle sequence

原文链接  http://www.cnblogs.com/hyzhou/archive/2012/04/12/2444158.html ORACLE SEQUENCE用法 在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没有关系. 1.Create Sequence 首先要有CREATE SEQUENCE或者CREATE ANY SEQUENCE权限. 创建语句如下: 复制代码 CREATE SEQUENCE seqTest INCREMENT BY 1

【POJ2778】DNA Sequence(AC自动机)

题意: 生物课上我们学到,DNA序列中只有A, C, T和G四种片段. 经科学发现,DNA序列中,包含某些片段会产生不好的基因,如片段"ATC"是不好片段,则"AGATCC", "CATCAA", "ATCATC"都是不好的DNA序列,这些不好片段我们可以称为病毒片段. 现在已知m个病毒片段, 问长度为n的DNA序列,有多少种可能不包含病毒片段.答案可能很大,取模 100000. [数据规模和约定] 0<=m<=1