题目:

A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...Sk. Each group Sk consists of a sequence of positive integer numbers ranging from 1 to k, written one after another. 
For example, the first 80 digits of the sequence are as follows: 
11212312341234512345612345671234567812345678912345678910123456789101112345678910

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by one line for each test case. The line for a test case contains the single integer i (1 ≤ i ≤ 2147483647)

Output

There should be one output line per test case containing the digit located in the position i.

Sample Input

2
8
3

Sample Output

2
2

题意分析:

把这些数,根据构造它们的那个数划分成 1; 1,2; 1,2,3;……

然后分析一个数字的位数是怎么确定的,其实,因为数是十进制,可以直接取以10为底的对数+1.

$log_{10}(N)+1$

在写程序的时候,一定要注意N要用double类型。然后我们可以递推求出所有可能的小于MAXN的数,最后一组数就是31268,这个数可以通过写个测试程序测试一下就出来了。

然后就可以确定输入的数字在整个序列中的哪个数字组中,接下来就是确定在这个数字组中的那个数中。

根据序列递增,然后利用求位数的公式,可以直接找到这个数。然后根据N的具体位置,取余取出来即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const LL MAXN = 2147483647;
const int Msize = 32000;
LL Sum[Msize], a[Msize]; void getS()
{
Sum[1] = a[1] = 1;
int i;
for(i = 2; i < Msize; i++)
{
a[i] = a[i-1] + (int)log10((double)i)+1;
Sum[i] = Sum[i-1] + a[i];
}
return;
} LL Pow(LL a, LL b)
{
LL ans = 1;
while(b)
{
if(b&1)
ans = ans*a;
b>>=1;
a = a*a;
}
return ans;
} int main()
{
getS();
LL N;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%I64d", &N);
int i = 1, j, len, pos;
while(N > Sum[i])
{
i++;
}
//第N位再i-1所代表的数字区
pos = N - Sum[i-1];
len = 0;
for(j = 1; len < pos ; j++)
{
len += (int)log10(j*1.0) + 1;
}
//N为在i-1数字区中的第j-1个数中,len为这个数字的最后一位的位置
int ans = (j-1)/Pow(10, len-pos)%10;
printf("%d\n", ans); }
return 0;
}

  

POJ_1019 Number Sequence 【递推】的更多相关文章

  1. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

  2. hdu-5496 Beauty of Sequence(递推)

    题目链接: Beauty of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java ...

  3. HDU 5950 Recursive sequence 递推转矩阵

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  4. hdu 5860 Death Sequence(递推+脑洞)

    Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...

  5. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

  6. luogu P1216 [IOI1994][USACO1.5]数字三角形 Number Triangles (递推)

    链接:https://www.luogu.org/problemnew/show/P1216 题面: 题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的 ...

  7. bzoj 2656 [Zjoi2012]数列(sequence) 递推+高精度

    2656: [Zjoi2012]数列(sequence) Time Limit: 2 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Descri ...

  8. hdu 4055 Number String(递推DP)

    给一个只含‘I','D','?'三种字符的字符串,I表示当前数字大于前面的数字,D表示当前的数字小于前面一位的数字,?表示当前位既可以小于又可以大于. 问1~n的排列中有多少个满足该字符串. http ...

  9. 递推:Number Sequence(mod找规律)

    解题心得: 1.对于数据很大,很可怕,不可能用常规手段算出最后的值在进行mod的时候,可以思考找规律. 2.找规律时不必用手算(我傻,用手算了好久).直接先找前100项进行mod打一个表出来,直接看就 ...

随机推荐

  1. ./run.sh --indir examples/demo/ --outdir examples/results/ --vis

    (AlphaPose20180911) luo@luo-ThinkPad-W540:AlphaPose$ ./run.sh --indir examples/demo/ --outdir exampl ...

  2. PHP 5.5环境配置

    php5.5 + apache2.4 安装配置 1 2 3 4 5 6 7 分步阅读 php5.5 做了大量的更新,在与apache搭配的时候如何选择也很有讲究,这里我们以64位 php5.6 和 A ...

  3. Linux 与 BSD

    1)Linux 与 BSD 有什么不同? http://linux.cn/article-3186-1.html 2)BSD(Unix)家族 http://blog.csdn.net/cradmin/ ...

  4. xgboost 并行调参

    Parallelism When Cross Validating XGBoost Models This raises the question as to how cross validation ...

  5. Flask解决跨域

    Flask解决跨域 问题:网页上(client)有一个ajax请求,Flask sever是直接返回 jsonify. 然后ajax就报错:No 'Access-Control-Allow-Origi ...

  6. hadoop2.2分布式环境搭建

    hadoop2.2的分布式环境需要配置的参数更多.但是需要安装的系统软件和单节点环境是一样的. 运行hadoop在非安全环境 hadoop的配置文件有两类: 1:只读的默认配置文件: core-def ...

  7. 类操作,removeClass&addClass

    // 添加类 function addClass(node,className){                 var reg=new RegExp("\\b"+classNa ...

  8. 类的 where T : class 泛型类型约束

    where T : struct | T必须是一个结构类型where T : class T必须是一个类(class)类型where T : new() | T必须要有一个无参构造函数where T ...

  9. win8 附件数据库失败解决方案《1》

    sql server 2005附加数据库错误:尝试打开或创建物理文件 无法打开物理文件 "E:\works\database\northwnd\northwnd.mdf".操作系统 ...

  10. CentOS 系统管理与yum软件仓库搭建

    重启 reboot shutdown -r now init 6 关闭 init 0 shutdown -h now shutdown -h 20:25 #8点25关机查看内存 free CPU利用率 ...