Range Update - Range Query using Fenwick Tree - 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].

Related

if condition in ampl

I am new in ampl and I want to use if condition in ampl with the following information:
I have a binary variable X[p,r], where {p in P, r in R}.
Now I want to make a new constraint such that the variable R[p,r] is used where X[p,r]=0.
I do not know how I can write it or even if the ampl can handle it or not, I tried the following constraint but they did not work:
s.t. a1{r in R, p in P and X[p,r]=0}:
s.t. a2{r in R p in P and X[p,r]=0};
s.t. a2{r in R ,p in P, and X[p,r]=0};
s.t. a2{r in R, p in P: and X[p,r]=0};
You cannot include a decision variable in the "for all" part of the constraint (in AMPL, the part inside the {...}). Instead, you need build into the constraint itself the logic that says the constraint is only active if X[p,r] = 0. The way to do that depends on the type of constraint: >=, =, or <=. I'll write each case separately, and I'll do it in a generic way instead of specific to your problem.
In the explanation below, I assume that the constraint is written as
a[1]y[1] + ... + a[n]y[n] >=/=/<= b,
where a[i] and b are constants and y[i] are decision variables. I also assume we want the constraint to hold if x = 0, where x is a binary decision variable, and we don't care whether the constraint holds if x = 1.
Let M be a new parameter (constant) that equals a large number.
Greater-than-or-equal-to constraints:
The constraint is a[1]y[1] + ... + a[n]y[n] >= b. Rewrite it as
a[1]y[1] + ... + a[n]y[n] >= b - Mx.
Then, if x = 0, the constraint holds, and if x = 1, it has no effect since the right-hand side is very negative.
(If all of the a[i] are nonnegative, you can instead use
a[1]y[1] + ... + a[n]y[n] >= bx,
which is tighter.)
Less-than-or-equal-to constraints:
The constraint is a[1]y[1] + ... + a[n]y[n] <= b. Rewrite it as
a[1]y[1] + ... + a[n]y[n] <= b + Mx.
Then, if x = 0, the constraint holds, and if x = 1, it has no effect since the RHS is very large.
Equality constraints:
The constraint is a[1]y[1] + ... + a[n]y[n] = b. Rewrite it as
a[1]y[1] + ... + a[n]y[n] <= b + Mx
a[1]y[1] + ... + a[n]y[n] >= b - Mx.
Then, if x = 0, the equality constraint holds, and if x = 1, the constraints have no effect.
Note: If your model is relatively large, i.e., it takes a non-negligible amount of time to solve, then you need to be careful with big-M-type formulations. In particular, you want M to be as small as possible while still enforcing the logic of the constraints above.

Finding element at x,y in a given matrix after rotation in C/C++?

How can I find the element at index x,y in a given matrix after rotating the total matrix without performing the matrix rotation.
That means I am just interested in that coordinate don't want to perform total operation on total matrix and than simply get the element at any index.
Example:
suppose a matrix is given
1 2 3
4 5 6
7 8 9
and i want to find the element at 1,1 after rotating the matrix by 90 degree.
answer should be "7".
**NOTE**: Without performing the rotation on total matrix.
and if i want the element at 1,2 than the answer should be "4".
I hope I clearly communicated the question please help if you know the solution or algorithm for this question.
Thank you.
Suppose you have a m x n matrix and you are interested in the position of M[i][j] after rotation.
So, after a rotation of 90 degrees clockwise, M[i][j] -> M[j][m+1-i].
As in your example, M[3][1] will be M[1][3+1-3] after rotation.
Hope this solves your problem.
Here's one way to solve the problem (other than using somebody else's solution).
It's fairly clear that the column index of each element is the row index of that element after rotation (at least, I hope that's clear).
So, the problem is the column index of an element after rotation.
The first row will become the last column, the second will be the second last, and so on until the last row which becomes the first column.
One way of viewing this is that we have the sequence (of rows) i = 1, 2, ..., m and want to map that to the sequence (of columns) j = m, m - 1, m - 2, ..., 2, 1.
But m = m + 1 - 1, m - 1 = m + 1 - 2, m - 2 = m + 1 - 3, ..., 1 = m + 1 - m.
So the desired sequence is j = m + 1 - i.
In other words, M[i][j] -> M[j][m + 1 - i].
You want to map:
(x,y) -> (x', y')
Assume following:1
x' = ax + by + c
y' = dx + ey + f
Now, (1, 1) maps to (W, 1)2
w = a + b + c
1 = d + e + f
(1, W) maps to (1, 1)3
1 = a + bw + c
1 = d + ew + f
and (W, H) maps to (1, H)4
1 = aw + bh + c
h = dw = eH + f
Solve 2, 3 and 4 equation and fill in to 1 get the value. (Hint: b = -1, e = 0)
// For 90 degree rotation using correct indexing for x and y (starting at 0 not 1)
// Assuming square matrix
template<class T, int size>
T elemAfter90degRot(int x, int y, T[size][size] mat) {
int j = y;
int i = size - 1 - x;
return mat[i][j];
}
I think that should do the trick for a 90 degree rotation of a square matrix

