Friday, 7 August 2015

LASTDIG

Standard

The last digit

Link to the question : LASTDIG 

HINT :

No need to worry about the constraints. Take out a pen and paper and notice the pattern of last digit for the powers of number 1 to 9. And then derive a general formula to shorten your code as the source limit is very small.

SOURCE CODE :


#include <stdio.h>
  int main(void) {
long long int a,p,q,b;
int t;
scanf("%d",&t);
while(t--)   {
scanf("%lld %lld",&a,&b);
p=a%10;
q=b%4;
if(b==0)
    printf("1\n");
else if(p==1||p==0||p==5||p==6)
printf("%d\n",p);
else if(q==1)
    printf("%d\n",p);
else if(q==2)
    printf("%d\n",((p*p)%10));
else if(q==3)
    printf("%d\n",((p*p*p)%10));
else if(q==0)
    printf("%d\n",((p*p*p*p)%10));
}
return 0;
}

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. The solution is giving wa by the spoj judges

    ReplyDelete