import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb=new StringBuffer();
int[]mods={1, 1, 4, 4, 2, 1, 1, 4, 4, 2};
while(true){
StringTokenizer st=new StringTokenizer(br.readLine());
String str=st.nextToken();
int m=Integer.parseInt(str.charAt(str.length()-1)+"");
str=st.nextToken();
int n;
if(str.length()>1){
n=Integer.parseInt(str.charAt(str.length()-2)+""+str.charAt(str.length()-1)+"");
}else{
n=Integer.parseInt(str.charAt(str.length()-1)+"");
}
if(m==0 && n==0 &&str.length()==1){
break;
}
if(m==0 ||m==1 ||m==5 ||m==6){
sb.append(m).append("\n");
}else if(n==0){
if(str.length()==1)
sb.append("1\n");
else{
sb.append(modP(m, 4,mods)).append("\n");
}
}else{
sb.append(modP(m, n,mods)).append("\n");
}
}
System.out.print(sb);
}
static int modP(int m,int n,int[]mods){
if(n%mods[m]>0)
n= n%mods[m];
else
n=mods[m];
int res=1;
for (int i = 0; i < n; i++) {
res*=m;
}
return res%10;
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb=new StringBuffer();
int[]mods={1, 1, 4, 4, 2, 1, 1, 4, 4, 2};
while(true){
StringTokenizer st=new StringTokenizer(br.readLine());
String str=st.nextToken();
int m=Integer.parseInt(str.charAt(str.length()-1)+"");
str=st.nextToken();
int n;
if(str.length()>1){
n=Integer.parseInt(str.charAt(str.length()-2)+""+str.charAt(str.length()-1)+"");
}else{
n=Integer.parseInt(str.charAt(str.length()-1)+"");
}
if(m==0 && n==0 &&str.length()==1){
break;
}
if(m==0 ||m==1 ||m==5 ||m==6){
sb.append(m).append("\n");
}else if(n==0){
if(str.length()==1)
sb.append("1\n");
else{
sb.append(modP(m, 4,mods)).append("\n");
}
}else{
sb.append(modP(m, n,mods)).append("\n");
}
}
System.out.print(sb);
}
static int modP(int m,int n,int[]mods){
if(n%mods[m]>0)
n= n%mods[m];
else
n=mods[m];
int res=1;
for (int i = 0; i < n; i++) {
res*=m;
}
return res%10;
}
}
No comments:
Post a Comment