hdu 1163 九余数定理

Eddy's digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5928    Accepted Submission(s): 3270


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 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
思路:求n^n对9取模,特判0的情况;
 对于那个定理的证明;
设那个数为abcd;这个数=1000*a+100*b+10*c+d;
        a+b+c+d=这个数-9999a-99b-99c;
        由于9999a-99b-9c被九整除;得证;
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<string>
 5 #include<queue>
 6 #include<algorithm>
 7 #include<stack>
 8 #include<cstring>
 9 #include<vector>
10 #include<list>
11 #include<set>
12 #include<map>
13 using namespace std;
14 #define ll long long
15 #define mod 1000000007
16 #define inf 999999999
17 #define esp 0.00000000001
18 //#pragma comment(linker, "/STACK:102400000,102400000")
19 int quickpow(int a,int b,int c)
20 {
21     int ans=1;
22     while(b)
23     {
24         if(b&1)
25         {
26             ans*=a;
27             ans%=c;
28         }
29         b>>=1;
30         a*=a;
31         a%=c;
32     }
33     return ans==0?9:ans;
34 }
35 int main()
36 {
37     int x,y,z,i,t;
38     while(~scanf("%d",&x))
39     {
40         if(!x)break;
41         printf("%d\n",quickpow(x,x,9));
42     }
43     return 0;
44 }

 

  
posted @ 2016-05-16 22:05  jhz033  阅读(189)  评论(0编辑  收藏  举报