Skip to content

Instantly share code, notes, and snippets.

@completejavascript
Created September 15, 2018 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save completejavascript/eb336f050d80dd1dc6dcaeed97dcd621 to your computer and use it in GitHub Desktop.
Save completejavascript/eb336f050d80dd1dc6dcaeed97dcd621 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int power(int a, int b)
{
if (b == 0) return 1;
if(b % 2 == 0)
{
int t = power(a, b/2);
return t*t;
}
else
{
int t = power(a, (b - 1) / 2);
return t*t*a;
}
}
int main()
{
ios::sync_with_stdio(false);
freopen("input.txt","r",stdin);
int T;
unsigned int a, b;
int last_digit;
cin >> T;
for(int tc = 0; tc < T; tc++)
{
cin >> a >> b;
if(b == 0) last_digit = 1;
else
{
int x = a % 10;
int y = b % 4;
if (y == 0) y = 4;
last_digit = power(x, y) % 10;
}
cout << last_digit << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment