SGU118——Digital Root
来源:互联网 发布:淘宝抠图教程 编辑:程序博客网 时间:2024/06/06 05:32
118. Digital Root
time limit per test: 0.5 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 off(n). For example, digital root of 987 is 6. Your task is to find digital root for expressionA1*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 numberN 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 than109.
Output
Write one line for every test case. On each line write digital root for given expression.
Sample Input
13 2 3 4
Sample Output
5
首先,比如样例三个数字,要求是2*3*4+2*3+2,可以这么看2+2*3+2*3*4 还有两个数论公式:(a+b)%c=(a%c)+(b%c) (a*b)%c=(a%c)*(b%c) 还有一个结论是一个数的所有位数的和为这个数对9取模。
#include<iostream>#include<string.h>#include<stdio.h>#include<ctype.h>#include<algorithm>#include<stack>#include<queue>#include<set>#include<math.h>#include<vector>#include<map>#include<deque>#include<list>using namespace std;int main(){ int t,n,a; scanf("%d",&t); while(t--) { int p=1,sum=0; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a); a%=9; p*=a; p%=9; sum+=p; } sum%=9; if(sum==0) printf("9\n"); else printf("%d\n",sum); } return 0;}
- SGU118——Digital Root
- sgu118:Digital root
- SGU118 Digital Root
- SGU118
- sgu118
- Digital Root
- digital root
- digital root
- Digital Root 算法
- Digital Root----code forces
- HDU 4351 - Digital root
- hdu4351 Digital root
- [SGU]118. Digital Root
- sgu 118 Digital Root
- Digital Root的问题
- Vasya and Digital Root
- POJ 1519 digital root
- 118. Digital Root
- iOS如何判断当前网络的运营商
- 【wikioi 3145】汉诺塔游戏C代码
- Spring Security3配置使用
- fmdb(FMDatabase) 数据库总结
- 获取手机号码,和IMEI 获取本地iphone手机号码
- SGU118——Digital Root
- java连接oracle数据库 -- jdbc连接
- 山东理工大学ACM平台题答案 1604 人见人爱A^B
- 如何扩大一个view的touch和click响应区域
- 夜的思绪
- 简单的Android播放器
- Word提示:“向程序发送命令时出现问题”解决方法
- 黑马程序员 java提高——张孝祥银行业务调度系统
- ORACLE之 DB_NAME、ORACLE_SID和INSTANCE_NAME