sgu 118 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 987 is 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 Nis 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


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int M=1000+5;
int a[M];
int main()
{
    int k;
    scanf("%d",&k);
    while(k--){
        int n;
        scanf("%d",&n);
        int ans=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            a[i]=a[i]%9;
            if(i!=1){
                a[i]=(a[i]*a[i-1])%9;
            }
            ans=(ans+a[i])%9;
        }
        printf("%d\n",ans?ans:9);
    }

    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章