SGU[118] Digital Root

Description

描述

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.

令f(n)表示正整数n的各个位上数字之和。如果f(n)是一个一位数,那么则是n的一个数字根,否i则,n的数字根等于f(n)的数字根。例如987的数字根为6。你的任务是求表达式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.

输入包含多组测试数据。输入的第一行为K (1 <= K <= 5),表示测试数据组数。

每组测试数据占一行,第一个整数N (N <= 1000)表示Ai的个数,接下来N个数表示Ai,保证Ai非负且不超过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

Analysis

分析

结论题——f(n) ≡ n(mod 9)。

证明如下:

令n = a0 * 10p0 +  a1 * 10p1 + ... + am-1 * 101 + am * 100,其中n为m位数。

则n mod 9 = a0 + a1 + ... + am-1 + a= f(n),即f(n) ≡ n(mod 9)。

证毕。

需要注意的是,当n mod 9 == 0的时候,f(n) = 9。

读入的时候要先把数据mod 9,否则中间计算过程会超int。

Solution

解决方案

#include <iostream>

using namespace std;

const int MAX = 1024;

int pData[MAX];

int main()
{
	int T, N;
	cin >> T;
	for(int i = 1; i <= T; i++)
	{
		int ans = 0;
		cin >> N;
		for(int j = 1; j <= N; j++)
		{ cin >> pData[j]; pData[j] %= 9; }
		for(int j = 1; j <= N; j++)
		{
			int nTmp = 1;
			for(int k = 1; k <= j; k++)
			{
				nTmp *= pData[k];
				if(nTmp >= 9) { nTmp %= 9; }
			}
			ans += nTmp;
			if(ans >= 9) { ans %= 9; }
		}
		cout << (ans == 0 ? 9 : ans) << endl;
	}
}

  

又是一个数论结论,结论题有时候还是挺难想到的。

时间: 2024-08-07 17:00:52

SGU[118] Digital Root的相关文章

数字根(digital root)

来源:LeetCode 258  Add Dights Question:Given a non-negative integer  num , repeatedly add all its digits until the result has only one digit. For example: Given  num = 38 , the process is like:  3 + 8 = 11 ,  1 + 1 = 2 . Since  2  has only one digit, r

Digital root(数根)

关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.适用范围为正整数和零.例如:65536,6+5+5+3+6=25,2+5=7,故数根为7. 二.性质 1. 任何数加减9的数字根还是它本身. 2. 9乘任何数字的数字根都是9. 3. 数字根的三则运算 (1). 两数之和的数字根等于这两个数的数字根的和数字根 (2).

Digital root的求解

源于hdu1013 题目描述: The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are s

Codeforces 10C Digital Root 规律题

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<string> #include<stdlib.h> #include<algorithm> using nam

[codewars_python]Sum of Digits / Digital Root

Instructions In this kata, you must create a digital root function. A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way unti

DIgital Root 的推导

背景 在LeetCode上遇到这道题:Add Digits 题目很简单,但是如果要用 O(1) 时间复杂度,不要涉及循环或递归来解答的话,我就不知道如何下手了. 于是我找了一下别人的解法,发现涉及到一个 Digital Root 的原理(由于维基百科打不开,所以我觉得有必要记录一下我搜集到的信息和理解). Digital Root 我是从这个网站上看到它的推导过程,但是为了防止以后这些引用的网站不存在或者访问不了,还是得自立更生写一下. 首先,A ≡ B mod C, ≡ 这个符号, 表示 A

Digital Root - SGU 118(高精度运算)

题目大意:有K组测试数据,然后每组有N个正整数,A1,A2,A3.....An,求出 A1 + A1*A2 + A1*A2*A3 + .......A1*A2*...An 的数根. 分析:有个对9取余的定理是可以直接求树根的,不过拿来玩大数运算也不错.ps.每位可以保存9位数,保存10位数会溢出. 高精度代码如下: ===========================================================================================

CodeForces 10C. Digital Root

求A,B,C ∈[1,N] && A*B != C,d(A*B) == d(C)的组数. 首先要知道d(x) = (x%9 == 0 ? 9 : x%9); 那么则会有A*B == C,则必有d(A*B) == d(C). 若不考虑A*B != C,则答案既是ans[x]*ans[y]*ans[d(x*y)],ans[x]为d(i) == x的个数,显然x∈[1,9]. 当考虑A*B != C时,则需要从ans[x]*ans[y]*ans[d(x*y)] - sum. sum = sigm

Codeforces 10C Digital Root 法冠军

主题链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<string> #include<stdlib.h> #include<algorithm> using nam