Minimize steps required to make two values equal by repeated division by any of their prime factor which is less than M
Given three positive integers M, X, and Y, the task is to find the minimum number of operations required to make X and Y equal such that in each operation divide X or Y by one of its prime factor less than M. If it is not possible to make X and Y equal, then print “-1”.
Examples:
Input: X = 20, Y =16, M = 10
Output: 3
Explanation:
Perform the operation on X and Y as illustrate below:
X: 20 -> 4 : 20/5 = 4
Y: 16 -> 8 : 16/ 2 = 8, 8 -> 4 : 8/2 = 4
Therefore, the total number of steps required to make X and Y equal is 3.
Input: X =160, Y = 180, M = 10
Output: 5
Approach: The idea to solve the given problem is based on the observation that the value at which both X and Y are equal is the GCD of X and Y. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int primes[1000006];
int gcd( int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
void preprocess()
{
for ( int i = 1; i <= 1000000; i++)
primes[i] = i;
for ( int i = 2; i * i <= 1000000; i++)
{
if (primes[i] == i)
{
for ( int j = 2 * i; j <= 1000000; j += i)
{
if (primes[j] == j)
primes[j] = i;
}
}
}
}
int Steps( int x, int m)
{
int steps = 0;
bool flag = false ;
while (x > 1)
{
if (primes[x] > m)
{
flag = true ;
break ;
}
x /= primes[x];
steps++;
}
if (flag)
return -1;
return steps;
}
int minimumSteps( int x, int y, int m)
{
preprocess();
int g = gcd(x, y);
x = x / g;
y = y / g;
int x_steps = Steps(x, m);
int y_steps = Steps(y, m);
if (x_steps == -1 || y_steps == -1)
return -1;
return x_steps + y_steps;
}
int main()
{
int X = 160;
int Y = 180;
int M = 10;
cout << (minimumSteps(X, Y, M));
}
|
Java
import java.util.*;
public class GFG {
static int primes[] = new int [ 1000006 ];
static int gcd( int a, int b)
{
if (b == 0 )
return a;
else
return gcd(b, a % b);
}
static void preprocess()
{
for ( int i = 1 ; i <= 1000000 ; i++)
primes[i] = i;
for ( int i = 2 ; i * i <= 1000000 ; i++) {
if (primes[i] == i) {
for ( int j = 2 * i;
j <= 1000000 ; j += i) {
if (primes[j] == j)
primes[j] = i;
}
}
}
}
static int Steps( int x, int m)
{
int steps = 0 ;
boolean flag = false ;
while (x > 1 ) {
if (primes[x] > m) {
flag = true ;
break ;
}
x /= primes[x];
steps++;
}
if (flag)
return - 1 ;
return steps;
}
static int minimumSteps( int x, int y, int m)
{
preprocess();
int g = gcd(x, y);
x = x / g;
y = y / g;
int x_steps = Steps(x, m);
int y_steps = Steps(y, m);
if (x_steps == - 1 || y_steps == - 1 )
return - 1 ;
return x_steps + y_steps;
}
public static void main(String args[])
{
int X = 160 ;
int Y = 180 ;
int M = 10 ;
System.out.println(
minimumSteps(X, Y, M));
}
}
|
Python3
primes = [ 0 ] * ( 1000006 )
def gcd(a, b) :
if (b = = 0 ) :
return a
else :
return gcd(b, a % b)
def preprocess() :
for i in range ( 1 , 1000001 ):
primes[i] = i
i = 2
while (i * i < = 1000000 ) :
if (primes[i] = = i) :
for j in range ( 2 * i, 1000001 , i):
if (primes[j] = = j) :
primes[j] = i
i + = 1
def Steps(x, m) :
steps = 0
flag = False
while (x > 1 ) :
if (primes[x] > m) :
flag = True
break
x / / = primes[x]
steps + = 1
if (flag ! = 0 ) :
return - 1
return steps
def minimumSteps(x, y, m) :
preprocess()
g = gcd(x, y)
x = x / / g
y = y / / g
x_steps = Steps(x, m)
y_steps = Steps(y, m)
if (x_steps = = - 1 or y_steps = = - 1 ) :
return - 1
return x_steps + y_steps
X = 160
Y = 180
M = 10
print (minimumSteps(X, Y, M))
|
C#
using System;
class GFG{
static int [] primes = new int [1000006];
static int gcd( int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static void preprocess()
{
for ( int i = 1; i <= 1000000; i++)
primes[i] = i;
for ( int i = 2; i * i <= 1000000; i++)
{
if (primes[i] == i)
{
for ( int j = 2 * i;
j <= 1000000; j += i)
{
if (primes[j] == j)
primes[j] = i;
}
}
}
}
static int Steps( int x, int m)
{
int steps = 0;
bool flag = false ;
while (x > 1)
{
if (primes[x] > m)
{
flag = true ;
break ;
}
x /= primes[x];
steps++;
}
if (flag)
return -1;
return steps;
}
static int minimumSteps( int x, int y, int m)
{
preprocess();
int g = gcd(x, y);
x = x / g;
y = y / g;
int x_steps = Steps(x, m);
int y_steps = Steps(y, m);
if (x_steps == -1 || y_steps == -1)
return -1;
return x_steps + y_steps;
}
static void Main()
{
int X = 160;
int Y = 180;
int M = 10;
Console.Write(minimumSteps(X, Y, M));
}
}
|
Javascript
<script>
var primes = Array(1000006).fill(0);
function gcd(a , b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
function preprocess() {
for (i = 1; i <= 1000000; i++)
primes[i] = i;
for (i = 2; i * i <= 1000000; i++) {
if (primes[i] == i) {
for (j = 2 * i; j <= 1000000; j += i) {
if (primes[j] == j)
primes[j] = i;
}
}
}
}
function Steps(x , m) {
var steps = 0;
var flag = false ;
while (x > 1) {
if (primes[x] > m) {
flag = true ;
break ;
}
x /= primes[x];
steps++;
}
if (flag)
return -1;
return steps;
}
function minimumSteps(x , y , m) {
preprocess();
var g = gcd(x, y);
x = x / g;
y = y / g;
var x_steps = Steps(x, m);
var y_steps = Steps(y, m);
if (x_steps == -1 || y_steps == -1)
return -1;
return x_steps + y_steps;
}
var X = 160;
var Y = 180;
var M = 10;
document.write(minimumSteps(X, Y, M));
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Last Updated :
16 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...