Given two integers n and m. Discover the variety of steady pairs potential with gcd(n, m) = 1, whereby every step 1 will be incremented to the pair.
Examples:
Enter: n = 1, m = 4
Output: 2
Rationalization: gcd(1, 4) =1, within the subsequent step gcd(2, 5) =1 so the utmost potential steady pairs are 2.Enter: n = 5, m = 13
Output:1
Rationalization: Solely that pair is feasible with gcd(n, m) =1 as a result of within the subsequent step n = 6, m = 14 which has gcd = 2 so the output is 1.
Strategy: This may be solved with the under thought:
We have to discover the okay such that gcd(n+okay, m+okay) is larger than 2, so there needs to be no less than 1 prime quantity that divides each n+okay, and m+okay, excluding distinctive instances when n=m and n = m+1. There needs to be a quantity p that divides each m+okay and n+okay.
Therefore the distinction (n-m) % p = 0, so we solely want to have a look at prime elements of n-m such that n+okay % p =0 so we take the minimal worth of p – npercentp which is the variety of steps it may be incremented in order that gcd (n, m) is 1.
Comply with the steps talked about under to unravel the issue:
- Construct the sieve to retailer the smallest prime elements
- Discover the present gcd and if the preliminary gcd just isn’t equal to 1 then return 0.
- Swap n, m if n > m
- Discover the distinction and examine if the distinction is 1, If the distinction is 1 return -1 since gcd shall be 1 for infinite steps
- Discover all of the prime elements of the distinction utilizing factorize operate.
- Initialize the variable pairs to search out the variety of steady pairs
- Iterate by means of the prime elements and examine the pairs which can be potential by taking a minimal of p-npercentp. Return the variety of pairs
Beneath is the implementation of the above method:
C++
// C++ code for the above method
#embrace <bits/stdc++.h>
utilizing namespace std;
int N = 10000005;
vector<lengthy lengthy> spf(N + 1, 1);
// Construct the sieve to retailer
// smallest prime elements
void build_sieve()
{
lengthy lengthy i, j;
for (i = 2; i < N; i++) {
if (spf[i] == 1) {
spf[i] = i;
for (j = i * i; j <= N; j += i) {
if (spf[j] == 1)
spf[j] = i;
}
}
}
}
// Operate to get the prime
// issue of the quantity n
vector<int> factorize(int n)
{
vector<int> ans;
whereas (n > 1) {
int reality = spf[n];
whereas (n % reality == 0) {
n /= reality;
}
ans.push_back(reality);
}
return ans;
}
// Operate to search out the utmost
// potential steady pairs
int find_maxpairs(int n, int m)
{
// Calling the build_sieve
build_sieve();
// Discover the present gcd and if preliminary
// gcd just isn't equal to 1 then return 0.
int gcd = __gcd(n, m);
if (gcd != 1) {
return 0;
}
// Swap n, m if n > m
if (n > m)
swap(n, m);
// Discover the distinction and examine if
// the distinction is 1, If the
// distinction is 1 return -1 since
// gcd shall be 1 for infinite steps
int diff = m - n;
if (diff == 1) {
return -1;
}
// Discover all of the prime elements
// of the distinction
vector<int> prime_factors = factorize(diff);
// Initialize the variable pairs to
// discover the variety of steady pairs
int pairs = INT_MAX;
// Iterate by means of the prime elements
// and examine the pairs that
// are potential.
for (auto p : prime_factors) {
int to_be_added = p - (n % p);
pairs = min(pairs, to_be_added);
}
// Return the variety of pairs
return pairs;
}
// Driver Code
int most important()
{
int n = 1;
int m = 4;
// Operate name
cout << find_maxpairs(n, m) << endl;
return 0;
}
Time Complexity: O(NlogN ) the place N is the dimensions of the sieve.
Auxiliary Area: O(N)