Dado un entero N , la tarea es encontrar dos pares de enteros positivos tales que el MCD del primer par sea el mismo que el MCM del segundo par y la suma de los valores sea igual a N.
Nota: Si es posible obtener varias salidas, imprima cualquiera de ellas
Ejemplos:
Entrada: N = 9
Salida: 6 1 1 1
Explicación: MCD(6, 1) = 1 y MCM(1, 1) = 1, MCD es igual a MCM.
La suma de todos los números (6 + 1 + 1 + 1) = 9Entrada: N = 3
Salida: -1
Explicación: No podemos dar valores enteros positivos a los cuatro valores.
Planteamiento: El planteamiento del problema se basa en la siguiente observación:
El MCD de cualquier número con 1 es siempre 1 y el MCM de 1 y 1 siempre será 1.
Entonces, fije el primer número del MCD N-3 y el segundo como 1 y todos los demás números del MCM como 1 y 1.
Entonces, al final, el la suma de todos los números ( N -3 + 1 + 1 + 1) será N y MCD ( N -3, 1) será igual a LCM (1, 1).
Siga los pasos mencionados a continuación para implementar la observación:
- Si N < 4 , entonces no es posible encontrar cuatro de esos valores.
- De lo contrario, encuentre los cuatro valores según la observación anterior.
A continuación se muestra el código para la implementación anterior:
C
// C code for above implementation #include <stdio.h> // Function to find the pairs whose GCD and // LCM are equal and their sum equal to N void find(int n) { // If there are several possible answers // you can output any of them. If not // exist then print -1 if (n < 4) { printf("-1\n"); } else { int a, b, c, d; a = n - 3; b = 1; c = 1; d = 1; printf("%d %d %d %d",a,b,c,d); } } // Driver code void main() { int N = 9; // Function call find(9); } // This code is contributed by ashishsingh13122000
C++
// C++ code for above implementation #include <bits/stdc++.h> using namespace std; // Function to find the pairs whose GCD and // LCM are equal and their sum equal to N void find(int n) { // If there are several possible answers // you can output any of them. If not // exist then print -1 if (n < 4) { cout << -1 << endl; } else { int a, b, c, d; a = n - 3; b = 1; c = 1; d = 1; cout << a << " " << b << " " << c << " " << d; } } // Driver code int main() { int N = 9; // Function call find(9); return 0; }
Java
// Java code for above implementation import java.io.*; class GFG { // Function to find the pairs whose GCD and // LCM are equal and their sum equal to N public static void find(int n) { // If there are several possible answers // you can output any of them. If not // exist then print -1 if (n < 4) { System.out.println(-1); } else { int a, b, c, d; a = n - 3; b = 1; c = 1; d = 1; System.out.print(a + " " + b + " " + c + " " + d); } } // Driver Code public static void main(String[] args) { int N = 9; // Function call find(9); } } // This code is contributed by Rohit Pradhan
Python3
# Python 3 code for above implementation # Function to find the pairs whose GCD and # LCM are equal and their sum equal to N def find(n): # If there are several possible answers # you can output any of them. If not # exist then print -1 if (n < 4): print("-1",end=" ") else: a = n - 3 b = 1 c = 1 d = 1 print(a, end=" ") print(b, end=" ") print(c, end=" ") print(d, end=" ") # Driver code N = 9 # Function call find(9) # This code is contributed by ashishsingh13122000.
C#
// C# code for above implementation using System; class GFG { // Function to find the pairs whose GCD and // LCM are equal and their sum equal to N static void find(int n) { // If there are several possible answers // you can output any of them. If not // exist then print -1 if (n < 4) { Console.WriteLine(-1); } else { int a, b, c, d; a = n - 3; b = 1; c = 1; d = 1; Console.Write(a + " " + b + " " + c + " " + d); } } // Driver Code public static void Main() { int N = 9; // Function call find(9); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for above implementation // Function to find the pairs whose GCD and // LCM are equal and their sum equal to N function find(n){ // If there are several possible answers // you can output any of them. If not // exist then print -1 if (n < 4) document.write("-1"," ") else{ let a = n - 3 let b = 1 let c = 1 let d = 1 document.write(a," ") document.write(b," ") document.write(c," ") document.write(d," ") } } // Driver code let N = 9 // Function call find(9) // This code is contributed by shinjanpatra </script>
6 1 1 1
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)