Question:
Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n. For example, f(13) = 6. Notice that f(1) = 1. What's the next largest n such that f(n) = n?
As always, start with simple cases:
f(1) = 1,
f(2) = 1,
.......
f(9) = 1,
f(10) = 2, since 10 starts with 1, which we need to add.
f(11) = 4, since there are two 1's in 11 that we need to add.
f(12) = 5,
f(13) = 6,
....
f(19) = 12,
So between 12 and 19, we need to add 1 for each number because the second digit is one. Moreover, between 10 and 19, we add 10 1's from the second digit and one 1 from the last digit.
f(20) = 12,
f(21) = 13,
f(22) = 13,
.....
f(29) = 13,
So between 20 and 29, there is only one 1 from the last digit. Moreover, this is true for all subsequent intervals 30-39, 40-49, ......, 90-99. Consequently,
f(99) = 1 (between 1 and 9)
+ 1 (from the last digit in 11)
+ 10 (from the second digit in 10, 11, ......19)
+ 1 (in 31)
+ 1 (in 41)
+ ............
+ 1 (in 91)
= 10 (from second digit) + 10 (from first digit)
= 20.
In general, we can deduct
Lemma 1.
f(10^k - 1) = k * 10^(k-1)
for example,
f(9) = 1
f(99) = 2 * 10 = 20,
......
This is deducted from counting 1's in each digit for all number from 1 to 10^k -1, there are 10^(k-1) 1's in each of k digits. Actually, 1 appears in the last digit exactly once in every 10 consecutive numbers(like in 1, 11, 21, 31, ....) and so we have 10^k/10=10^(k-1) 1's contributed from last digit. Similarly, 1 appears 10 times in the second digit from last for every 100 consecutive numbers(like in 10-19, 110-119, 210-219, ...) and so we have 10 * (10^k/100) = 10^(k-1).
From this lemma, we can deduct
Lemma 2.
f(10^k) = k *10^(k-1) + 1.
The extra 1 is from the leading 1 in 10^k.
Recall the above process counting 1's between 10-99, we can deduct
Lemma 3.
f(m * 10^k) = m * f(10^k - 1) + 10^k = m * k * 10^(k - 1) + 10^k, for 1 < m < 9.
The second equality is using Lemma 1. This is just extending Lemma 1 to the cases where leading digits are bigger than 1.
The counting is just done by breaking the range between 1 and m * 10^k to two ranges, one is from 10^k to 2 * 10^k for leading 1's, and the rest. For example, f(5000) is breaking into 1-999, 1000-1999, 2000-2999, 3000-3999, and 4000-4999. Ignore the leading 1's, each range contributes f(10^k - 1). The leading 1's can only come from the second range 1000-1999, which has exactly 10^k leading 1's.
分享到:
相关推荐
根据给定的信息,我们可以推断出这是一篇关于Google招聘测试的文章,具体是关于Google实验室能力倾向测试(GLAT)的介绍与解析。虽然提供的内容片段并不完整,但基于现有信息,我们可以对提及的每一道题目进行详细...
- **背景介绍**:2004年10月,Google在几本专业杂志上发布了名为“Google实验室能力倾向测试”(GLAT)的一份特殊招聘试题。这份试题在全球范围内引起了广泛的关注,不仅因为它出自全球领先的科技公司之一Google,还...
该星表集包括的数据有:main.dat主星表、annex1.dat双星和聚星星表。 依巴谷主星表的字段如下: * HIC:输入星表标识符 * Comp:星体类型标识 * Target:目标星体标识 * RAhms:赤经 * DE-:赤纬 * DEdms:赤纬 * ...
下载后解开压缩包,里面有五个文件:glut.h,glut.lib,glut32.lib,glut.dll,glut32.dll。 然后把.h文件放到VC的include路径下的GL文件夹下,VC++6.0版本对应的文件夹是安装路径下VC98\Include\GL。...
2. **纬度和经度处理**:`input_glat`和`input_glong`函数分别用于获取用户输入的纬度和经度,以度分秒的形式。纬度范围限制在0°至60°之间,经度可以是负值,表示西经。 3. **时间计算**:`t_century`函数将从...
idl代码与Matlab 辉光 Global airglOW模型,可以从Fortran 2003编译器中独立且轻松地进行访问。 可选提供脚本语言,包括: Python≥3.6 Matlab的 GNU八度≥4.2 IDL ...glat , glon , Q , Echar , Nb
接收 xyz 格式的点云,然后将其转换为具有纬度向量、经度向量、glat/glon 网格(使用 meshgrid)和 az(高度)矩阵的 MATLAB 样式的网格。 然后将这些保存到指定的输出 .mat 文件中。
它的构建允许任何人检查志愿者为数据库中或任何 GLON、GLAT 位置的任何主题(图像)创建的分类。 对于每个图像,您都可以看到原始志愿者绘图、聚类结果和该地区 SIMBAD 上的对象(这一点还没有完全完成)。 例如...