Check for difference in i-th bit - bit-manipulation

For positive integers a and b, are the following two expressions logically equivalent?
a ⊕ b = 2i
|a - b| = 2i

Related

Polynomial Long Division in OCaml

I'm trying to implement polynomial long division based on polynomials of type int array. Here the highest degree coefficients are at the end. I'm basing my code off of the pseudo code available on Wikipedia:
function n / d is
require d ≠ 0
q ← 0
r ← n // At each step n = d × q + r
while r ≠ 0 and degree(r) ≥ degree(d) do
t ← lead(r) / lead(d) // Divide the leading terms
q ← q + t
r ← r − t × d
return (q, r)
H
This line
q = poly_add q t
is testing for the equality between q and poly_add q t, it is not an assignment and the compiler is warning you that you are ignoring the result of this test. You are also misunderstanding your pseudo-code: t is supposed to be a polynomial of degree degree r - degree d.
You need to use references, or transform your while loop into a recursive function.
Similarly, since r is an array, whose length cannot be changed:
while ((Array.length r) >= n2) && (Array.length r != 0)do
This test does not change during the loop. Structural inequality is <>.
Another issue is that this line
t.(0) <- r.((Array.length r)-1)/p2.(n2-1)
is mutating your zero polynomial which is a bad idea.
Overall, it is not clear if your polynomial are supposed to be mutable or not.
If they are not supposed to be mutable, you should avoid a.( ) <- altogether.

Is it possible to model a logical OR without boolean variables in linear programming?

I want to state a linear model where I can't use boolean variables due to efficiency reasons. I can only use a solver that can handle boolean variables not that efficiently. And in a productive model I would need hundreds of those variables.
I use a boolean variable to decide if I can satisfy demand either from one source (continuous variable A) or another source (continuous variable B) but not both.
The constraint is:
A + B >= demand
But either A OR B can be non-zero.
This can be ensured by using a boolean variable (Bool_A) and the following constraints:
A <= 1000 * Bool_A
B <= 1000 * (1- Bool_A)
If Bool_A = 1, then the variable A can take non-zero values and B is forced to 0, and if Bool_A = 0 then vice versa.
My question is now: does anyone know, if it is possible to model this using only linear variables (no booleans and integer variables) or has a proof that it is not possible.
In Brown, G. and Dell, R., "Formulating linear and integer linear programs: A rogues’ gallery" the following linear programming formulation for the XOR (exclusive or) can be found :
X = A xor B
resolves to
X ≤ A + B
X ≥ A - B
X ≥ - A + B
X ≤ 2 - A - B
Using an auxiliary variable:
X = A + B - 2*H
H ≤ A
H ≤ B
H ≥ A + B - 1
H ≥ 0

Merging linear chain of vertices in a Graph (in C++)

I have a large graph and it is represented in adjacency list. I would like to compress the graph by merging the linear chain of nodes. For example, if the edges are a-c, b-c, c-d, d-e, e-f, e-g:
a - c - d - e - f
| |
b g
Then c-d, d-e can be merged to a single node x and the new edge list should have a-x, b-x, x-g. I would like to implement it in C++, but I am wondering if there is any C++ graph library which handles this. Also, any suggestion for a efficient algorithm is appreciated.
I think you example might be broken so I am going to solve a slightly different one:
a - c - i - d - e - f
| |
b g
|
h
I think the solution looks like:
a - c - x - e - f
| |
b h
If you agree, then consider counting the number of times each vertex appears in the adjacency list, and storing the first two neighbors for each:
a b c d e f g h i
1 1 3 2 3 1 2 1 2
c c a i d e e g c
b e g h d
The places where it is 2, we can consider collapsing: at d, g, and i:
d g i # candidates
2 2 2
i e c
e h d
Now you can see g has two neighbors not in the candidates, so simply delete g because it is a singleton "chain." This leaves d, whose neighbor i is in the candidates, so collapse d and i into a new vertex x and you're done.
You simply need to remove all nodes with degree 2, merging their two neighbors into a single node.
Repeat the process till no such nodes are left.
The Boost Graph library is usually a good way to store and work with graphs. See here how to merge vertices and contract the edge.

Range Update - Range Query using Fenwick Tree

