Dado un número entero N , la tarea es encontrar el recuento total de números de N dígitos de modo que el XOR bit a bit de los dígitos de los números sea un solo dígito.
Ejemplos:
Entrada: N = 1
Salida: 9
Explicación:
1, 2, 3, 4, 5, 6, 7, 8, 9 son los números.Entrada: N = 2
Salida: 66
Explicación:
Hay 66 de esos números de 2 dígitos cuya Xor de dígitos es un número de un solo dígito.
Enfoque: el enfoque ingenuo será iterar sobre todos los números de N dígitos y verificar si el XOR bit a bit de todos los dígitos del número es un solo dígito. En caso afirmativo, incluya esto en el conteo; de lo contrario, verifique el siguiente número.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find count of N-digit // numbers with single digit XOR void countNums(int N) { // Range of numbers int l = (int)pow(10, N - 1); int r = (int)pow(10, N) - 1; int count = 0; for(int i = l; i <= r; i++) { int xorr = 0, temp = i; // Calculate XOR of digits while (temp > 0) { xorr = xorr ^ (temp % 10); temp /= 10; } // If XOR <= 9, // then increment count if (xorr <= 9) count++; } // Print the count cout << count; } // Driver Code int main() { // Given number int N = 2; // Function call countNums(N); } // This code is contributed by code_hunt
Java
// Java program for the above approach class GFG { // Function to find count of N-digit // numbers with single digit XOR public static void countNums(int N) { // Range of numbers int l = (int)Math.pow(10, N - 1), r = (int)Math.pow(10, N) - 1; int count = 0; for (int i = l; i <= r; i++) { int xor = 0, temp = i; // Calculate XOR of digits while (temp > 0) { xor = xor ^ (temp % 10); temp /= 10; } // If XOR <= 9, // then increment count if (xor <= 9) count++; } // Print the count System.out.println(count); } // Driver Code public static void main(String[] args) { // Given Number int N = 2; // Function Call countNums(N); } }
Python3
# Python3 program for the above approach # Function to find count of N-digit # numbers with single digit XOR def countNums(N): # Range of numbers l = pow(10, N - 1) r = pow(10, N) - 1 count = 0 for i in range(l, r + 1): xorr = 0 temp = i # Calculate XOR of digits while (temp > 0): xorr = xorr ^ (temp % 10) temp //= 10 # If XOR <= 9, # then increment count if (xorr <= 9): count += 1 # Print the count print(count) # Driver Code # Given number N = 2 # Function call countNums(N) # This code is contributed by code_hunt
C#
// C# program for the above approach using System; class GFG{ // Function to find count of N-digit // numbers with single digit XOR public static void countNums(int N) { // Range of numbers int l = (int)Math.Pow(10, N - 1), r = (int)Math.Pow(10, N) - 1; int count = 0; for(int i = l; i <= r; i++) { int xor = 0, temp = i; // Calculate XOR of digits while (temp > 0) { xor = xor ^ (temp % 10); temp /= 10; } // If XOR <= 9, // then increment count if (xor <= 9) count++; } // Print the count Console.WriteLine(count); } // Driver Code public static void Main() { // Given number int N = 2; // Function call countNums(N); } } // This code is contributed by code_hunt
Javascript
<script> // JavaScript program for the above approach // Function to find count of N-digit // numbers with single digit XOR function countNums(N) { // Range of numbers let l = Math.floor(Math.pow(10, N - 1)); let r = Math.floor(Math.pow(10, N)) - 1; let count = 0; for(let i = l; i <= r; i++) { let xorr = 0, temp = i; // Calculate XOR of digits while (temp > 0) { xorr = xorr ^ (temp % 10); temp = Math.floor(temp / 10); } // If XOR <= 9, // then increment count if (xorr <= 9) count++; } // Print the count document.write(count); } // Driver Code // Given number let N = 2; // Function call countNums(N); // This code is contributed by Surbhi Tyagi. </script>
66
Complejidad temporal: O(N*10 N )
Espacio auxiliar: O(1)
Enfoque Eficiente: La idea es usar Programación Dinámica . Observe que el máximo Bitwise XOR que se puede obtener es 15.
- Cree una tabla dp[][] , donde dp[i][j] almacene el recuento de números de i-dígitos de modo que su XOR sea j .
- Inicialice el dp[][] para i = 1 y para cada i forme 2 a N itere para cada dígito j de 0 a 9.
- Para cada XOR k anterior posible de 0 a 15, encuentre el valor haciendo XOR de XOR k anterior y el dígito j , e incremente el recuento de dp[i][valor] en dp[i – 1][k] .
- El recuento total de números de N dígitos con un XOR de un solo dígito se puede encontrar sumando dp[N][j] donde j varía de 0 a 9 .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for // the above approach #include<bits/stdc++.h> using namespace std; // Function to find // count of N-digit // numbers with single // digit XOR void countNums(int N) { // dp[i][j] stores the number // of i-digit numbers with // XOR equal to j int dp[N][16]; memset(dp, 0, sizeof(dp[0][0]) * N * 16); // For 1-9 store the value for (int i = 1; i <= 9; i++) dp[0][i] = 1; // Iterate till N for (int i = 1; i < N; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 16; k++) { // Calculate XOR int xo = j ^ k; // Store in DP table dp[i][xo] += dp[i - 1][k]; } } } // Initialize count int count = 0; for (int i = 0; i < 10; i++) count += dp[N - 1][i]; // Print the answer cout << (count) << endl; } // Driver Code int main() { // Given number N int N = 1; // Function Call countNums(N); } // This code is contributed by Chitranayal
Java
// Java program for the above approach class GFG { // Function to find count of N-digit // numbers with single digit XOR public static void countNums(int N) { // dp[i][j] stores the number // of i-digit numbers with // XOR equal to j int dp[][] = new int[N][16]; // For 1-9 store the value for (int i = 1; i <= 9; i++) dp[0][i] = 1; // Iterate till N for (int i = 1; i < N; i++) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 16; k++) { // Calculate XOR int xor = j ^ k; // Store in DP table dp[i][xor] += dp[i - 1][k]; } } } // Initialize count int count = 0; for (int i = 0; i < 10; i++) count += dp[N - 1][i]; // Print the answer System.out.println(count); } // Driver Code public static void main(String[] args) { // Given number N int N = 1; // Function Call countNums(N); } }
Python3
# Python3 program for the # above approach # Function to find count of # N-digit numbers with single # digit XOR def countNums(N): # dp[i][j] stores the number # of i-digit numbers with # XOR equal to j dp = [[0 for i in range(16)] for j in range(N)]; # For 1-9 store the value for i in range(1, 10): dp[0][i] = 1; # Iterate till N for i in range(1, N): for j in range(0, 10): for k in range(0, 16): # Calculate XOR xor = j ^ k; # Store in DP table dp[i][xor] += dp[i - 1][k]; # Initialize count count = 0; for i in range(0, 10): count += dp[N - 1][i]; # Print answer print(count); # Driver Code if __name__ == '__main__': # Given number N N = 1; # Function Call countNums(N); # This code is contributed by shikhasingrajput
C#
// C# program for the above approach using System; class GFG{ // Function to find count of N-digit // numbers with single digit XOR public static void countNums(int N) { // dp[i][j] stores the number // of i-digit numbers with // XOR equal to j int [,]dp = new int[N, 16]; // For 1-9 store the value for(int i = 1; i <= 9; i++) dp[0, i] = 1; // Iterate till N for(int i = 1; i < N; i++) { for(int j = 0; j < 10; j++) { for (int k = 0; k < 16; k++) { // Calculate XOR int xor = j ^ k; // Store in DP table dp[i, xor] += dp[i - 1, k]; } } } // Initialize count int count = 0; for(int i = 0; i < 10; i++) count += dp[N - 1, i]; // Print the answer Console.Write(count); } // Driver Code public static void Main(string[] args) { // Given number N int N = 1; // Function call countNums(N); } } // This code is contributed by rutvik_56
Javascript
<script> // javascript program for the above approach // Function to find count of N-digit // numbers with single digit XOR function countNums(N) { // dp[i][j] stores the number // of i-digit numbers with // XOR equal to j var dp = Array(N); for(var i =0;i<N;i++) dp[i] = Array(16).fill(0); // For 1-9 store the value for (i = 1; i <= 9; i++) dp[0][i] = 1; // Iterate till N for (i = 1; i < N; i++) { for (j = 0; j < 10; j++) { for (k = 0; k < 16; k++) { // Calculate XOR var xor = j ^ k; // Store in DP table dp[i][xor] += dp[i - 1][k]; } } } // Initialize count var count = 0; for (i = 0; i < 10; i++) count += dp[N - 1][i]; // Print the answer document.write(count); } // Driver Code // Given number N var N = 1; // Function Call countNums(N); // This code contributed by umadevi9616 </script>
9
Complejidad temporal: O(N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por jrishabh99 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA