HDU1163 "Nine-odd-number theorem" "Water problem"

Source: Internet
Author: User

Eddy ' s digital Roots

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)Total Submission (s): 4632 Accepted Submission (s): 2578



Problem Description
The digital root of a positive integer is found by summing the digits of the integer. If The resulting value is a, digit then, digit is the digital root. If The resulting value contains or more digits, those digits was summed and the process is repeated. This was continued as long as necessary to obtain a and 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 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 are that:give you the n,want your to find the N^n ' s digital Roots.



Input
The input file would contain a list of positive integers n, one per line. The end of the input would 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



Author

Eddy

The main topic: to give you a positive integer n, the number of N to add up, assuming that the result is less than 10, the result is N of the number of the root, assuming that more than 10, then the upper results of the figures on each of you add up. Now give you a number n, seeking the n^n of the number root

Idea: A look at the scale of data 10000^10000, must be the N split off. By finding the law, we find that the digital root of N^n can be converted to the number of first n

Word root A, and then seek the original root of A*n, assigned to a, and then in turn a*n, seeking n-1 times, the n^n of the digital roots.

For example: To find the number of 5^5

The first method: 5^5 = 3125 3 + 1 + 2 + 5 = 11 1 + 1 = 2 Finally the result is 2

Another method: 5 of the number root is 5 5*5*5*5*5 = 25*5*5*5

Equivalent to 25 of the number root 7 *5*5*5 = 35*5*5 = 8*5*5 = 40*5 = 4*5 = 20 = 2

Finally the result is 2

For another method, it is simpler to use the nine-odd theorem.

Nine remainder theorem: A number n the sum of the numbers, to 9 is equal to the number of 9 to take the remainder

<span style= "Font-family:microsoft yahei;font-size:18px;" >//does not use the nine remainder theorem #include<stdio.h>int main () {    int n;    while (~SCANF ("%d", &n) && N)    {        int a = n;        int b = 0;        while (a > 0)        {            b + = a%;            a/=;        }        if (b! = 0)            a = B;        int x = A;        for (int i = 1; i < n; i++)        {            a *= x;            b = 0;            while (a > 0)            {                b + = a%;                a/=;            }            if (b! = 0)                a = b;        }        b = 0;        while (a > 0)        {            b + = a%;            a/=;        }        if (b! = 0)            a = B;        printf ("%d\n", a);    }    return 0;} </span>

Use nine remainder theorem # include <stdio.h>int main () {    int n,a,sum,i;    while (scanf ("%d", &n) &&n)    {        sum=1;        for (i=0;i<n;i++)        {            sum=sum*n%9;        }        if (sum==0)             printf ("9\n");        else             printf ("%d\n", sum);            }    return 0;}






HDU1163 "Nine-odd-number theorem" "Water problem"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.