We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Alas this solution is not correct in general (though it may pass the test cases, leaves room for improvement ;-)
Eulers formula with the totient phi only holds for . (here: n1=base, n=modulo).
The correct way is to split in factors and use chinese remainder theorem, see the editorial for one way to do it.
Your point about Devu Vs Police looked me little confusing but no worries I have shared your post with one of my old https://assignmentjunkie.co.uk/do-my-assignment collegue.I shall share the reviews little later.Thanks.
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
long long n1,k1,n2,k2,n,t,i,f,j,l;
cin>>t;
for(i=0;i<t;i++)
{cin>>n1>>k1>>n2>>k2>>n;
if(n1==0 && k1==0)
j=1;
j=pow(n1,k1);
if(n2==0 && k2==0)
l=1;
l=pow(n2,k2);
if (j==0 && l==0)
f=1;
f=pow(j,l);
cout<<f%n<<'\n';
}
giving error in some tests
dont know what is wrong with this code?
Simple you have problems with NZEC about powers of pow(10⁹,10⁹) because is large number and passing of limit in your hardware and language number limits. About thar I have same problem in various languages and the secret here is discover some rules for use power module and succeed on all tests. Good luck in your code.
No more comments
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Python3 solution
Alas this solution is not correct in general (though it may pass the test cases, leaves room for improvement ;-)
Eulers formula with the totient phi only holds for . (here: n1=base, n=modulo).
The correct way is to split in factors and use chinese remainder theorem, see the editorial for one way to do it.
Your point about Devu Vs Police looked me little confusing but no worries I have shared your post with one of my old https://assignmentjunkie.co.uk/do-my-assignment collegue.I shall share the reviews little later.Thanks.
static BigInteger solve(int n1, int k1, int n2, int k2, int n) { String s1 = String.valueOf(n1); String s2 = String.valueOf(n2); String s3 = String.valueOf(n); BigInteger b1, b2, b3; b1 = new BigInteger(s1); b2 = new BigInteger(s2); b3 = new BigInteger(s3); b1 = b1.pow(k1); b2 = b2.pow(k2); int i = b2.intValue(); BigInteger result = b1.pow(i); result = result.mod(b3); return result; }
failed some test cases due to time limits please help me to reduce my code :-)
Hints:
(1) Euler's theorem: if a and n are coprime positive integers, we have
where phi() is Euler's totient function. Fermat's little theorem is a special case of this where n is prime.
(2) What if a and n are not coprime? Try this:
where d=gcd(a,n) is the greatest common divisor of a and n.
These are the main ingredients of this problem. Watch out for boundary cases.
Simple you have problems with NZEC about powers of pow(10⁹,10⁹) because is large number and passing of limit in your hardware and language number limits. About thar I have same problem in various languages and the secret here is discover some rules for use power module and succeed on all tests. Good luck in your code.