首页 > 试题广场 >

异或

[编程题]异或
  • 热度指数:17032 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
给定整数m以及n各数字A1,A2,..An,将数列A中所有元素两两异或,共能得到n(n-1)/2个结果,请求出这些结果中大于m的有多少个。

输入描述:
第一行包含两个整数n,m. 
第二行给出n个整数A1,A2,...,An。
数据范围
对于30%的数据,1 <= n, m <= 1000
对于100%的数据,1 <= n, m, Ai <= 10^5


输出描述:
输出仅包括一行,即所求的答案
示例1

输入

3 10  
6 5 10

输出

2

超时
n,m=map(int,input().split())
A=list(map(int,input().split()))
sum=0
k=n-1
i=0
while k:
for j in range(i+1,n):
if A[i]^A[j] > m:
sum+=1
i+=1
k-=1
print(sum)

发表于 2020-08-30 16:59:11 回复(0)
内存超限
n,m=input().split()
n=int(n)
m=int(m)
a=[int(i) for i in input().strip().split()]
c=[]
for i in range(len(a)-1):
    for j in range(i+1,len(a)):
        if a[j]^a[i]>m:
            c.append(a[i]^a[j])
print(len(c))
发表于 2018-08-09 12:39:25 回复(0)
//这题难道不是用数组是最快的么
//然而告诉我不能使用numpy
import numpy as np
if __name__=='__main__':
    x=input().split()
    n=int(x[0]);m=int(x[1])
    arr=sorted(list(map(int,input().split())))
    n_1=np.array(arr)
    a=n*(n-1)/2
    account=0
    yh=[]
    i=0
    for i in range(n-1):
        i=i+1
        b=arr[0]
        arr.append(b)
        arr.remove(arr[0])
        n_2=np.array(arr)
        n_3=n_1^n_2
        for j in n_3:
            if j>m:
                account=account+1
    l=int(account/2)
    print(l)

发表于 2018-08-07 15:59:40 回复(0)
from itertools import combinations
n,m = map(int,input().split())
print(len(set(filter(lambda x:x > m,map(lambda x:x[0]^x[1],combinations(list(map(int,input().split())),2))))))
不知道为什么会报错,在小数据集上测试结果还可以,我看这题还没有Python解法,抛砖引玉啦
发表于 2018-07-08 20:28:27 回复(1)