hdu 1061 Rightmost Digit

来源:互联网 发布:coc墙升级数据 编辑:程序博客网 时间:2024/06/05 17:57
Rightmost Digit
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 31559    Accepted Submission(s): 12070




Problem Description
Given a positive integer N, you should output the most right digit of N^N.


 


Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).


 


Output
For each test case, you should output the rightmost digit of N^N.


 


Sample Input
2
3
4
 


Sample Output
7
6


Hint
In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7.
In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.


/*题解:
    求N^N的最低位有一定规律 。当N最末尾为0 1 5 6时,最末尾一定为0 1 5 6
    当N最末尾为 2 3 4 7 8 9时,其次方的最末尾具有周期性。 
    */


#include<cstdio>
int main(){
    int T,t;
    scanf("%d",&T);
    while(T--){
        __int64 m;
        scanf("%I64d",&m);
        t=m%10;
        if(t==0) printf("0\n");
        if(t==1) printf("1\n");
        if(t==5) printf("5\n");
        if(t==6) printf("6\n");
        if(t==2){
            if(m%4==0) printf("6\n"); 
            if(m%4==1) printf("2\n"); 
            if(m%4==2) printf("4\n"); 
            if(m%4==3) printf("8\n");
        }
        if(t==3){
            if(m%4==0) printf("1\n"); 
            if(m%4==1) printf("3\n"); 
            if(m%4==2) printf("9\n"); 
            if(m%4==3) printf("7\n");
        }
        if(t==4){
            if(m%2==0) printf("6\n"); 
            if(m%2==1) printf("4\n");
        }
        if(t==7){
            if(m%4==0) printf("1\n"); 
            if(m%4==1) printf("7\n"); 
            if(m%4==2) printf("9\n"); 
            if(m%4==3) printf("3\n");
        }
        if(t==8){
            if(m%4==0) printf("6\n"); 
            if(m%4==1) printf("8\n"); 
            if(m%4==2) printf("4\n"); 
            if(m%4==3) printf("2\n");
        }
        if(t==9){
            if(m%2==0) printf("1\n"); 
            if(m%2==1) printf("9\n"); 
        }
    }
    return 0;
}
        
0 0
原创粉丝点击