Reputation: 35
I am trying to find the unit digit of a number raised to a certain power, using the simplest method. I know about modular exponentiation. I am trying to implement something similar to How to find the units digit of a certain power in a simplest way, but my code is showing the wrong answer for certain test cases in SPOJ. Here is the link to the question in SPOJ: http://www.spoj.com/problems/LASTDIG/
#include<iostream>
using namespace std;
int main() {
int a2[] = {6, 2, 4, 8},
a3[] = {1, 3, 9, 7},
a4[] = {6, 4},
a7[] = {1, 7, 9, 3},
a8[] = {6, 8, 4, 2},
a9[] = {1, 9},
t;
long long int b1, b2;
cin >> t;
while (t--) {
cin >> b1 >> b2;
b1 = b1 % 10;
if (b2 == 0)
cout << 1 << endl;
else if (b1 == 0)
cout << 0 << endl;
else if (b2 == 1 || b1 == 5 || b1 == 6)
cout << b1 << endl;
else if (b1 == 2)
cout << a2[b2 % 4] << endl;
else if (b1 == 3)
cout << a3[b2 % 4] << endl;
else if (b1 == 4)
cout << a4[b2 % 2] << endl;
else if (b1 == 7)
cout << a7[b2 % 4] << endl;
else if (b1 == 8)
cout << a8[b2 % 4] << endl;
else if (b1 == 9)
cout << a9[b2 % 2] << endl;
}
return 0;
}
Upvotes: 0
Views: 369
Reputation: 28830
First
a7[]={1,7,0,3}
^^^
zero should be 9
a7[]={1,7,9,3}
And a9
a9[]={9,1}
^^^
should be reverse
a9[]={1,9}
Upvotes: 1