快速切题 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 ...
随机推荐
- JRebel for IntelliJ 热部署破解方法
1.打开idea,然后打开设置. 2.点击Plugins 3.重启之后点击 4.下载激活JRebel的插件,下载地址:https://github.com/ilanyu/ReverseProxy/re ...
- 最大子段和SP1716GSS3 线段树
前言 spoj需要FQ注册,比较麻烦,大家就在luogu评测吧 题目大意: $n$ 个数,$q$ 次操作 操作$0 _ x_ y$把$A_x$ 修改为$y$ 操作$1 _ l _r$询问区间$[l, ...
- input标签type=button时,如何禁用和开启按钮
本文为博主原创,未经允许不得转载: <input id="exportCameraButton" type="button" class="bt ...
- stm32 iic读取mpu6050失败 改用串口
mpu6050使用iic一直失败.放弃治疗,使用串口... #include "led.h" #include "mpu6050.h" #include &qu ...
- HDU 6178 Monkeys(树上的二分匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=6178 题意:现在有一n个顶点的树形图,还有k只猴子,每个顶点只能容纳一只猴子,而且每只猴子至少和另外一只猴子通过 ...
- 【Python】【有趣的模块】【Bobo】
[python web框架之 bobo的安装配置] [Mac] 我的Mac环境,python3.5 1. 安装bobo : >>> pip3 install bobo 2. 配 ...
- 一个对iBatis的总结写的不错(转载)
转载自:http://blog.csdn.net/panxueji/article/details/9852795 一. ibatis介绍 ibatis始于2002年,2010年更名为mybatis, ...
- c++ 返回指定元素连续相等的位置索引(equal_range)
#include <iostream> // cout #include <algorithm> // equal_range, sort #include <vecto ...
- Qt5_容器_知识点记录
1.删除: 1.1.erase 1.2.remove / removeAt 2. 3. 4. 5.
- socket编程之accept()函数【转载】
名称 accept() 接收一个套接字中已建立的连接 使用格式 #include <sys/types.h> #include <sys/socket.h> int accep ...