数学 - 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 ...
随机推荐
- SQL Server 性能调优(方法论)【转】
目录 确定思路 wait event的基本troubleshooting 虚拟文件信息(virtual file Statistics) 性能指标 执行计划缓冲的使用 总结 性能调优很难有一个固定的理 ...
- GNU C中的零长度数组
http://blog.csdn.net/ssdsafsdsd/article/details/8234736 在标准C和C++中,长度为0的数组是被禁止使用的.不过在GNU C中,存在一个非常奇怪的 ...
- Spring 事务传播行为
事务传播行为 指定是Spring中一个事务方法调用另一个事务方法时.处理的行为 使用方式: @Transactional(propagation=Propagation.REQUIRED) 事务的使用 ...
- Windows 开发之VC++垃圾清理程序软件
概述 本程序软件的主要实现垃圾文件清理的功能,即对指定的文件格式的临时文件或垃圾文件进行遍历.扫描.显示.删除清理等功能.在程序界面设计方面,对默认对话框重新自定义绘制,主要包括标题栏的重绘.对话框边 ...
- android中checkbox的padding引发的问题
自己定义checkbox中的勾选框图标.这次由于想偷懒.图标弄的大了些.然后一系列的问题就都引出来了. 1.图标比checkbox的layout_height高.看不见了. 非常吐血吧,Compoun ...
- 关闭危害的端口DOS命令(转载)
rem ipseccmd -w REG -p "HFUT_SECU" -r "Block UDP/137" -f *+0:137:UDP -n BLOCK -x ...
- FPGA设计经验谈 —— 10年FPGA开发经验的工程师肺腑之言
FPGA设计经验谈 —— 10年FPGA开发经验的工程师肺腑之言 2014年08月08日 14:08 看门狗 关键词: FPGA 作者:friends 从大学时代第一次接触FPGA至今已有10多 ...
- Android 网络下载图片
2中方法: 1. public byte[] downloadResource(Context context, String url) throws ClientProtocolException, ...
- 56. Two Sum【easy】
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 高性能爬虫——asynicio模块
一 背景知识 爬虫的本质就是一个socket客户端与服务端的通信过程,如果我们有多个url待爬取,只用一个线程且采用串行的方式执行,那只能等待爬取一个结束后才能继续下一个,效率会非常低. 需要强调 ...