题目:

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. Oracle树查询,start with connect by prior 递归查询用法(转载)

    本人觉得这个写的真不错,实用性强,就转载过来了 这个子句主要是用于B树结构类型的数据递归查询,给出B树结构类型中的任意一个结点,遍历其最终父结点或者子结点. 先看原始数据: 1 create tabl ...

  2. 转-使用wifi调试程序

    转自:http://www.cnblogs.com/sunzhenxing19860608/archive/2011/07/14/2106492.html 数据线丢了,不想花钱去买,在网上看了看,an ...

  3. SQL Server 索引维护:系统常见的索引问题

    在很多系统中,比如本人目前管理的数据库,索引经常被滥用,甚至使用DTA(数据库引擎优化顾问)来成批创建索引(DTA目前个人认为它的真正用处应该是在发现缺失的统计信息,在以前的项目中,用过一次DTA,里 ...

  4. 公共技术点之 Java 反射 Reflection

    本文摘录地址: http://codekk.com/open-source-project-analysis/detail/Android/Mr.Simple/%E5%85%AC%E5%85%B1%E ...

  5. 453. Minimum Moves to Equal Array Elements 一次改2个数,变成统一的

    [抄题]: Given a non-empty integer array of size n, find the minimum number of moves required to make a ...

  6. Docker学习笔记_安装和使用nginx

    一.软件环境 1.宿主机OS:Win10 64位 2.虚拟机OS:Ubuntu 18.04,虚拟机IP:192.168.8.25 3.Docker安装在虚拟机Ubuntu 18.04上 二.安装过程 ...

  7. dataframe 用法总结

    http://pda.readthedocs.io/en/latest/chp5.html data = [] 列表初始化 data = (,) data = {} 字典初始化 data = pd.D ...

  8. SQLAlchemy 进阶

    SQLAlchemy使用 1.执行原生SQL语句 from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engin ...

  9. 第七课 ROS的空间描述和变换

    在命令行工具中也有一个与transformcaster相类似的工具叫做static_transform_publisher,它能够接受命令行参数来接受位置信息.旋转信息.父框架.子框架以及周期信息,通 ...

  10. 利用 Aspose.Words 组件,在不依赖与 Office 组件的情况下把 Word 文件转换成 HTML 代码。

    首先利用 Nuget 获取 Aspose.Words.dll public ActionResult AsposeWordsDemo() { string srcFileName = Server.M ...