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 was a single integer T which is the number of test cases. T test Cases follow. Each test case is 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 = rightmost, so the-the-digit is 7. In the second case, 4 * 4 * 4 * 4 = at the very rightmost digit is 6.
Method One:
1#include <iostream>2 using namespacestd;3 inta[ -]={0,1,4,7,6,5,6,3,6,9,0,1,6,3,6,5,6,7,4,9,0};4 intMain ()5 {6 intB,n;7Cin>>b;8 while(b--)9 {TenCin>>N; Onecout<<a[n% -]<<Endl; A } - return 0 ; -}
Method Two:
1#include <stdio.h>2 intMy_power (intMintn);//to find the n-th mantissa of M3 intMain ()4 {5 intcases, N;6scanf"%d", &cases);7 while(cases--)8 {9scanf"%d", &n);Tenprintf"%d\n", My_power (n, N)); One } A - return 0; - } the - intMy_power (intMintN) - { -m = m%Ten; + if(n = =1) - returnm; + if(n%2==0) A return(My_power (M*m, n/2) ) %Ten; at Else - return(My_power (M*m, n/2) *m)%Ten; -}
The time complexity of the fast power is O (logn), n = 1 billion, about 32 recursive calls can produce results, the efficiency greatly improved
Rightmost Digit (Fast power)