whats wrong..

to calculate:last digit of a^c
(a can be upto 1000 digits and c upto 18 digits)
plss… point out the fallacy in the code…

#include<iostream.h>
#include<math.h>
#include<string.h>
main()
{
      int t,x,p,m,l,k,i;
      unsigned long long s,r,c;
      char a[1001];
      cin>>t;
      while(t--)
      {
       x=c=0;a[0]='\0';
       gets(a);
       l=strlen(a);
       x=a[l-1]-48;
       cin>>c;
       if(!c) 
       cout<<"1\n";
       else if(x==0||x==1||x==5||x==6)
       cout<<x<<"\n";
       else if(x==2||x==3||x==7||x==8)
       {
         r=c%4;
         if(r==1||r==2||r==3) 
         p=pow(x,r);
         else 
         p=pow(x,4);
         cout<<p%10<<"\n";
       }
       else if(x==4||x==9)
       {
         s=c%2;
         if(s) 
         m=pow(x,s);
         else 
         m=pow(x,2);
         cout<<m%10<<"\n";
       }
}
return 0;
}

Your logic is perfectly alright!!!

The error is that u r using gets…instead use scanf or cin…as gets takes the whole line as input…that is if u do…

gets(a)

the input that is stored in a is:-

123 1234

so the length becomes 8 and the last digit becomes 4 also when u input the next number then the number from the next line is taken as input…!!!

also lastdig2 has a constraint on the size of your source code i.e. 700 bytes…so remove all unnecessary spaces in the code also remove all indentations…this will get u AC…hope this helps…:slight_smile:

this is your Ac code…LINK!!!

and also 1 more thing is use

<iostream>

instead of

<iostream.h>

and also use “using namespace std;” while using cin and cout…!!!

1 Like

lots f thanks … :slight_smile:

can u give the problem link!!!