Rightmost Digit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 63994 Accepted Submission(s): 24006
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).
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
234
Sample Output
76
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的1取值范围为1000000000,一看就知道这是无法计算的数太大,但是通过简单的计算会发现有规律:末尾为0,无论数有多大,最后一位肯定为0,1,5,6均是如此,
2的话只是后几位相乘会发现它是一个以4为循环的数,2^1=2 2^2=4 2^3=8 2^4=6 2^5=2,因此我们可以看到,二的倍数中个位数是以2、4、8、6为循环周期的,而且在本题中,以2为个位数的N肯定是偶数,因此,个位数只能为4和6,当N能被4整除的时候,个位才为6,否则为4;
3:3^1=3 3^2=9 3^3=7 3^4=1 3^5=3,因此3的倍数中个位数是以3、9、7、1为循环周期,与2类似,9和1去掉,尾数只能为3和7,当n%4==1时,个位为3,否则为7;
4:4、6、4、6,以2为周期,而且只能为偶数,所以个位为6;
6,:个位只能为6;
7:7、9、3、1,周期为4,个位只能为7和3,当n%4==1时,个位为7,否则为3;
8:8、4、2、6,周期为4,个位只能为4和6,当n%4==0,个位为6,否则为4;
9:9、1,所以9的周期为2,但是末尾为9的数无论为什么取余均为1所以为9
代码:
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define max(a,b)a>b?a:b
#define min(a,b)a>b?b:a
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
int n,i,j,ans=0;
scanf("%d",&n);
int aa=n%10;
if(aa==0)
{
printf("0\n");
}
if(aa==1)
{
printf("1\n");
}
if(aa==2)
{
if(n%4==0)
{
printf("6\n");
}
else
printf("4\n");
}
if(aa==3)
{
if(n%4==1)
{
printf("3\n");
}
else
printf("7\n");
}
if(aa==4)
{
printf("6\n");
}
if(aa==5)
{
printf("5\n");
}
if(aa==6)
{
printf("6\n");
}
if(aa==7)
{
if(n%4==3)
printf("3\n");
else
printf("7\n");
}
if(aa==8)
{
if(n%4==0)
printf("6\n");
else
printf("4\n");
}
if(aa==9)
{
printf("9\n");
}
}
return 0;
}