hihoCoder #1349 Nature Numbers
题目大意
考虑自然数构成的序列 $a$:$01234567891011\dots$,序列下标从 $0$ 开始,即 $a_0 =0, a_1 = 1$ 。
求 $a_n$($0\le n\le 10^{18}$)。
解法
设 $a_n$ 所在的数字为 $x(n)$ 。
首先不难求出 $x(n)$ 的位数, 设其为 $k$ 。
从而可以求出 $x(n)$ 是第几个 $k$ 位数,这样也就求出了 $x(n)$ 。
设 $x(n)$ 是第 $i$($i\ge 1$)个 $k$ 位数,则有
$$ i = \left\lceil \frac{n+1 - s_{k-1}}{k}\right\rceil $$
$\lceil a/b \rceil$($a\ge 0, b>0$)用代码可表示为(a + b - 1) / b。
其中,$s_{k-1}$ 表示「位数不超过 $k-1$ 的自然数」的位数之和。
进一步,可以求出 $a_n$ 在 $x(n)$ 第几位。
比较方便的办法是,把个位作为第 0 位,十位作为第 1 位,百位作为第 2 位,以此类推;
这样,x 的第 j 位可以表示为 x / pow(10, j) % 10。
设 $a_n$ 在 $x(n)$ 的第 $j$ 位,则有
$$ j = i k - (n +1 - s_{k-1}) $$
hihoCoder #1349 Nature Numbers的更多相关文章
- hihoCoder 1595 : Numbers
Description You are given n constant integers c[1], c[2], ..., c[n] and an integer k. You are to ass ...
- hihoCoder 1432 : JiLi Number(吉利数)
hihoCoder #1432 : JiLi Number(吉利数) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 Driver Ji l ...
- [HihoCoder] Highway 高速公路问题
Description In the city, there is a one-way straight highway starts from the northern end, traverses ...
- The top 100 papers Nature explores the most-cited research of all time.
The top 100 papers Nature explores the most-cited research of all time. The discovery of high-temper ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- hihocoder 1873 ACM-ICPC北京赛区2018重现赛 D Frog and Portal
http://hihocoder.com/problemset/problem/1873 时间限制:1000ms 单点时限:1000ms 内存限制:512MB 描述 A small frog want ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
随机推荐
- Android(java)学习笔记117:SharedPreferences(轻量级存储类)
1.SharedPreferences是Android平台上一个轻量级的存储类,简单的说就是可以存储一些我们需要的变量信息.2个activity 之间的数据传递除了可以他通过intent来传递数据,还 ...
- Array - Merge Sorted Array
/** * 将nums2中的值合并入nums1,使其仍然有序 * 可以任务nums1的长度>=m+n * @param nums1 已排序数组 * @param m nums1数组已初始化的数目 ...
- ubuntu下安装eclipse<转>
转载自http://my.oschina.net/u/1407116/blog/227084 http://my.oschina.net/u/1407116/blog/227087 一 JD ...
- shell脚本,awk取奇数行与偶数行方法。
第一种方法: 第二种方法: 第三种方法:
- Shell脚本调用ftp上传文件
Shell脚本调用ftp上传文件 1.脚本如下 ftp -n<<! open x.x.x.x ###x.x.x.x为ftp地址 user username password ###user ...
- Linux curl命令中,HTTP 302处理
在Linux中使用curl命令时,偶尔会遇到一些URL跳转到新的URL,即HTTP中的3XX(redirection,重定向 ). $curl -s -I $URL > log 这时在返回的报文 ...
- 关键字final
final数据 对于一个final变量,如果是基本数据类型的变量,则其数值一旦在初始化之后便不能更改:如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象.再次赋值将引起编译报错. 当f ...
- CNCF 有哪些具体的项目内容?
前言:CNCF(Cloud Native Computing Foundation)于 2015 年 7 月成立,隶属于 Linux 基金会,初衷围绕“云原生”服务云计算,致力于维护和集成开源技术,支 ...
- MySQL中一条SQL的加锁分析
MySQL中一条SQL的加锁分析 id主键 + RC id唯一索引 + RC id非唯一索引 + RC id无索引 + RC id主键 + RR id唯一索引 + RR id非唯一索引 + RR id ...
- 【Python学习之四】递归与尾递归
看完廖雪峰老师的教程,感觉尾递归函数是一个相对难点.于是复习一下,思考了一下,发表一些见解,记录一下. 1.递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数 ...