数学 - SGU 118. Digital Root
Digital Root
Problem's Link
Mean:
定义f(n)为n各位数字之和,如果n是各位数,则n个数根是f(n),否则为f(n)的数根.
现在给出n个Ai,求出A1*A2*…*AN + A1*A2*…*AN-1 + … + A1*A2 + A1 这个式子的数根.
analyse:
这道题目要用到这个规律,设f(n)是n的digital root,那么f(A*N)=f(A*f(N));
具体证明过程如下:
设自然数N=a[n]a[n-1]…a[0],其中a[0],a[1]、…、a[n]分别是个位、十位、…上的数字
再设M=a[0]+a[1]+…+a[n]
求证:N≡M(mod 9).
证明:
∵ N=a[n]a[n-1]…a[0]=a[n]*10^n+a[n-1]*10^(n-1)+…+a[1]*10+a[0].
又∵ 1≡1(mod 9),
10≡1(mod 9),
10^2≡1(mod 9),
…
10^n≡1(mod 9).
上面这些同余式两边分别同乘以a[0]、a[1]、a[2]、…、a[n],再相加得:
a[0]+a[1]*10+…+a[n]*10^n≡(a[0]+a[1]+…+a[n])(mod 9),
即 N≡M(mod 9),得证。
有了这个性质就容易解决本题了
在计算过程中,可以不断mod 9,因为我们知道有这样两个性质:
(A+B)mod C = ((A mod C) + (B mod C))mod C
(AB)mod C = ((A mod C)×(B mod C)) mod C
还要注意,如果余数为0,则输出9.
Time complexity: O(N)
view code
) ;
)
;
;
)
) );
;
}
数学 - SGU 118. Digital Root的更多相关文章
- SGU 118.Digital root
时间限制:0.25s 空间限制:4M 题目大意 给出n个数,求n1+n1*n2+n1*n2*n3+n1...nn 的数根,数根是一个数各个位置数字和的树根,个位数的数根为它本身. 例如,f[987]= ...
- 快速切题 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 ...
- 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 ...
- digital root问题
问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 1. 数字根(Digital Root)
数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...
随机推荐
- ormlite操作表记录
实体类的创建对应表中people表, @DatabaseTable(tableName="people") public class People { @DatabaseField ...
- python import 错误 TypeError: 'module' object is not callable
python import 错误 TypeError: 'module' object is not callable 在这里,有 Person.py test.py; 在 test.py 里面 im ...
- 微信小程序 - 输入起点、终点获取距离并且进行路线规划(腾讯地图)
更新: 2018-9-19 腾讯官方经纬度转详细地址,详细地址转经纬度 index.wxml <!--地图容器--> <map id="myMap" style= ...
- UNIX网络编程读书笔记:recvmsg和sendmsg函数
这两个函数是最通用的I/O函数.实际上我们可以把所有read.readv.recv和recvfrom调用替换成recvmsg调用.类似地,各种输出函数调用也可以替换成sendmsg调用. #inclu ...
- Spring事务属性具体解释
Spring.是一个Java开源框架,是为了解决企业应用程序开发复杂性由Rod Johnson创建的.框架的主要优势之中的一个就是其分层架构,分层架构同意使用者选择使用哪一个组件,同一时候为 J2EE ...
- LoadRunner中运行场景时提示"You do not have a license for this Vuser type."
LoadRunner中运行场景时提示"You do not have a license for this Vuser type." 2012-06-15 17:09:07| 分 ...
- springmvc sitemesh json问题
参考: 解决方法: <sitemesh> <mapping path="/*" decorator="/WEB-INF/views/template/t ...
- Android应用开发揭秘之优化技术
2013-06-28 第15章 优化技术 不管用什么语言进行开发,所有的优秀代码都会展示出共有的经典品质: 简练,可读性强,模块化,层次性,设计良好,高效,优雅,清晰等. Java程序员能够依 ...
- EL和自定义函数库
问题:在数据显示的时候经常需要调用一些方法对需要显示的数据进行基本的处理,如: 数据过滤.求子串等操作.那么就需要使用EL表达式进行快速的函数调用. 引入HTMLFilter.java类 描述为函数 ...
- .Net Excel 导出图表Demo(柱状图,多标签页) .net工具类 分享一个简单的随机分红包的实现方式
.Net Excel 导出图表Demo(柱状图,多标签页) 1 使用插件名称Epplus,多个Sheet页数据应用,Demo为柱状图(Epplus支持多种图表) 2 Epplus 的安装和引用 新建一 ...