算法计算和 [英] algorithm to calculate AND

查看:114
本文介绍了算法计算和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计算和数字从0到(N)^ {1/2} - 1 各从0号到(N)^ {1/2} - 1 。我想这样做的 O(N)时,不能使用XOR,OR和操作。

I want to calculate AND of numbers from 0 to (n)^{1/2} - 1 with each of the numbers from 0 to (n)^{1/2} - 1. I want to do this in O(n) time and can't use the XOR, OR, AND operations.

具体,我可以计算 X + 1和Y 如果我知道 X和Y

Specifically, can I calculate X+1 AND Y if I know X AND Y?

P.S。 - 内存模型正在这里假设和运算(加,乘,除)在<的log(n)位数字可以做的是恒定的时间。

P.S. - RAM model is being assumed here and operations (add, multiply, divide) on < log(n) bit numbers can be done is constant time.

推荐答案

是的。

开始用[1×1]格:

H(-1) = [ 0 ]

然后递归的应用:

Then apply the recursion:

H(i) = [ H(i-1)  H(i-1)
         H(i-1)  H(i-1)+(1 << i) ]

在这里,它表示矩阵级联。即每个递归加倍网格中的每个维度的大小。重复进行,直到达到所需的大小。

where that denotes matrix concatenation. i.e. each recursion doubles the size of the grid in each dimension. Repeat until you achieve the required size.

这篇关于算法计算和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