We can rewrite nn modmod x=kx = k as xp=nk{x*p = n - k} where pp is any arbitrary integer. So, it’s clear that xx will be a divisor of nkn - k which is strictly greater than kk and the same goes for yy. Now to obtain maximum gcdgcd we’ve to take nkn - k as yy and the biggest divisor of yy as xx i.ei.e y=nk{y = n - k}, x=x = biggest divisor of yy which is not equal to yy and greater than kk. By doing this we can ensure gcd(x,y)=x{gcd(x, y) = x} because xx divides both xx and yy and this is the maximum gcdgcd that we can obtain. Now to find the biggest divisor of yy, we have to factorize yy. Let pp be the smallest prime factor of yy where y/p>k{y / p > k}, then x=y/p{x = y / p}. Note that, prime factorization in O(y){O( \sqrt y)} won’t fit in the time limit. So we’ve to factorize yy by only the prime numbers which can be found by Sieve of Eratosthenes. There are a total of 7849878498 primes under 10610^6. So, it will easily fit in the limit.

Corner Cases: If y=1y = 1 or yky \le k then the answer will be 1-1. If yy is a prime number and k=0k = 0, the answer will be 11. If yy is a prime and k>0k > 0 then the answer will be 1-1.

Contributors

Statistics

53% Solution Ratio
sayeef006Earliest, Jul '21
Nafis2003174.132453Fastest, 0.0s
Nafis2003174.132453Lightest, 5.5 kB
D3structorShortest, 534B
Toph uses cookies. By continuing you agree to our Cookie Policy.