Dados dos enteros A y B , la tarea es encontrar un triplete (X, Y, Z) tal que todos ellos sean divisibles por A , exactamente uno de ellos sea divisible tanto por A como por B , y X + Y = Z .
Ejemplo:
Entrada: A = 5, B = 3
Salida: 10 50 60
Explicación: Para el triplete (10, 50, 60), todos ellos son divisibles por 5, 60 es divisible por 5 y 3, y 10 + 50 = 60 Por lo tanto, (10, 50, 60) es un triplete válido. Otros posibles tripletes son (5, 25, 30), (5, 15, 20)Entrada: A = 7, B = 11
Salida: 28 154 182
Enfoque: El problema dado es un problema basado en la observación que se puede resolver usando matemáticas básicas . Se puede observar que el triplete (A, A * B, A * (B + 1)) , satisface todas las condiciones dadas excepto cuando el valor de B es 1 . En ese caso, se puede ver que siempre se violará la condición de que exactamente uno de ellos sea divisible tanto por A como por B. Entonces, si B = 1 , no existe un triplete válido; de lo contrario, imprima (A, A * B, A * (B + 1)) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program of the above approach #include <iostream> using namespace std; // Function to find a triplet (X, Y, Z) // such that all of them are divisible // by A, exactly one of them is divisible // by both A and B, and X + Y = Z void findTriplet(int A, int B) { // If the value of B is 1 if (B == 1) { cout << -1; return; } // Print Answer cout << A << " " << A * B << " " << A * (B + 1); } // Driver Code int main() { int A = 5; int B = 3; findTriplet(A, B); return 0; }
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to find a triplet (X, Y, Z) // such that all of them are divisible // by A, exactly one of them is divisible // by both A and B, and X + Y = Z static void findTriplet(int A, int B) { // If the value of B is 1 if (B == 1) { System.out.println(-1); return; } // Print Answer System.out.println(A + " " + A * B + " " + A * (B + 1)); } // Driver Code public static void main (String[] args) { int A = 5; int B = 3; findTriplet(A, B); } } // This code is contributed by hrithikgarg03188.
Python3
# Python code for the above approach # Function to find a triplet (X, Y, Z) # such that all of them are divisible # by A, exactly one of them is divisible # by both A and B, and X + Y = Z def findTriplet(A, B): # If the value of B is 1 if (B == 1): print(-1) return # Print Answer print(f"{A} {A * B} {A * (B + 1)}") # Driver Code A = 5 B = 3 findTriplet(A, B) # This code is contributed by Saurabh Jaiswal
C#
// C# program of the above approach using System; class GFG { // Function to find a triplet (X, Y, Z) // such that all of them are divisible // by A, exactly one of them is divisible // by both A and B, and X + Y = Z static void findTriplet(int A, int B) { // If the value of B is 1 if (B == 1) { Console.Write(-1); return; } // Print Answer Console.Write(A + " " + A * B + " " + A * (B + 1)); } // Driver Code public static int Main() { int A = 5; int B = 3; findTriplet(A, B); return 0; } } // This code is contributed by Taranpreet
Javascript
<script> // JavaScript code for the above approach // Function to find a triplet (X, Y, Z) // such that all of them are divisible // by A, exactly one of them is divisible // by both A and B, and X + Y = Z function findTriplet(A, B) { // If the value of B is 1 if (B == 1) { document.write(-1); return; } // Print Answer document.write(A + " " + A * B + " " + A * (B + 1)); } // Driver Code let A = 5; let B = 3; findTriplet(A, B); // This code is contributed by Potta Lokesh </script>
5 15 20
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por hrithikgarg03188 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA