# PROBLEM LINK:

Magic Number | CodeChef

* Author:* Charan Narukulla

# DIFFICULTY:

Simple

# PREREQUISITES:

Math

# PROBLEM:

Riyaz challenged Vikas to find a number called **MAGIC NUMBER** . In his view magic number is defined as the last digit of the value abab a and b both are not 0. You have to find the **MAGIC NUMBER** .

# EXPLANATION:

Based on given Constraints, basic brute force will not work. One should try noting down last digits of some set of continuous number. One can observe that last digits get repeated based on some logic. Finding the last digits of a to the power upto 4 each individually will give a clarity on the logic.

# SOLUTIONS:

## Setter's Solution

```
#include <iostream>
using namespace std;
#define ll long long
int main() {
int t;
cin>>t;
while(t--){
ll a,b,ans=1,point=-1,vals[5];
cin>>a>>b;
if(b==0)
cout<<1<<endl;
else{
for(int i=1;i<=4;i++){
ans=ans*a;
vals[i]=ans%10;
}
if(b<4){
cout<<vals[(b)]<<endl;
}
else if(b%4==0)
cout<<vals[4]<<endl;
else
cout<<vals[(b%4)]<<endl;
}}
return 0;
}
```