Use '->' in prolog

I want to generate a list that is made up by sublists which contains 2 intergers and their sum and product.
The expected result is
A = [[2,3,5,6],[2,4,6,8],[2,5,7,10],[2,6,8,12],
[2,7,9,14],[2,8,10,16],[3,4,7,12],[3,5,8,15],
[3,6,9,18],[3,7,10,21],[4,5,9,20]].
But I kept have some sublists that does not have the sum and product:
?- get(A).
A = [[2,3,5,6],[2,4,6,8],[2,5,7,10],[2,6,8,12],
[2,7,9,14],[2,8,10,16],[2,_G419,_G422,_G425],
[3,4,7,12],[3,5,8,15],[3,6,9,18],[3,7,10,21],
[3,_G530,_G533,_G536],[4,5,9,20]].
Here is my code :
get(4,5,[]):-!.
get(N,M,[[N,Q,S,P]|List]):-
Q is M + 1,
S is N + Q,
S =< 10 ->
P is N * Q,
get(N,Q,List);
X is N + 1,
get(X,X,List).
get(List):-get(2,2,List).
I think the problem is about using -> in my code, but I don't know how to fix it, I think the logic about -> is right: if S=<10, then calculate the product and the other value with N and Q; else calculate N+1 and N+2 and ...
The operator precedence of (->)/2 is higher than the
operator precedence of (,)/2. As a result the (,)/2
are grouped together as an argument for (->)/2.
So your rule:
get(N,M,[[N,Q,S,P]|List]):-
Q is M + 1,
S is N + Q,
S =< 10 ->
P is N * Q,
get(N,Q,List);
X is N + 1,
get(X,X,List).
Is basically read by the interpreter as:
get(N,M,[[N,Q,S,P]|List]):-
(Q is M + 1,
S is N + Q,
S =< 10) ->
P is N * Q,
get(N,Q,List);
X is N + 1,
get(X,X,List).
With the result that Q ans S are unbound when
the if condition is not satisfied and the else
part is executed. You can fix your code by introducing
additional parenthesis:
get(N,M,[[N,Q,S,P]|List]):-
Q is M + 1,
S is N + Q,
(S =< 10 ->
P is N * Q,
get(N,Q,List)
; X is N + 1,
get(X,X,List)).
But even with this fix, there is an issue with P
being unbound in the else branch.
For clarity, you might even try a solution without
if-then-else, see for example here
Prolog removing IF THEN ELSE
I concur with Jan, but I want to inteject a point about formatting. Because ; and -> so strongly affect the meaning of a program, it's poor form (meaning makes it harder to understand) to put those operators at the end of a line.
get(N,M,[[N,Q,S,P]|List]):-
Q is M + 1,
S is N + Q,
S =< 10
->
P is N * Q,
get(N,Q,List)
; X is N + 1,
get(X,X,List).
When you arrange code like this it becomes immediately apparent that the entire clause is an if-then-else, with the first 3 lines being the 'if' portion. (Though I wouldn't object to Jan's layout for the final version.)

Is there a way to set a decision variable to true iff a variable is in a range in an integer linear program?

