题目:

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. Mock Server实践

    转载自 https://tech.meituan.com/mock-server-in-action.html 背景 在美团服务端测试中,被测服务通常依赖于一系列的外部模块,被测服务与外部模块间通过R ...

  2. [docker]本地仓库的创建的使用

    如果自己创建的镜像可以供其他同事使用,那就可以大大节约开发时间成本,docker的本地仓库正好可以满足这样的需求 1.在仓库服务器上创建本地仓库 baylor@baylor-virtual-machi ...

  3. git之创建版本库

    好吧,你知道了git既然有这么多好处,那么我们接下来就可以使用git命令来创建我们的仓库.这一节我们来学习一下如何在windows下面建立自己的仓库.首先我们先在本地创建一个主目录,比如文件名字叫le ...

  4. ASP.NET框架获取数据字典数据做成树的格式

    private List<TreeEntity> treeList = new List<TreeEntity>();//创建一个树的List集合 public ActionR ...

  5. UIWebView分页显示

    問題:使用iOS UIWebView時,載入本地html檔案,但是該檔案太大,結果螢幕畫面形成一長條型顯示,雖然用滾動畫面可以看見整個html檔案,但是滑來滑去,不好用. 目標:用UIWebView載 ...

  6. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(7):装配SpringBean·依赖注入装配

    一.依赖注入的三种方式      在实际环境中实现IoC容器的方式主要分为两大类,一类是依赖查找,依赖查找是通过资源定位,把对应的资源查找回来.另一类则是依赖注入.一般而言,依赖注入可分为3中方式: ...

  7. spring的一些配置和重要的接口和类

    spring的配置文件 通常是applicationContext.xml(具体的bean配置会在后面内容中详解) setter方法注入: <property name=“” value=“ja ...

  8. screen工具

    1.背景 系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份.ftp 传输等等.通常情况下我们都是为每一个这样的任务开一个远 ...

  9. SQL 2005报错之Restore fail for Server 'DatabaseServerName'.

    Restore fail for Server 'DatabaseServerName'.(Microsoft.SqlServer.Smo) Additional information: Syste ...

  10. orleans 2.0 教程之-----官方文档翻译,给大家学习ol一个参考

    本人也是英文盲,翻译不对的地方请谅解.由于翻译内容较多,会慢慢更新 orleans简称ol,一些专用词不做翻译.先决条件,读这表文章之前需要了解:actor,es,cqrs 参考链接: https:/ ...