• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Sgu 118

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 987 is 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 are N 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

Author : Michael R. Mirzayanov
Resource : PhTL #1 Training Contests
Date : Fall 2001
 1 #pragma comment(linker,"/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cmath>
 5 #include <queue>
 6 #include <set>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <algorithm>
10 using namespace std;
11 #define INF 0x7fffffff 
12 #define mod 1000000007
13 #define ll long long
14 #define maxn 20025
15 #define pi acos(-1.0)  
16 int n, m, k, c,t;
17 int main(){
18     scanf("%d", &t);
19     while (t--){
20         scanf("%d", &n);
21         int s = 1, ans = 0, x;
22         for (int i = 0; i < n; i++){
23             scanf("%d", &x);
24             s = s*(x%9) % 9;
25             ans = (ans + s) % 9;
26         }
27         if (ans == 0)ans = 9;
28         printf("%d\n", ans);
29     }
30     return 0;
31 }
View Code
posted @ 2014-02-06 20:36  HaibaraAi  阅读(88)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3