HDOJ 1163 Eddy's digital Roots [简单数论]

来源:互联网 发布:淘宝店铺访客提醒 编辑:程序博客网 时间:2024/05/16 11:42

H - Eddy's digital Roots
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

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 summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

The Eddy's easy problem is that : give you the n,want you to find the n^n's digital Roots.
 

Input

The input file will contain a list of positive integers n, one per line. The end of the input will be indicated by an integer value of zero. Notice:For each integer in the input n(n<10000).
 

Output

Output n^n's digital root on a separate line of the output.
 

Sample Input

240
 

Sample Output

44
 

题目描述:
求n^n次的digital root(数根),例如root(67)=6+7=root(13)=1+3=4;

求解思路:
现在分析一个问题,假设将十位数为a,个位数为b的一个整数表示为ab,则推导得
ab*ab = (a*10+b)*(a*10+b) = 100*a*a+10*2*a*b+b*b
根据上式可得:root(ab*ab) = a*a+2*a*b+b*b = (a+b)*(a+b);[公式一] 
同理也可证得:root(ab*ab*ab) = (a+b)*(a+b)*(a+b);[公式二] 
可以看出,N个相同整数的乘积总值的树根 = 每一项元素的树根的乘积

再设另外一个整数cd,且cd!=ab
ab*cd = (a*10+b)*(c*10+d) = 100*a*c+10*(a*d+b*c)+b*d
根据上式可得:root(ab*cd) = a*c+a*d+b*c+b*d = (a+b)*(c+d);[公式三] 
可见,对于两个不相同整数也成立。

最后将上面证得的结果一般化:
N个整数的乘积总值的数根 = 每个项元素的数根的乘积 

提示:本题只需根据[公式三] 即可AC.

 

AC代码:

#include <iostream>#include <cmath>using namespace std;int boot(int n){    int m,sum,a;    sum=0,m=n;    do{        a=m%10;        m=m/10;        sum+=a;    }while(m!=0);    if(sum/10!=0) boot(sum);    else    return sum;}int main(){    int n,m,k;    while(cin>>n,n)    {        k=boot(n);        m=1;        while(n--)        m=boot(m*k);        cout<<m<<endl;    }    return 0;}

转载自CSDN,原文链接:

HDOJ 1163 Eddy's digital Roots [简单数论]

0 0