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. Java查找算法之二分查找

    二分查找是一种查询效率非常高的查找算法.又称折半查找. 一.算法思想 有序的序列,每次都是以序列的中间位置的数来与待查找的关键字进行比较,每次缩小一半的查找范围,直到匹配成功. 一个情景:将表中间位置 ...

  2. 一种新的技术,C++/CLI

    一.来源 在一个项目中,拿到了一个demo,看起来像是C#,又像是C++,部分截图如下 1.界面[C#的winform] 2.mian入口,是cpp 3.解决方案 二.猜测 一开始以为是C#工程,因为 ...

  3. HDU1698 Just a Hook(线段树&区间覆盖)题解

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  4. 阿里云Linux服务器初探

    阿里云Linux服务器初探 阿里云Linux服务器初探 因为钱包的关系,本人买了一个660元2年的1核1GB的小服务器(centos是Linux的发行版),在当初是用2核4GB(内存)的时候使用的是w ...

  5. WebApi——json返回多了 k_BackingField

    产生原因: model类添加了    [System.Serializable] 解决方案: xxxxx.WebApi\App_Start\WebApiConfig.cs的Register函数中添加如 ...

  6. UVa 11729 突击战

    https://vjudge.net/problem/UVA-11729 题意:有n个部下,每个部下需要完成一项任务.第i个部下需要你话B分钟交代任务,然后立刻执行J分钟完成任务.安排交代任务顺序并计 ...

  7. [转]VS“当前上下文中不存在名称“ViewBag”解决方法

    原文链接:<VS"当前上下文中不存在名称"ViewBag",当前上下文不存在名称"model""-已解决> 自己的项目出现了错误 ...

  8. js获取url 参数

    window.getRequest = function (url) { var theRequest = new Object(); var indexOf = url.indexOf(" ...

  9. spring boot开发 静态资源加载不出来

    spring boot 1.5 版本之前 不拦截静态资源 springboot 2.x版本 拦截静态资源 private static final String[] CLASSPATH_RESOURC ...

  10. C#复制文件

    string pLocalFilePath ="";//要复制的文件路径 string pSaveFilePath ="";//指定存储的路径 if (File ...