Problem D: Digit Rightmost
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 78 Solved: 12
[ Submit][ Status][ Web Board]
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
<span class="sampledata" style="font-family: monospace; font-size: 18px; white-space: pre; background-image: none; background-attachment: scroll; background-color: rgb(141, 184, 255); background-position: 0px 0px; background-repeat: repeat repeat;">2 3 4 </span>
Sample Output
<span class="sampledata" style="font-family: monospace; font-size: 18px; white-space: pre; background-image: none; background-attachment: scroll; background-color: rgb(141, 184, 255); background-position: 0px 0px; background-repeat: repeat repeat;">7 6 </span>
HINT#include <stdio.h>
int f(int a,int b,int p)
{
int temp = 1;
while(b)
{
if(b & 0x01)
{
temp = (temp * (a%p)) % p;
}
a = ( (a%p) * (a%p) ) % p;
b >>= 1;
}
return temp;
}
int main()
{
int a,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",f(n,n,10));
}
return 0;
}
#include <stdio.h>
int f(int a,int b,int p)
{
int temp = 1;
while(b)
{
if(b & 0x01)
{
temp = (temp * (a%p)) % p;
}
a = ( (a%p) * (a%p) ) % p;
b >>= 1;
}
return temp;
}
int main()
{
int a,n,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%d\n",f(n,n,10));
}
return 0;
}
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.