快速切题 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 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*A2 + 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 秦九韶公式的更多相关文章
- Digital root(数根)
关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...
- 数字根(digital root)
来源:LeetCode 258 Add Dights Question:Given a non-negative integer num , repeatedly add all its digi ...
- digital root问题
问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 【BZOJ】3751: [NOIP2014]解方程【秦九韶公式】【大整数取模技巧】
3751: [NOIP2014]解方程 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4856 Solved: 983[Submit][Status ...
- Digital Root 的推导
背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...
- 【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 ...
- 1. 数字根(Digital Root)
数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...
- Codeforces Beta Round #10 C. Digital Root 数学
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...
随机推荐
- noip2008 真题练习 2017.2.25
不是有很多可以说的,记住不能边算边取min Code #include<iostream> #include<fstream> #include<sstream> ...
- Entity Framework 6 和 MVC5
网站地址: Entity Framework 6 http://msdn.microsoft.com/en-us/data/ef.aspx MVC5 http://www.asp.net ...
- NSwag enum
https://github.com/RSuter/NJsonSchema/wiki/JsonSchemaGenerator#integer-vs-string-enumerations Intege ...
- 第八章 对称加密算法--AES
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第7章“初等加密算法--对称加密算法” 8.1.AES 特点: 密钥建立时间短.灵敏性好.内存需求低(不管怎样,反正就是好) ...
- [bzoj 1774][Usaco2009 Dec]Toll 过路费
题目描述 跟所有人一样,农夫约翰以着宁教我负天下牛,休叫天下牛负我的伟大精神,日日夜夜苦思生 财之道.为了发财,他设置了一系列的规章制度,使得任何一只奶牛在农场中的道路行走,都 要向农夫约翰上交过路费 ...
- DPDK无法分出大页面:EAL: No free hugepages reported in hugepages-2048kB 解决方法
参考: [dpdk-users] Fw: DPDK Error --> EAL: No free hugepages reported in hugepages-2048kB DPDK无法分出连 ...
- MVC ---- 增删改成 EF6
1.MVC EF6的增删改成小练习 namespace T4Demo { public partial class Form1 : Form { NBEntities nb = new NBEntit ...
- MVC ---- 理解学习Func用法
//Func用法 public static class FuncDemo{ public static void TestFunc(){ //数据源 List<User> usList ...
- C#类头部声明样式
/******************************************************************** * * 使本项目源码前请仔细阅读以下协议内容,如果你同意以下 ...
- 【Tomcat】tomcat热部署和热加载(转载)
我在项目开发过程中,经常要改动JAVA/JSP 文件,但是又不想从新启动服务器(服务器从新启动花时间),想直接获得(debug)结果.有两种方式热部署 和热加载: 1.热加载:在server.xml ...