Skip to content

Instantly share code, notes, and snippets.

@ititorit
Created September 26, 2016 13:43
Show Gist options
  • Save ititorit/39dd0426d1537bcbe0042ec02a88ee0b to your computer and use it in GitHub Desktop.
Save ititorit/39dd0426d1537bcbe0042ec02a88ee0b to your computer and use it in GitHub Desktop.
SPOJ LASTDIG
#include <bits/stdc++.h>
using namespace std;
int main(){
long long int t,a,b,rm,lst,ans;
int ar[] = {0,0,4,4,2,1,1,4,4,2};
cin >> t;
while(t--){
cin >> a >> b;
lst=a%10;
if(lst == 1 || b == 0){
ans = 1;
} else if(lst == 0){
ans = 0;
} else{
rm = ar[lst];
rm = b % rm;
if(rm == 1) ans = (int)pow(lst,1)%10;
else if(rm == 2) ans = (int)pow(lst,2)%10;
else if(rm == 3) ans = (int)pow(lst,3)%10;
else ans = (int)pow(lst,4)%10;
}
cout << ans << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment