Get Last Digit

Nestor was doing the work of his math class about three days but he is tired of make operations a lot and he should deliver his task tomorrow. His math’s teacher gives him two numbers a and b. The problem consist of finding the last digit of the potency of base a and index b. Help Nestor with his problem. You are given two integer numbers: the base a (0 <= a <= 20) and the index b (0 <= b <= 2,147,483,000), a and b both are not 0. You have to find the last digit of ab.

Input

The first line of input contains an integer t, the number of test cases (t <= 30). t test cases follow. For each test case will appear a and b separated by space.

Output

For each test case output an integer per line representing the result.

Example

Input:
2
3 10
6 2

Output:
9
6

Solution

C++

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
 int p_hasil[100];
 int i=0,p=0,dataIn,dataIn2;
 int a[100];
 int b[100];
 cin >>p;
 cout <<"\n";  if (p>30)
 {
 cout <<"Tidak boleh >30 \n";
 cin >>p;
 cout <<"\n";  }  for (i=0;i >dataIn;
 if (dataIn==0)
 {
 cout <<"Data tidak boleh 0 \n";  cin >>dataIn;
 }
 cout <<" ";  cin >>dataIn2;
 if (dataIn2==0)
 {
 cout <<"Data tidak boleh 0 \n";  cin >>dataIn2;
 }
 a[i]=dataIn;
 b[i]=dataIn2;
 if (a[i]==5 || a[i]==6 || a[i]==16 || a[i]==15) {p_hasil[i]=a[i];}
 else if (a[i]==0 || a[i]==10 || a[i]==20) {if (a[i]==0) {p_hasil[i]=0;}else if((a[i]==10 || a[i]==20) && b[i]==0){p_hasil[i]=1;}else{p_hasil[i]=0;}}
 else if (a[i]==2 || a[i]==12) {if (b[i]==0) {p_hasil[i]=1;}else if (b[i]==1 || b[i]%4==1) {p_hasil[i]=a[i];}else if (b[i]==2 || b[i]%4==2) {p_hasil[i]=4;}else if (b[i]==3 || b[i]%4==3) {p_hasil[i]=8;}else if (b[i]==4 || b[i]%4==0) {p_hasil[i]=6;}}
 else if (a[i]==3 || a[i]==13) {
 if (b[i]==0) {
 p_hasil[i]=1;
 }
 else if (b[i]==1 || b[i]%4==1) {
 p_hasil[i]=a[i];
 }
 else if (b[i]==2 || b[i]%4==2) {
 p_hasil[i]=9;
 }
 else if (b[i]==3 || b[i]%4==3) {
 p_hasil[i]=7;
 }
 else if (b[i]==4 || b[i]%4==0) {
 p_hasil[i]=1;
 }

 }
 else if (a[i]==4 || a[i]==14) {
 if (b[i]==0) {
 p_hasil[i]=1;
 }
 else if (b[i]==1) {
 p_hasil[i]=a[i];
 }
 else if (b[i]%2==0) {
 p_hasil[i]=6;
 }
 else if (b[i]%2==1) {
 p_hasil[i]=4;
 }
 }
 else if (a[i]==7 || a[i]==17) {
 if (b[i]==0) {
 p_hasil[i]=1;
 }
 else if (b[i]==1 || b[i]%4==1) {
 p_hasil[i]=a[i];
 }
 else if (b[i]==2 || b[i]%4==2) {
 p_hasil[i]=9;
 }
 else if (b[i]==3 || b[i]%4==3) {
 p_hasil[i]=3;
 }
 else if (b[i]==4 || b[i]%4==0) {
 p_hasil[i]=1;
 }

 }
 else if (a[i]==8 || a[i]==18) {
 if (b[i]==0) {
 p_hasil[i]=1;
 }
 else if (b[i]==1 || b[i]%4==1) {
 p_hasil[i]=a[i];
 }
 else if (b[i]==2 || b[i]%4==2) {
 p_hasil[i]=4;
 }
 else if (b[i]==3 || b[i]%4==3) {
 p_hasil[i]=2;
 }
 else if (b[i]==4 || b[i]%4==0) {
 p_hasil[i]=6;
 }

 }
 else if (a[i]==9 || a[i]==19) {
 if (b[i]==0) {
 p_hasil[i]=1;
 }
 else if (b[i]%2==0 || b[i]==1) {
 p_hasil[i]=1;
 }
 else if (b[i]%2==1) {
 p_hasil[i]=9;
 }
 } 

 }
 for (i=0;i
<p ;i++ ) {
 cout <<p_hasil[i]<<"\n";
 }
 return 0;
 }

Java

import java.util.Arrays;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main{
		public static void main(String[] args) throws java.lang.Exception {
		    BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.in));
			int p_hasil[] = new int[100];
			String hsl_split[] = new String[100];
			int i=0,p=0;
			String soal=new String();
			String soal_splite[] = new String[100];
			int a[]=new int[100];
			int b[]=new int[100];
			try{ p = Integer.parseInt(dataIn.readLine());}catch(IOException e ){e.printStackTrace();}
			for (i=0;i<p ;i++ ) {
				try{
                    soal=dataIn.readLine();soal_splite=soal.split(" ");a[i]=Integer.parseInt(soal_splite[0]);b[i]=Integer.parseInt(soal_splite[1]);
                    	if (a[i]==5 || a[i]==6 || a[i]==16 || a[i]==15)
                              {
                                   p_hasil[i]=a[i];
                              }
                    	else if (a[i]==0 || a[i]==10 || a[i]==20) {
                              if (a[i]==0)
                                   {p_hasil[i]=0;}
                              else if((a[i]==10 || a[i]==20) && b[i]==0)
                                   {p_hasil[i]=1;}else{p_hasil[i]=0;}
                               }
                    	      else if (a[i]==2 || a[i]==12)
                                   {if (b[i]==0)
                                        {p_hasil[i]=1;}
                                   else if (b[i]==1 || b[i]%4==1)
                                        {p_hasil[i]=a[i];}
                                   else if (b[i]==2 || b[i]%4==2)
                                        {p_hasil[i]=4;}
                                   else if (b[i]==3 || b[i]%4==3)
                                        {p_hasil[i]=8;}
                                   else if (b[i]==4 || b[i]%4==0)
                                        {p_hasil[i]=6;}}
                    	      else if (a[i]==3 || a[i]==13) {
                    		if (b[i]==0) {
                    			p_hasil[i]=1;
                    		}
                    		else if (b[i]==1 || b[i]%4==1) {
                    			p_hasil[i]=a[i];
                    		}
                    		else if (b[i]==2 || b[i]%4==2) {
                    			p_hasil[i]=9;
                    		}
                    		else if (b[i]==3 || b[i]%4==3) {
                    			p_hasil[i]=7;
                    		}
                    		else if (b[i]==4 || b[i]%4==0) {
                    			p_hasil[i]=1;
                    		}

                    	}
                    	else if (a[i]==4 || a[i]==14) {
                    		if (b[i]==0) {
                    			p_hasil[i]=1;
                    		}
                    		else if (b[i]==1) {
                    			p_hasil[i]=a[i];
                    		}
                    		else if (b[i]%2==0) {
                    			p_hasil[i]=6;
                    		}
                    		else if (b[i]%2==1) {
                    			p_hasil[i]=4;
                    		}
                    	}
                    	else if (a[i]==7 || a[i]==17) {
                    		if (b[i]==0) {
                    			p_hasil[i]=1;
                    		}
                    		else if (b[i]==1 || b[i]%4==1) {
                    			p_hasil[i]=a[i];
                    		}
                    		else if (b[i]==2 || b[i]%4==2) {
                    			p_hasil[i]=9;
                    		}
                    		else if (b[i]==3 || b[i]%4==3) {
                    			p_hasil[i]=3;
                    		}
                    		else if (b[i]==4 || b[i]%4==0) {
                    			p_hasil[i]=1;
                    		}

                    	}
                    	else if (a[i]==8 || a[i]==18) {
                    		if (b[i]==0) {
                    			p_hasil[i]=1;
                    		}
                    		else if (b[i]==1 || b[i]%4==1) {
                    			p_hasil[i]=a[i];
                    		}
                    		else if (b[i]==2 || b[i]%4==2) {
                    			p_hasil[i]=4;
                    		}
                    		else if (b[i]==3 || b[i]%4==3) {
                    			p_hasil[i]=2;
                    		}
                    		else if (b[i]==4 || b[i]%4==0) {
                    			p_hasil[i]=6;
                    		}

                    	}
                    	else if (a[i]==9 || a[i]==19) {
                    		if (b[i]==0) {
                    			p_hasil[i]=1;
                    		}
                    		else if (b[i]%2==0 || b[i]==1) {
                    			p_hasil[i]=1;
                    		}
                    		else if (b[i]%2==1) {
                    			p_hasil[i]=9;
                    		}
                    	}
				}catch(IOException e ){
                    e.printStackTrace();
				}
			}
			for (i=0;i<p ;i++ ) {System.out.println(p_hasil[i]);}
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *