Dados dos números a y b hallar todos los x tales que a % x = b.
Ejemplos:
Input : a = 21, b = 5 Output : 2 The answers of the Modular Equation are 8 and 16 since 21 % 8 = 21 % 16 = 5 .
Aquí surgen 3 casos:
- Si (a <b) entonces no habrá respuesta.
- Si (a = b) , entonces todos los números mayores que a son la respuesta, por lo que habrá infinitas soluciones posibles.
- Si ( a > b ) Supongamos que x es una respuesta a nuestra ecuación. Entonces x divide (a – b). También como a % x = b entonces b < x. Estas condiciones son necesarias y suficientes también. Entonces, la respuesta es el número de divisores de a – b que son estrictamente mayores que b y que se pueden resolver en O(sqrt(ab)) . Aquí solo surge un caso que tenemos que tratar por separado cuando (ab) es un cuadrado perfecto, luego sumaremos su raíz cuadrada dos veces, por lo que tenemos que restar una vez, si se presenta este caso.
C++
// C++ program to find x such that a % x is equal // to b. #include <bits/stdc++.h> using namespace std; void modularEquation(int a, int b) { // if a is less than b then no solution if (a < b) { cout << "No solution possible " << endl; return; } // if a is equal to b then every number // greater than a will be the solution // so its infinity if (a == b) { cout << "Infinite Solution possible " << endl; return; } // all resultant number should be greater than // b and (a-b) should be divisible by resultant // number // count variable store the number of values // possible int count = 0; int n = a - b; int y = sqrt(a - b); for (int i = 1; i <= y; ++i) { if (n % i == 0) { // checking for both divisor and quotient // whether they divide ( a-b ) completely // and greater than b . if (n / i > b) count++; if (i > b) count++; } } // Here y is added twice in the last iteration // so 1 y should be decremented to get correct // solution if (y * y == n && y > b) count--; cout << count << endl; } // Driver code int main() { int a = 21, b = 5; modularEquation(a, b); return 0; }
Java
// Java program to find x such that // a % x is equal to b. import java.io.*; class GFG { static void modularEquation(int a, int b) { // if a is less than b then no solution if (a < b) { System.out.println("No solution possible "); return; } // if a is equal to b then every number // greater than a will be the solution // so its infinity if (a == b) { System.out.println("Infinite Solution possible "); return; } // all resultant number should be greater // than b and (a-b) should be divisible // by resultant number // count variable store the number of // values possible int count = 0; int n = a - b; int y = (int)Math.sqrt(a - b); for (int i = 1; i <= y; ++i) { if (n % i == 0) { // checking for both divisor and // quotient whether they divide // ( a-b ) completely and // greater than b . if (n / i > b) count++; if (i > b) count++; } } // Here y is added twice in the last // iteration so 1 y should be decremented // to get correct solution if (y * y == n && y > b) count--; System.out.println(count); } // Driver code public static void main(String[] args) { int a = 21, b = 5; modularEquation(a, b); } } // This code is contributed by Prerna Saini
Python3
# Python3 program to find x such # that a % x is equal to b. import math def modularEquation(a, b) : # if a is less than b then no solution if (a < b) : print("No solution possible ") return # if a is equal to b then every number # greater than a will be the solution # so its infinity if (a == b) : print("Infinite Solution possible ") return # all resultant number should be # greater than b and (a-b) should # be divisible by resultant number # count variable store the number # of values possible count = 0 n = a - b y = (int)(math.sqrt(a - b)) for i in range(1, y+1) : if (n % i == 0) : # checking for both divisor # and quotient whether they # divide ( a-b ) completely # and greater than b . if (n / i > b) : count = count + 1 if (i > b) : count = count + 1 # Here y is added twice in the # last iteration so 1 y should be # decremented to get correct # solution if (y * y == n and y > b) : count = count - 1 print (count) # Driver code a = 21 b = 5 modularEquation(a, b) # This code is contributed by Nikita Tiwari.
C#
// C# program to find x such that // a % x is equal to b. using System; class GFG { static void modularEquation(int a, int b) { // if a is less than b then no solution if (a < b) { Console.WriteLine("No solution possible "); return; } // if a is equal to b then every number // greater than a will be the solution // so its infinity if (a == b) { Console.WriteLine("Infinite Solution possible "); return; } // all resultant number should be greater // than b and (a-b) should be divisible // by resultant number // count variable store the number of // values possible int count = 0; int n = a - b; int y = (int)Math.Sqrt(a - b); for (int i = 1; i <= y; ++i) { if (n % i == 0) { // checking for both divisor and // quotient whether they divide // ( a-b ) completely and // greater than b . if (n / i > b) count++; if (i > b) count++; } } // Here y is added twice in the last // iteration so 1 y should be decremented // to get correct solution if (y * y == n && y > b) count--; Console.WriteLine(count); } // Driver code public static void Main() { int a = 21, b = 5; modularEquation(a, b); } } //This code is contributed by vt_m.
PHP
<?php // PHP program to find x // such that a % x is equal // to b. function modularEquation($a, $b) { // if a is less than b // then no solution if ($a < $b) { echo "No solution possible " ; return; } // if a is equal to b // then every number // greater than a will // be the solution // so its infinity if ($a == $b) { echo "Infinite Solution possible "; return; } // all resultant number // should be greater than // b and (a-b) should be // divisible by resultant // number // count variable store // the number of values // possible $count = 0; $n = $a - $b; $y = sqrt($a - $b); for ( $i = 1; $i <= $y; ++$i) { if ($n % $i == 0) { // checking for both // divisor and quotient // whether they divide // ( a-b ) completely // and greater than b . if ($n / $i > $b) $count++; if ($i > $b) $count++; } } // Here y is added twice // in the last iteration // so 1 y should be // decremented to get correct // solution if ($y * $y == $n && $y > $b) $count--; echo $count ; } // Driver Code $a = 21; $b = 5; modularEquation($a, $b); // This code is contributed by anuj_67. ?>
Javascript
<script> // Javascript program to find x // such that a % x is equal // to b. function modularEquation(a, b) { // If a is less than b // then no solution if (a < b) { document.write("No solution possible "); return; } // If a is equal to b then every // number greater than a will // be the solution so its infinity if (a == b) { document.write("Infinite Solution possible "); return; } // All resultant number should be // greater than b and (a-b) should be // divisible by resultant number // Count variable store the number // of values possible let count = 0; let n = a - b; let y = Math.sqrt(a - b); for(let i = 1; i <= y; ++i) { if (n % i == 0) { // checking for both // divisor and quotient // whether they divide // ( a-b ) completely // and greater than b . if (n / i > b) count++; if (i > b) count++; } } // Here y is added twice // in the last iteration // so 1 y should be // decremented to get correct // solution if (y * y == n && y > b) count--; document.write(count); } // Driver Code let a = 21; let b = 5; modularEquation(a, b); // This code is contributed by _saurabh_jaiswal. </script>
Producción:
2
Publicación traducida automáticamente
Artículo escrito por Surya Priy y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA