以M事件中ň处理事件中

由网友(妈妈很潮流﹌)分享简介:问题我一直在面试定。我是接近的解决方案,但并没有解决这个问题很遗憾。Question I've been given at the job interview. I was close to the solution but did not solve it unfortunately.假设我们有一个序列包含的的...

问题我一直在面试定。我是接近的解决方案,但并没有解决这个问题很遗憾。

Question I've been given at the job interview. I was close to the solution but did not solve it unfortunately.

假设我们有一个序列包含的的 N 的类型的数字。而且我们肯定知道这个序列中每个数字确实发生了完全的的 N 的时代不同的是,恰好出现在一个数字的的 M 的倍( 0 的< M 的< N 的)。我们怎样才能找到这个数字与 O(N)的操作和 O(1)的附加内存?

Assume we have a sequence that contains N numbers of type long. And we know for sure that among this sequence each number does occur exactly n times except for the one number that occurs exactly m times (0 < m < n). How do we find that number with O(N) operations and O(1) additional memory?

对于最简单的情况下( N 的 = 2 和的 M 的 = 1 的)所有我们应该做的仅仅是在每一个序列号进行累计 XOR 。其结果将是等于所需的号码。但我试图处理任意的的 M 的和的 N 的

For the simplest case (when n = 2 and m = 1) all that we should do is just to perform accumulative xor on every number in sequence. The result will be equal to the desired number. But i'm stuck while trying to deal with arbitrary m and n.

我会AP preciate一个实际的C ++解决方案。

I would appreciate an actual C++ solution.

编辑:我们知道的实际值的的 M 的和的 N 的的先验

We know actual values of m and n a priori.

示例我们知道的的 N 的 = 3和的 M 的 = 2该序列( N 的 = 8 的)是

Example. We know that n = 3 and m = 2. The sequence (N = 8) is

5 11 5 2 11 5 2 11

和正确的答案是的 2,因为它仅出现两次的这一特殊情况。

And the right answer is 2 in this particular case because it occurs only twice.

推荐答案

您做64总和,每个位,每个款项您计算总和模N,被设置为每一位这样计算回报米,应该在的结果,和0的每个比特不应该被设置

You do 64 sums, one for each bit, for each of the sums you calculate sum mod n, this calculation return m for each bit that should to be set in the result, and 0 for each bit that should not be set.

示例: N = 3,M = 2,表= [5月11日5 2 11 5 2 11]

Example: n = 3, m = 2. list = [5 11 5 2 11 5 2 11]

              5  11   5   2  11   5   2  11
sum of bit 0: 1 + 1 + 1 + 0 + 1 + 1 + 0 + 1 = 6   6 % 3 = 0
sum of bit 1: 0 + 1 + 0 + 1 + 1 + 0 + 1 + 1 = 5   5 % 3 = 2
sum of bit 2: 1 + 0 + 1 + 0 + 0 + 1 + 0 + 0 = 3   3 % 3 = 0
sum of bit 3: 0 + 1 + 0 + 0 + 1 + 0 + 0 + 1 = 3   3 % 3 = 0

因此​​,只有第1位被置位,这意味着,结果是2

So only bit 1 is set, which means the result is 2.

优化执行: (技巧和注意事项,也可用于实际问题)的 值得一提的是,迭代时的阵列,执行速度将在一定延伸由存储器存取限制,如果一个人需要用它通常是最快的时间执行它们都是一个元件的每个元件执行多个操作,从而处理器只需要加​​载的每个元素从内存中一次。 有趣的博客文章。

Optimizing implementation: (Tricks and considerations that are also useful for real problems) It is worth noting that when iterating over an array, execution speed will to some extend be limited by memory access, if one need to perform multiple operations with each element it is usually fastest to perform them all on one element at a time, thus the processor only need to load each element from memory once. Interesting blog post on memory and cache.

有可能总结多个位中一个整数,而不是施加64个不同的位掩码来获得它自己的各一个位可以例如只使用4的每个中提取16位与每个之间的空间3比特位屏蔽,如只要没有溢出出现正常的加法运算将工作完全一样,如果处理16 4位整数,因此这种方法适用于15个数字。后15个数字已经被以这种方式处理的结果必须被添加到能够保持更大的整数存储(可能是8 64位整数各保持8 8位整数,当然他们必须依次被排空到更大的整数等)。照片 其结果是,而不是为每个值做64位掩码,63 bitshifts和64加法人们只要做4位掩码,3 bitshifts和4的添加,以及用于每15个值8位掩码,4 bitshifts和8增加,以及对每255值16位掩码,8 bitshifts和16个附加等。

It is possible to sum multiple bits in a single integer, rather than applying 64 different bitmasks to get each bit on it's own one could for instance use only 4 bitmasks that each extract 16 bits with 3 bits of space between each, as long as no overflow occur a normal addition operation will work exactly as if dealing with 16 4-bit integers, thus this method will work for 15 numbers. After 15 numbers have been processed this way the results must be added to a storage capable of holding bigger integers (could be 8 64-bit integers each holding 8 8-bit integers, they must of course in turn be emptied into bigger integers etc.). The result is that rather than for each value doing 64 bitmasks, 63 bitshifts and 64 additions one need only do 4 bitmasks, 3 bitshifts and 4 additions, plus for every 15 values 8 bitmasks, 4 bitshifts and 8 additions, plus for every 255 values 16 bitmasks, 8 bitshifts and 16 additions etc.

可视化: (总结使用16位整数4×4位整数)的

1000 1000 1000 1000 +
1000 0000 0000 1000 +
0000 0000 0000 1000 +
1000 1000 0000 0000 +
1000 0000 1000 0000 +
0000 0000 1000 1000 =
0010 0100 1100 0010

您是否认为这是4列的4位整数或16位整数1列的结果是一样的,这是唯一真正的,只要长的4位整数不会溢出。

The result is the same whether you consider this to be 4 columns of 4-bit integers or 1 column of 16-bit integers, this is only true as long as long the 4-bit integers do not overflow.

阅读全文

相关推荐

最新文章