I have an integer-valued bounded variable, call it X. (Somewhere around 0<=X<=100)
I want to have a binary variable, call it Y, such that Y=1 if X >= A and X <= B, otherwise Y=0.
The best I've come up with thus far is the following (where T<x> are introduced binary variables, and M is a large number)
(minimize Y)
(X - A) <= M*Ta
(B - X) <= M*Tb
Y <= Ta
Y <= Tb
Y >= Ta + Tb - 1
(In other words, introducing two binary variables that are true if the variable satisfies the lower and upper bounds of the range, respectively, and setting the result to the binary multiplication of those variables)
This... Works, sort of, but has a couple major flaws. In particular, it's not rigorously defined - Y can be 1 even if X is outside the range.
So: is there a better way to do this? In particular: is there a way to rigorously define it, or if not, a way to at least prevent false positives?
Edit: to clarify: A and B are variables, not parameters.
I think the below works.
(I) A * Y <= X <= B * Y + 100 * (1 - Y)
(II) (X - A) <= M * Ta
(III) (B - X) <= M * Tb
(IV) Y >= Ta + Tb - 1
So X < A makes:
(I)Y=0
and (II), (III), (IV) do not matter.
X > B makes:
(I) Y = 0
and (II), (III), (IV) do not matter.
A <= X <= B makes:
(I) Y = 1 or Y = 0
(II) Ta = 1
(III) Tb = 1
(IV) Y = 1
Rewriting loannis's answer in a linear form by expanding out multiplication of binary variables with a continuous variable:
Tc <= M*Y
Tc <= A
Tc >= A - M*(1-Y)
Tc >= 0
Tc <= X
Td <= M*Y
Td <= B
Td >= B - M*(1-Y)
Td >= 0
X <= Td + 100*(1-Y)
(X - A + 1) <= M * Ta
(B - X + 1) <= M * Tb
Y >= Ta + Tb - 1
This seems to work, although I have not yet had the chance to expand it out to prove it. Also, some of these constraints may be unnecessary; I have not checked.
The expansion I did was according to the following rule:
If b is a binary variable, and c is a continuous one, and 0 <= c <= M, then y=b*c is equivalent to the following:
y <= M*b
y <= c
y >= c - M*(1 - b)
y >= 0

Properties of the modulo operation

I have the compute the sum S = (a*x + b*y + c) % N. Yes it looks like a quadratic equation but it is not because the x and y have some properties and have to be calculated using some recurrence relations. Because the sum exceeds even the limits of unsigned long long I want to know how could I compute that sum using the properties of the modulo operation, properties that allow the writing of the sum something like that(I say something because I do not remember exactly how are those properties): (a*x)%N + (b*y)%N + c%N, thus avoiding exceeding the limits of unsigned long long.
Thanks in advance for your concern! :)
a % N = x means that for some integers 0 <= x < N and m: m * N + x = a.
You can simply deduce then that if a % N = x and b % N = y then
(a + b) % N =
= (m * N + x + l * N + y) % N =
= ((m + l) * N + x + y) % N =
= (x + y) % N =
= (a % N + b % N) % N.
We know that 0 < x + y < 2N, that is why you need to keep remainder calculation. This shows that it is okay to split the summation and calculate the remainders separately and then add them, but don't forget to get the remainder for the sum.
For multiplication:
(a * b) % N =
= ((m * N + x) * (l * N + y)) % N =
= ((m * l + x * l + m * y) * N + x * y) % N =
= (x * y) % N =
= ((a % N) * (b % N)) % N.
Thus you can also do the same with products.
These properties can be simply derived in a more general setting using some abstract algebra (the remainders form a factor ring Z/nZ).
You can take the idea even further, if needed:
S = ( (a%N)*(x%N)+(b%N)*(y%N)+c%N )%N
You can apply the modulus to each term of the sum as you've suggested; but even so after summing them you must apply the modulus again to get your final result.
How about this:
int x = (7 + 7 + 7) % 10;
int y = (7 % 10 + 7 % 10 + 7 % 10) % 10;
You remember right. The equation you gave, where you %N every of the summands is correct. And that would be exactly what I use. You should also %N for every partial sum (and the total) again, as the addition results can be still greater than N. BUT be careful this works only if your size limit is at least twice as big as your N. If this is not the case, it can get really nasty.
Btw for the following %N operations of the partial sums, you dont have to perform a complete division, a check > N and if bigger just subtraction of N is enough.
Not only can you reduce all variable mod n before starting the calculation, you can write your own mod-mul to compute a*x mod n by using a shift-and-add method and reduce the result mod n at each step. That way your intermediate calculations will only require one more bit than n. Once these products are computed, you can add them pairwise and reduce mod n after each addition which will also not require more than 1 bit beyond the range of n.
There is a python implementation of modular multiplication in my answer to this question. Conversion to C should be trivial.