Monday 21 January 2013

3442. The last digit Problem code: LASTDIG

#include<cmath>
#include<cstdio>
int main()
{
    long long int a , b , n, i , k;
    scanf("%lld", &n);
    for (i=0;i<n;i++)
    {
        scanf("%lld %lld", &a , &b);
               
        if(b==0&&a==0)
            k=1;
        else if (b==0 )
            k=1;
        else if (a==0)
            k=0;
        else
        {                   
            if (b%4==0)
                b = 4;
            else
                b=b%4;
            k = pow(a, b);
        }   
            k = k%10;   
            printf("%lld\n", k);       
    }
    return 0;
}

1 comment:

  1. what is the logic for if(b%4)?? why are we checking this condition ?

    ReplyDelete