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的更多相关文章

  1. SGU 118.Digital root

    时间限制:0.25s 空间限制:4M 题目大意 给出n个数,求n1+n1*n2+n1*n2*n3+n1...nn 的数根,数根是一个数各个位置数字和的树根,个位数的数根为它本身. 例如,f[987]= ...

  2. 快速切题 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 ...

  3. Codeforces Beta Round #10 C. Digital Root 数学

    C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...

  4. Digital root(数根)

    关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...

  5. 数字根(digital root)

    来源:LeetCode 258  Add Dights Question:Given a non-negative integer  num , repeatedly add all its digi ...

  6. 【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+..+ ...

  7. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  8. digital root问题

    问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  9. 1. 数字根(Digital Root)

    数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...

随机推荐

  1. 【Project Euler 8】Largest product in a series

    题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...

  2. python的加密模块(md5,sha,crypt)学习

    python的加密模块(md5,sha,crypt)学习 命令行使用python MD5: yinguicai@Cpl-IBP-Product:~/data/work/svn/v1.4.0_dev/A ...

  3. javascript 的继承实例

    shape.prototype = { getEdge:function(){ return this.edge; }, getArea:function(){ return this.a*this. ...

  4. 移动通信安全——GSM安全体系

    一.GSM网络概述 1.安全机制 认证用户,防止未授权接入 对空中接口传输加密,防止无线信道上用户信息被窃听 SIM卡独立于终端,管理用户信息 在空中接口上以临时身份标识用户,防止用户被跟踪 但是GS ...

  5. iOS 获得通讯录中联系人的所有属性--b

    ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfA ...

  6. MVC之Ajax.BeginForm使用详解之更新列表 mvc验证jquery.unobtrusive-ajax

    MVC之Ajax.BeginForm使用详解之更新列表   1.首先,请在配置文件设置如下:(该项默认都存在且为true) <add key="UnobtrusiveJavaScrip ...

  7. 编译安装PHP7并安装Redis扩展Swoole扩展(未实验)

    用PECL自动安装Redis扩展.Swoole扩展 pecl install redis pecl install swool 编译安装PHP7并安装Redis扩展Swoole扩展 在编译php7的机 ...

  8. 大数据学习之Scala中main函数的分析以及基本规则(2)

    一.main函数的分析 首先来看我们在上一节最后看到的这个程序,我们先来简单的分析一下.有助于后面的学习 object HelloScala { def main(args: Array[String ...

  9. pow(x,y)函数的实现算法(递归函数)

    函数pow(x,y)实现运算x^y,即x的y次方,这里x和y都为整数. 算法的基本思想是,减少乘法次数,重复利用结算结果,例如: x^4,如果逐个相乘的话,需要四次乘法.如果我们这样分解(x^2)*( ...

  10. vue-tree

    vue-tree vue 编写的树形菜单,小巧实用,支持vue1.0,vue2.0 v1.0 功能: 1.支持多级树目录 2.支持高亮点击的节点 3.支持展开点击节点 4.支持点击收缩节点时收缩所有子 ...