给定一个整数N ,任务是找到 N 位数字的总数,使得这些数字的位异或是一位数。
例子:
Input: N = 1
Output: 9
Explanation:
1, 2, 3, 4, 5, 6, 7, 8, 9 are the numbers.
Input: N = 2
Output: 66
Explanation:
There are 66 such 2-digit numbers whose Xor of digits is a single digit number.
方法:最简单的方法是遍历所有 N 位数字并检查该数字所有数字的按位异或是否为单个数字。如果是,则将其包含在计数中,否则,检查下一个数字。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
C++
// C++ program for
// the above approach
#include
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
输出:
66
时间复杂度: O(N*10 N )
辅助空间: O(1)
高效的方法:这个想法是使用动态规划。观察可以得到的最大Bitwise XOR是 15。
- 创建一个表 dp[][] ,其中dp[i][j]存储 i 位数字的计数,使得它们的 XOR 为j 。
- 为 i = 1 初始化 dp[][] 并对每个i形式从 2 到 N 迭代从 0 到 9 的每个数字j 。
- 对于从 0 到 15 的每个可能的先前 XOR k ,通过对先前的 XOR k和数字j进行 XOR 来找到该值,并将dp[i][value]的计数增加dp[i – 1][k] 。
- 可以通过对dp[N][j]求和,其中 j 的范围从0 到 9来找到具有一位数异或的 N 位数字的总数。
下面是上述方法的实现:
C++
// C++ program for
// the above approach
#include
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);
}
}
蟒蛇3
# 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
输出:
9
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。