您的位置:首页 > 其它

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

2014-07-04 15:53 405 查看
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

2
4
0




Sample Output

4
4




题目描述:

求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 [简单数论]

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: