digital root问题
问题阐述会是这样的:
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?
这其实是一个digital root的问题。
digital root的定义如下:
The digital root (also repeated digital sum) of a non-negative integer is the (single digit) value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.
For example, the digital root of 65,536 is 7, because 6 + 5 + 5 + 3 + 6 = 25 and 2 + 5 = 7.
编程题中一般会要求在O(1)时间算出一个数的digital root,这时候就不能用上述思想解答问题了。
通用公式:
或者是

所有的原理其实和模运算以及同余定理有关:
考虑12345 = 1 × 10,000 + 2 × 1,000 + 3 × 100 + 4 × 10 + 5.
同时10 i= 9 + 1; 100 i= 99 + 1,所以又可以写成:
12,345 = 1 × (9,999 + 1) + 2 × (999 + 1) + 3 × (99 + 1) + 4 × (9 + 1) + 5.
展开后:
12,345 = (1 × 9,999 + 2 × 999 + 3 × 99 + 4 × 9) + (1 + 2 + 3 + 4 + 5).
这样便满足了数根的思想,计算数根的一次迭代,当然(1 + 2 + 3 + 4 + 5)=15 又可以接着迭代,总之是:

数根是模9的余数是因为
因此
这样便有
,也就有如下推论:

这里要强调为什么当数字是9的倍数时,dr(n)是9?
例如:18 = 10 + 9
$18 \equiv 0 \pmod{9}$
但 $10 + 9 \equiv 1 + 8 \pmod{9}$,莫着急,这只是表象, $1 + 8 = 9 \equiv 0 \pmod{9}$
所以9的倍数的数根也可以用(mod 9)运算,只不过由于数根只在1-9之间,所以为零时只要换成9即可,毕竟$9 \equiv 0 \pmod{9}$
至于
也是这个道理,数根只能在1-9之间,而(mod 9)的数域在0-8之间, 所以先对数字减1然后再补1即可折中等效了。
关键点是理解为什么由
$a = b + c$
$b \equiv r_1 \pmod{9} $
$c \equiv r_2 \pmod{9} $
可推导出:
$a \equiv r_1 + r_2\pmod{9}$
提示:把数写成 $n = mq + r $,依据一条推论:
推论 a≡b(mod m)的充要条件是a=mt+b(t为整数)。
表示对模m同余关系的式子叫做模m的同余式,简称同余。
参考资料:
A NEAT NUMBER TRICK: DIGITAL ROOTS AND MODULO-9 ARITHMETIC
digital root问题的更多相关文章
- Digital root(数根)
关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...
- 数字根(digital root)
来源:LeetCode 258 Add Dights Question:Given a non-negative integer num , repeatedly add all its digi ...
- 【HDOJ】4351 Digital root
digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ ...
- Sum of Digits / Digital Root
Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...
- 1. 数字根(Digital Root)
数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...
- 快速切题 sgu118. Digital Root 秦九韶公式
118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...
- Codeforces Beta Round #10 C. Digital Root 数学
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...
- 数学 - SGU 118. Digital Root
Digital Root Problem's Link Mean: 定义f(n)为n各位数字之和,如果n是各位数,则n个数根是f(n),否则为f(n)的数根. 现在给出n个Ai,求出A1*A2*…*A ...
- 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...
随机推荐
- jquery 判断多组radio checkbox是否选中
最近要做一个问卷调查的小页面,需要判断用户是否每项都有选择,如果每个都挨个判断很苦逼,所以网上搜了搜,自己也总结了一下,写了一段小代码~哈哈,水平有限大家见谅.html代码就不上了,N多单选和多选框就 ...
- 深入浅析mysql引擎
mysql引擎 mysql数据库引擎取决于mysql在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译mysql.在缺省情况下,mysql支持三个引擎:ISAM,MYISAM和HEAP.另 ...
- python cmd模块练习
# encoding=utf-8 import cmd import sys # cmd模块练习 class Client(cmd.Cmd): ''' 1)cmdloop():类似与Tkinter的m ...
- 一个简化的printf函数
<C和指针>第7章第5道编程题: 实现一个简化的printf函数,它能够处理%d.%f.%s 和 %c 格式码,根据ANSI标准的原则,其他格式码的行为是未定义的.你可以假定已经存在函数 ...
- 【Robot Framework 介绍】总纲
Robot Framework是一个由python构建的的开源的自动化测试框架,现在版本还在不停的更新中.由于它开源性,网上有大量的第三方接口和很多资料.下面提供两个比较官方的链接,有兴趣的同学可以直 ...
- Oracle监控指标
1.数据文件或数据设备 参考:http://f.dataguru.cn/thread-106901-1-1.html2.数据库日志空间活或回滚段(包括大小.设备.文件及可用率.日志空间竞争情况或回滚段 ...
- 键盘有没有NKRO ?微软帮你测
玩家甚至媒体的解读是错的,所以小编在此重点说明一些概念.并分享如何测试.在许多游戏与软体中都会使用组合键功能,也就是同时按下特定几个按键之后就能触发特别的功能,简单的说就是一些动作的快捷键.不过,有时 ...
- QT GUI(主)线程与子线程之间的通信——使用跨线程的信号槽
在主线程上,可以控制子线程启动,停止,清零 如果子线程启动的话,每一秒钟会向主线程发送一个数字,让主线程更新界面上的数字. 程序截图: 上代码: #include <QtGui> #inc ...
- 使用myeclipse生成的HibernateSessionFactory的合理性
作为简单的一个单件模式, getSessionFactory()函数返回null是一个不合理的决定,可以返回一个没有初始化的SessionFactory对象,但是返回null就和这种模式的语意有冲突了 ...
- git 之别名配置
在git操作中有很多命令我们自己可以起别名,以提高操作效率. 1. 配置方式 1)项目级别的配置,仅对当前项目生效(将写入到.git/config文件中) $ git config --glob ...