Hdu1163 [9 Remainder Theorem] [water question]

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 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 in 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



Author

Eddy

Give you a positive integer N and add all the numbers in N. If the result is less than 10, the result is the root number of n. If the result is greater than 10, then add the numbers in the above results. Now I will give you a number N, and calculate the number root of N ^ n.

Idea: The data size is 10000 ^ 10000, and N must be split. We can find that the root number of N ^ N can be converted to the number of N first.

Root A, then obtain the original root of a * n, assign it to a, and then calculate a * n in sequence, and obtain the numerical root of N ^ n.

For example, calculate the number of 5 ^ 5

Method 1: 5 ^ 5 = 3125 3 + 1 + 2 + 5 = 11 1 + 1 = 2 the final result is 2.

Method 2: The number root of 5 is 5 5*5*5*5*5 = 25*5*5*5

Equivalent to 25 digits, root 7*5*5*5 = 35*5*5 = 8*5*5 = 40*5 = 4*5 = 20 = 2

The final result is 2.

The ninth remainder theorem can be used for the second method, which is simpler.

9 remainder Number Theorem: The sum of all numbers in a number n. 9 is equal to 9.

<span style="font-family:Microsoft YaHei;font-size:18px;">//不使用九余数定理#include<stdio.h>int main(){    int n;    while(~scanf("%d", &n) && n)    {        int a = n;        int b = 0;        while(a > 0)        {            b += a % 10;            a /= 10;        }        if(b != 0)            a = b;        int x = a;        for(int i = 1; i < n; i++)        {            a *= x;            b = 0;            while(a > 0)            {                b += a % 10;                a /= 10;            }            if(b != 0)                a = b;        }        b = 0;        while(a > 0)        {            b += a % 10;            a /= 10;        }        if(b != 0)            a = b;        printf("%d\n",a);    }    return 0;}</span>

//使用九余数定理#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 [9 Remainder Theorem] [water question]

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.