118. Digital Root

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

Let f(n) be a sum of digits for positive integer n. If f(n) is one-digit number then it is a digital root for n and otherwise digital root of n is equal to digital root of f(n). For example, digital root of 987is 6. Your task is to find digital root for expression A1*A2*…*AN + A1*A2*…*AN-1 + … + A1*A+ A1.

Input

Input file consists of few test cases. There is K (1<=K<=5) in the first line of input. Each test case is a line. Positive integer number N is written on the first place of test case (N<=1000). After it there areN positive integer numbers (sequence A). Each of this numbers is non-negative and not more than 109.

Output

Write one line for every test case. On each line write digital root for given expression.

Sample Input

1
3 2 3 4

Sample Output

5
//started 21:52
#include<cstdio>
#include <cstring>
using namespace std;
const int maxn=1000;
int a[maxn],n;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",a+i);
int ans=0;
for(int i=n-1;i>=0;i--){
ans+=1;
ans*=(a[i]%9);
ans%=9;
}
if(ans!=0)printf("%d\n",ans);
else {
for(int i=0;i<n;i++){
if(a[i]!=0){
puts("9");
break;
}
if(i=n-1)puts("0");
}
}
}
return 0;
}
//first ok 21:57
//first wa 22:02 假设爆int

  

快速切题 sgu118. Digital Root 秦九韶公式的更多相关文章

  1. Digital root(数根)

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

  2. 数字根(digital root)

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

  3. digital root问题

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

  4. 【BZOJ】3751: [NOIP2014]解方程【秦九韶公式】【大整数取模技巧】

    3751: [NOIP2014]解方程 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4856  Solved: 983[Submit][Status ...

  5. Digital Root 的推导

    背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...

  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. 1. 数字根(Digital Root)

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

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

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

随机推荐

  1. Python3基础 __repr__ 实例对象的名字,可以显示信息

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. 移动距离|2015年蓝桥杯B组题解析第八题-fishers

    移动距离 X星球居民小区的楼房全是一样的,并且按矩阵样式排列.其楼房的编号为1,2,3... 当排满一行时,从下一行相邻的楼往反方向排号. 比如:当小区排号宽度为6时,开始情形如下: 1 2 3 4 ...

  3. RHEL7--linux系统启动流程与故障排除

    一.Linux启动过程 MBR保存着系统的主引导程序(grub 446字节,分区表64字节),启动过程就是把内核加载到内存. 启动的顺序: 1.BIOS: 2.BIOS激活MBR: 3.MBR中的引导 ...

  4. C#学习笔记(十):函数和参数

    函数 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...

  5. go 异常处理

    package main import "fmt" func main() { defer func() { if err := recover(); err != nil { f ...

  6. python pandas demo

    1. import pandas as pd web_stats = {,,,,,], ,,,,,], ,,,,,]} df = pd.DataFrame(web_stats) print(df.he ...

  7. cookie(2)

    转载,原文地址 https://segmentfault.com/a/1190000004743454 一.引言 随着浏览器的处理能力不断增强,越来越多的网站开始考虑将数据存储在「客户端」,那就不得不 ...

  8. Qt5_加载DLL

    1.QLibrary 加载普通 DLL //写清楚库的路径,如果放在当前工程的目录下,路径为./Oracle.so QLibrary *libOCI = new QLibrary("F:\\ ...

  9. 数据库使用SSIS进行数据清洗教程

    OLTP系统的后端关系数据库用于存储不同种类的数据,理论上来讲,数据库中每一列的值都有其所代表的特定含义,数据也应该在存入数据库之前进行规范化处理,比如说“age”列,用于存储人的年龄,设置的数据类型 ...

  10. WPF几种高级绑定

    (1)Binding  + RelativeSource + AncestorType 模式  , 根据关联源所指定的类型,可动态绑定指定类型的Path属性(Path可以省略)(PS:动态指父级在运行 ...