不同数量的 XOR
Distinct number of XORs
鉴于:
两个二进制字符串x和y长度相同;以 arbitrary
的方式重新排列 x 的字符,并以 arbitrary
[=27] 的方式重新排列 y 的字符=](不一定相同)方式。
得到的不同异或的总数是多少?
注:
if x = 1 and y = 11 in binary then take x = 01 So,that length of x and y are equal
示例:
if x = 0 and y = 10
那么可能的结果是 2,即 2 个不同的 XOR 是可能的
"00" XOR "10" is "10"
"00" XOR "01" is "01"
"00" XOR "10" is "10"
"00" XOR "01" is "01"
希望这就是您想要的:
代码:
def f(x, y):
# Convert integers to string
x, y = str(x), str(y)
# Pad inputs
x, y = x.zfill(max(len(x), len(y))), y.zfill(max(len(x), len(y)))
rv = set()
for x_comb in set(''.join(perm_x) for perm_x in itertools.permutations(x)):
for y_comb in set(''.join(perm_y) for perm_y in itertools.permutations(y)):
rv.add(f"{x_comb} XOR {y_comb} is {int(x_comb,2) ^ int(y_comb,2):b}")
return list(rv)
用法示例:
>>> f(0, 10)
['00 XOR 01 is 1', '00 XOR 10 is 10']
您可以尝试计算不同数量的可能字符串作为 (m+n)!/m!n!其中 m 和 n 是零和 1 然后 find 尝试找到一个模式,比如当字符串相同和不同时找到一个解决方案
鉴于:
两个二进制字符串x和y长度相同;以 arbitrary
的方式重新排列 x 的字符,并以 arbitrary
[=27] 的方式重新排列 y 的字符=](不一定相同)方式。
得到的不同异或的总数是多少?
注:
if x = 1 and y = 11 in binary then take x = 01 So,that length of x and y are equal
示例:
if x = 0 and y = 10
那么可能的结果是 2,即 2 个不同的 XOR 是可能的
"00" XOR "10" is "10"
"00" XOR "01" is "01"
"00" XOR "10" is "10"
"00" XOR "01" is "01"
希望这就是您想要的:
代码:
def f(x, y):
# Convert integers to string
x, y = str(x), str(y)
# Pad inputs
x, y = x.zfill(max(len(x), len(y))), y.zfill(max(len(x), len(y)))
rv = set()
for x_comb in set(''.join(perm_x) for perm_x in itertools.permutations(x)):
for y_comb in set(''.join(perm_y) for perm_y in itertools.permutations(y)):
rv.add(f"{x_comb} XOR {y_comb} is {int(x_comb,2) ^ int(y_comb,2):b}")
return list(rv)
用法示例:
>>> f(0, 10)
['00 XOR 01 is 1', '00 XOR 10 is 10']
您可以尝试计算不同数量的可能字符串作为 (m+n)!/m!n!其中 m 和 n 是零和 1 然后 find 尝试找到一个模式,比如当字符串相同和不同时找到一个解决方案