Have a look at my "N independent " solution(except when a=b), of course it is wrong but it still passes . Test cases where it should have failed… some test cases :
A=10, B=2, N=1 my answer=8(correct is 4)
A=12, B=3,N=1 my answer=9(correct is 3)
A=18,B=2,N>2
A=22,B=6,N>2
A=26,B=10,N>2
there are many many more test cases where it should have failed. Test cases were very weak. I understand that making good test cases is difficult task but this time they are extremely weak.
As mentioned by @khiljee and @bvsbrk in the above posts, I used the fact that gcd(a^n + b^n, a-b) = gcd(2*b^n, a-b)
Use prime factorization to find GCD as below:
If we have the unique prime factorizations of a = p1^e1 p2^e2 ⋅⋅⋅ pm^em and b = p1^f1 p2^f2 ⋅⋅⋅ pm^fm* where ei ≥ 0 and fi ≥ 0, then the gcd of a and b is gcd(a,b) = p1^min(e1,f1) * p2^min(e2,f2) * ⋅⋅⋅ pm^min(em,fm).
I was failing some test cases while using the prime factorization approach as above. Which I fixed while the competition approached its end.
The case being, we can compute primes for 2*b and a-b separately and extend the solution to know primes for 2b^n, and hence find GCD. If you use 2b to compute prime, say 2 happened x times in prime factorization of 2*b, while extending the solution 2*b^n we should raise power as (x-1)*n + 1 instead of x*n times.
My solution was to notice that A - B is smaller than A ^ N + B ^ N. Then we can find all the factors of A - B, and for each one of them check whether or not that factor of A - B divides A ^ N + B ^ N and by taking the maximum of all of these numbers we find the gcd(A - B, A ^ N + B ^ N).
Can’t we use:
long long d = (bpow(a, n, MOD) + bpow(b, n, MOD)) % (MOD);//MOD=10^9+7
instead of
long long d = (bpow(a, n, a - b) + bpow(b, n, a - b)) % (a - b);
Can anyone explain me the prod function in tester’s solution,
long long prod(long long a, long long b, long long mod = MOD)
{
long long res = 0;
while(b)
{
if(b & 1)
res = (res + a) % mod;
a = (a + a) % mod;
b >>= 1; // right shift
}
return res;
}
I see it is similar to fast exponentiation but I don’t really get this code
You are referring to tester’s solution.
We are using the following identity: GCD(A^N+B^N,A-B)=GCD((A^N+B^N)mod(A-B),A-B).
Although the said solution does not output the result mod 10^9+7 which, I would think, it should.
Thank you, for a few days, I was stuck at this idea trying to prove or invalidate it. Finally I scrapped it, and now it turns out it was wrong. Thanks for giving a fail case