Dados a, b y n. Encuentre x e y que satisfagan ax + by = n. Imprima cualquiera de los x e y que satisfagan la ecuación
Ejemplos:
Input : n=7 a=2 b=3 Output : x=2, y=1 Explanation: here x and y satisfies the equation Input : 4 2 7 Output : No solution
Podemos verificar si existe alguna solución o no usando ecuaciones diofánticas lineales , pero aquí necesitamos encontrar las soluciones para esta ecuación, por lo que podemos simplemente iterar para todos los valores posibles de 0 a n, ya que no puede exceder n para esta ecuación dada. Así que resolver esta ecuación con lápiz y papel da y=(n-ax)/b y de manera similar obtenemos que el otro número es x=(n-by)/a . Si ninguno de los valores satisface la ecuación, al final tenemos imprimir «sin solución»
C++
// CPP program to find solution of ax + by = n #include <bits/stdc++.h> using namespace std; // function to find the solution void solution(int a, int b, int n) { // traverse for all possible values for (int i = 0; i * a <= n; i++) { // check if it is satisfying the equation if ((n - (i * a)) % b == 0) { cout << "x = " << i << ", y = " << (n - (i * a)) / b; return; } } cout << "No solution"; } // driver program to test the above function int main() { int a = 2, b = 3, n = 7; solution(a, b, n); return 0; }
Java
// Java program to find solution // of ax + by = n import java.io.*; class GfG { // function to find the solution static void solution(int a, int b, int n) { // traverse for all possible values for (int i = 0; i * a <= n; i++) { // check if it is satisfying the equation if ((n - (i * a)) % b == 0) { System.out.println("x = " + i + ", y = " + (n - (i * a)) / b); return ; } } System.out.println("No solution"); } public static void main (String[] args) { int a = 2, b = 3, n = 7; solution(a, b, n); } } // This code is contributed by Gitanjali.
Python3
# Python3 code to find solution of # ax + by = n # function to find the solution def solution (a, b, n): # traverse for all possible values i = 0 while i * a <= n: # check if it is satisfying # the equation if (n - (i * a)) % b == 0: print("x = ",i ,", y = ", int((n - (i * a)) / b)) return 0 i = i + 1 print("No solution") # driver program to test the above function a = 2 b = 3 n = 7 solution(a, b, n) # This code is contributed by "Sharad_Bhardwaj".
C#
// C# program to find solution // of ax + by = n using System; class GfG { // function to find the solution static void solution(int a, int b, int n) { // traverse for all possible values for (int i = 0; i * a <= n; i++) { // check if it is satisfying the // equation if ((n - (i * a)) % b == 0) { Console.Write("x = " + i + ", y = " + (n - (i * a)) / b); return ; } } Console.Write("No solution"); } // Driver code public static void Main () { int a = 2, b = 3, n = 7; solution(a, b, n); } } // This code is contributed by Vt_m.
PHP
<?php // PHP program to find // solution of ax + by = n // function to find the solution function solution($a, $b, $n) { // traverse for all possible values for ($i = 0; $i * $a <= $n; $i++) { // check if it is satisfying // the equation if (($n - ($i * $a)) % $b == 0) { echo "x = " , $i , ", y = " , ($n - ($i * $a)) / $b; return; } } echo "No solution"; } // Driver Code $a = 2; $b = 3; $n = 7; solution($a, $b, $n); // This code is contributed by anuj_67. ?>
Javascript
<script> // JavaScript program to find solution // of ax + by = n // function to find the solution function solution(a, b, n) { // traverse for all possible values for (let i = 0; i * a <= n; i++) { // check if it is satisfying the equation if ((n - (i * a)) % b == 0) { document.write("x = " + i + ", y = " + (n - (i * a)) / b); return ; } } document.write("No solution"); } // Driver code let a = 2, b = 3, n = 7; solution(a, b, n); // This code is contributed by sanjoy_62. </script>
x = 2, y = 1
Complejidad de tiempo : O (n/a), ya que estamos usando un bucle para atravesar n/a veces.
Espacio Auxiliar : O(1), ya que no se ha tomado ningún espacio extra.