Dados dos números enteros N y K , la tarea es verificar si N deja solo residuos distintos cuando se divide por todos los números enteros en el rango [1, K] . Si es así, imprima Sí . De lo contrario , imprima No.
Ejemplos:
Entrada: N = 5, K = 3
Salida: Sí
Explicación:
(5 % 1) == 0
(5 % 2) == 1
(5 % 3) == 2
Dado que todos los residuos {0, 1, 2} son distinto.
Entrada: N = 5, K = 4
Salida: No
Explicación:
(5 % 1) == 0
(5 % 2) == 1
(5 % 3) == 2
(5 % 4) == 1, que no es distinto.
Enfoque:
siga los pasos que se indican a continuación para resolver el problema:
- Inicializar un conjunto S
- Iterar sobre el rango [1, K] .
- En cada iteración, verifique si N % i ya está presente en el Conjunto S o no.
- Si no está presente, inserte N % i en el conjunto S
- De lo contrario, imprima No y termine.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to check if all // remainders are distinct or not #include <bits/stdc++.h> using namespace std; // Function to check and return // if all remainders are distinct bool is_distinct(long long n, long long k) { // Stores the remainder unordered_set<long long> s; for (int i = 1; i <= k; i++) { // Calculate the remainder long long tmp = n % i; // If remainder already occurred if (s.find(tmp) != s.end()) { return false; } // Insert into the set s.insert(tmp); } return true; } // Driver Code int main() { long long N = 5, K = 3; if (is_distinct(N, K)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java program to check if all // remainders are distinct or not import java.util.*; class GFG{ // Function to check and return // if all remainders are distinct static boolean is_distinct(long n, long k) { // Stores the remainder HashSet<Long> s = new HashSet<Long>(); for(int i = 1; i <= k; i++) { // Calculate the remainder long tmp = n % i; // If remainder already occurred if (s.contains(tmp)) { return false; } // Insert into the set s.add(tmp); } return true; } // Driver Code public static void main(String[] args) { long N = 5, K = 3; if (is_distinct(N, K)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by gauravrajput1
Python3
# Python3 program to check if all # remainders are distinct or not # Function to check and return # if all remainders are distinct def is_distinct(n, k): # Stores the remainder s = set() for i in range(1, k + 1): # Calculate the remainder tmp = n % i # If remainder already occurred if (tmp in s): return False # Insert into the set s.add(tmp) return True # Driver Code if __name__ == '__main__': N = 5 K = 3 if (is_distinct(N, K)): print("Yes") else: print("No") # This code is contributed by Shivam Singh
C#
// C# program to check if all // remainders are distinct or not using System; using System.Collections.Generic; class GFG{ // Function to check and return // if all remainders are distinct static bool is_distinct(long n, long k) { // Stores the remainder HashSet<long> s = new HashSet<long>(); for(int i = 1; i <= k; i++) { // Calculate the remainder long tmp = n % i; // If remainder already occurred if (s.Contains(tmp)) { return false; } // Insert into the set s.Add(tmp); } return true; } // Driver Code public static void Main(String[] args) { long N = 5, K = 3; if (is_distinct(N, K)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by gauravrajput1
Javascript
<script> // Javascript program to check if all // remainders are distinct or not // Function to check and return // if all remainders are distinct function is_distinct(n, k) { // Stores the remainder let s = new Set(); for(let i = 1; i <= k; i++) { // Calculate the remainder let tmp = n % i; // If remainder already occurred if (s.has(tmp)) { return false; } // Insert leto the set s.add(tmp); } return true; } // Driver code let N = 5, K = 3; if (is_distinct(N, K)) document.write("Yes"); else document.write("No"); // This code is contributed by souravghosh0416. </script>
Producción:
Yes
Complejidad temporal: O(K)
Espacio auxiliar: O(K)