http://ayazdzulfikar.blogspot.in/2014/12/penggunaan-fenwick-tree-bit.html?showComment=1434865697025#c5391178275473818224
For example being told that the value of the function or f (i) of the index-i is an i ^ k, for k> = 0 and always stay on this matter. Given query like the following:
Add value array [i], for all a <= i <= b as v Determine the total
array [i] f (i), for each a <= i <= b (remember the previous function
values ​​clarification)
To work on this matter, can be formed into Query (x) = m * g (x) - c,
where g (x) is f (1) + f (2) + ... + f (x).
To accomplish this, we
need to know the values ​​of m and c. For that, we need 2 separate
BIT. Observations below for each update in the form of ab v. To
calculate the value of m, virtually identical to the Range Update -
Point Query. We can get the following observations for each value of
i, which may be:
i <a, m = 0
a <= i <= b, m = v
b <i, m = 0
By using the following observation, it is clear that the Range Update - Point Query can be used on any of the BIT. To calculate the value of c, we need to observe the possibility for each value of i, which may be:
i <a, then c = 0
a <= i <= b, then c = v * g (a - 1)
b <i, c = v * (g (b) - g (a - 1))
Again, we need Range Update - Point Query, but in a different BIT.
Oiya, for a little help, I wrote the value of g (x) for k <= 3 yes: p:
k = 0 -> x
k = 1 -> x * (x + 1) / 2
k = 2 -> x * (x + 1) * (2x + 1) / 6
k = 3 -> (x * (x + 1) / 2) ^ 2
Now, example problem SPOJ - Horrible Queries . This problem is
similar issues that have described, with k = 0. Note also that
sometimes there is a matter that is quite extreme, where the function
is not for one type of k, but it could be some that polynomial shape!
Eg LA - Alien Abduction Again . To work on this problem, the solution
is, for each rank we make its BIT counter m respectively. BIT combined
to clear the counters c it was fine.
How can we used this concept if:
Given an array of integers A1,A2,…AN.
Given x,y: Add 1×2 to Ax, add 2×3 to Ax+1, add 3×4 to Ax+2, add 4×5 to
Ax+3, and so on until Ay.
Then return Sum of the range [Ax,Ay].

algorithms for modular inverses

i have read section about The Extended Euclidean Algorithm & Modular Inverses,which states that it not only computes GCD(n,m) but also a and b such that a*n+b*b=1;
algorithm is described by by this way:
Write down n, m, and the two-vectors (1,0) and (0,1)
Divide the larger of the two numbers by the smaller - call this
quotient q
Subtract q times the smaller from the larger (ie reduce the larger
modulo the smaller)
(i have question here if we denote by q n/m,then n-q*m is not equal to 0?because q=n/m;(assume that n>m),so why it is necessary such kind of operation?
then 4 step
4.Subtract q times the vector corresponding to the smaller from the
vector corresponding to the larger
5.Repeat steps 2 through 4 until the result is zero
6.Publish the preceding result as gcd(n,m)
so my question for this problem also is how can i implement this steps in code?please help me,i dont know how start and from which point could i start to solve such problem,for clarify result ,it should look like this
An example of this algorithm is the following computation of 30^(-1)(mod 53);
53 30 (1,0) (0,1)
53-1*30=23 30 (1,0)-1*(0,1)=(1,-1) (0,1)
23 30-1*23=7 (1,-1) (0,1)-1*(1,-1)=(-1,2)
23-3*7=2 7 (1,-1)-3*(-1,2)=(4,-7) (-1,2)
2 7-3*2=1 (4,-7) (-1,2)-3*(4,7)=(-13,23)
2-2*1=0 1 (4,-7)-2*(-13,23)=(30,-53) (-13,23)
From this we see that gcd(30,53)=1 and, rearranging terms, we see that 1=-13*53+23*30,
so we conclude that 30^(-1)=23(mod 53).
The division is supposed to be integer division with truncation. The standard EA for gcd(a, b) with a <= b goes like this:
b = a * q0 + r0
a = r0 * q1 + r1
r0 = r1 * q2 + r2
...
r[N+1] = 0
Now rN is the desired GCD. Then you back-substitute:
r[N-1] = r[N] * q[N+1]
r[N-2] = r[N-1] * q[N] + r[N]
= (r[N] * q[N+1]) * q[N] + r[N]
= r[N] * (q[N+1] * q[N] + 1)
r[N-3] = r[N-2] * q[N-1] + r[N-1]
= ... <substitute> ...
Until you finally reach rN = m * a + n * b. The algorithm you describe keeps track of the backtracking data right away, so it's a bit more efficient.
If rN == gcd(a, b) == 1, then you have indeed found the multiplicative inverse of a modulo b, namely m: (a * m) % b == 1.