I had implemented the solution with all datatypes as in long too…but I got runtime error in those 2 cases for which I am getting wrong answer in cases of this int approach…
I am not able to understand why I am getting plartially correct
I have done a correct solution…but I do want now too…that in my previous solutions I was getting runtime error for some cases
check for this code …
import java.util.; public class Main { public static void main(String args[]) { int t; Scanner scan=new Scanner(System.in); t=scan.nextInt(); while(t–!=0) { long n,p,a; a=scan.nextLong(); n=scan.nextLong(); p=scan.nextLong(); System.out.println(findanswer(a,n,p)); } } public static long findanswer(long a,long n,long p) { if(n==1l) { return a%p; } long temp=findanswer(a,n-1,p); long temp1=findpower(temp,n-1,p); return (temptemp1)%p; } public static long findpower(long x,long y,long p) { if(y==1l) { return x%p; } long temp=findpower(x,y/2,p); if(y%2l==0l) { return (temptemp)%p; } else { long temp1=(temptemp)%p; return (temp1*x)%p; } } }
Check for the code in which I got 80 score…
Cant read your code like this.
Just do this -:
int ans = findpower(a,n);
reverse loop from n-1 to 1:
{
ans = findpower(ans,i)%p;
ans %= p;
}
print ans
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.