HDU 1163 Eddy ' s digital Roots

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): 5783 Accepted Submission (s): 3180


Problem DescriptionThe 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.

Inputthe 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).

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

Sample Input240

Sample output44    parsing: Examining the digital roots. The number root definition: For a positive integer, if it is single digit, its number root is itself. If it is not a single digit, then the numbers on each of them are added together, and if it is a single digit, then this number is the root of the number, otherwise the numbers on this number will be added until the sum is one digit. Number N number root: Digital root = (n-1)%9+1. For the convenience of calculation, we can also write as digital root = (n%9 = = 0? 9:n%9). Number Root properties: 1. Any number plus 9 of the number root equals its own number root. 2. Any number multiplied by 9 of the number root equals 9. 3. The number of the sum of the numbers is equal to the number root of the number of the number root. 4. The number root of a plurality of numbers is equal to the number root of the number of the number root. According to the above law, the fast algorithm can be obtained by combining the fast power and the method of taking the modulus.  
 //  calculates the y power of x (FAST)  int  Quickpow (int  x,int   Y) { int  ret = 1  ;  while  (Y if  (Y&1  ) ret  *= X;        x  *= X;    Y  >>= 1  ;  return   ret;}  
 //  calculate x's y power to mod modulo (fast)  int  quickpowmod (int  x,int   Y,int MoD) { int  ret = 1  ;    x %= mod;  while  (Y if  (Y&1  ) ret  = Ret*x%mod;        x  = X*x%MOD;    Y  >>= 1  ;  return   ret;}  

1#include <cstdio>2 3 intQuickpowmod (intXintYintMoD)4 {5     intRET =1;6X%=MoD;7      while(y) {8         if(y&1)9RET = ret*x%MoD;Tenx = x*x%mod;; OneY >>=1; A     } -     returnret; - } the  - intMain () - { -     intN; +      while(SCANF ("%d",&N), N) { -         intAns = quickpowmod (n,n,9); +printf"%d\n", ans = =0?9: ans); A     } at     return 0; -}

HDU 1163 Eddy ' s digital Roots

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.