rightmost Digit Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 34329 accepted Submission (s): 13091
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 ' the ' 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 "the", 3 * 3 * 3 = rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.
Author IGNATIUS.L
Fast power modulus, not explained.
AC Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstdlib>
using namespace std;
int qmi (int m)
{
int a = m%10, ans = 1;
while (m)
{
if (m&1)
ans = (ans*a)%10;
A = (a*a)%10;
m>>=1;
}
return ans%10;
}
int main ()
{
int n, t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
printf ("%d\n", QMI (n));
}
return 0;