Tuesday, April 20, 2010

The Last Digit of a^b

Given two integer numbers: the base a (0 < a < 20) and the index b (0 <= b <= 2,147,483,000). You have to find the last digit of a^b.

This is quite a simple problem to solve, provided you know a few rules. Here is the code which is pretty self explanatory:

import java.util.*;

public class Main {
  public static int f(int a, int b) {

    if(b==0)return 1;

    //find last digit of a
    int da = a % 10;

    if(da==0)return 0;
    if(da==5)return 5;
    switch(b%4){
    case 0:
      return da%2!=0?1:6;
    case 1:
      return da;
    case 2:
      return da*da%10;
    default:
      return da*da*da%10;
    }
  }
  public static void main(String[] a) {
    Scanner c = new Scanner(System.in);
    c.nextLine();
    while(c.hasNext())
      System.out.println(f(c.nextInt(),c.nextInt()));
  }
}
Links:
SPOJ LASTDIG: The last digit

1 comment:

  1. Can you please tell which rule have you used here?

    ReplyDelete