integer programming: need help to formulate a constrain - mixed-integer-programming

I am trying to formulate a constrain for my math model. the constrain goal is:
if A = 1 and B = 1 then C <= D
otherwise (A or B or both are 0) there is no constraint.
A and B are binary variables. C and D are integer numbers.
so far I was able to come up with this equation:
M(A - 1) - (B - 1) + C <= D (M is a big big number)
this formulation does not hold when A = 1 and B = 0

You could do this in two steps, first introduce a variable X representing logical and of A and B.
X >= A + B - 1
X <= A
X <= B
Then use X to express the inequality:
C - M(1-X) <= D

Related

Maths Two Percentage Increases - Any Takers

Maths Puzzle
A = 10 (COST)
B = 20 (Sell Price)
C = 15% (Fee)
D = Tax at 20% or 1/6th if you are taking it away
E = Margin
B x C = 3.60
B / 6 in Reverse = 3.33
E = B - A - (B x C) - (B / 6) = 3.07
Ok The above works correct when i i provide B Sell Price
I want a formula that will Give me E, If i say E = 3.07 It says B = 20
How would i do that, the math
Any boffins can help
Thanks
First:
You did not use D: I suppose instead of B / 6, it should read B x D, where D can be either 20% or 16,666...%
There is a mistake: B x C is not 3.60 for the given values (B=20, C=15%), it is 3.
This is a matter of mathematical deduction:
Given the equation:
E = B - A - (B x C) - (B x D)
Add A to both sides:
E + A = B - (B x C) - (B x D)
Isolate B:
E + A = B x (1 - C - D)
Divide both sides by (1 - C - D). Condition: C + D cannot equal 100%
(E + A) / (1 - C - D) = B
So there is your formula for calculating B. Take note of the condition: this only holds true when C + D is not equal to 100%.

last digit of a^b^c

I've got stuck on this problem :
Given a, b and c three
natural numbers (such that 1<= a, b, c <= 10^9), you are supposed to find the last digit of the number a^b^c."
What I've firstly thought was the O(log n) algorithm for raising a at power n.
int acc=1; //accumulator
while(n>0) {
if(n%2==1)
acc*=a;
a=a*a;
n/=2;
}
Obviously, some basic math might help, like the "last digit" stuff :
Last_digit(2^n) = Last_digit(2^(n%4))
Where n%4 is the remainder of the division n/4
In a nutshell, I've tried to combine these, but I couldn't get on the good way.
Some help would really be apreciated.
The problem is that b^c may be very large. So you want to reduce it before using the standard modular exponentiation.
You can remark that a^(b^c) MOD 10 can have a maximum of 10 different values.
Because of the pigeonhole principle, there will be a number p such that for some r:
a^r MOD 10 = a^(p+r) MOD 10
p <= 10
r <= 10
This implies that for any q:
a^r MOD 10 = a^r*a^p MOD 10
= (a^r*a^p)*a^p MOD 10
= ...
= a^(r+q*p) MOD 10
For any n = s+r+q*p, with s < p you have:
a^n MOD 10 = a^s*a^(r+q*p) MOD 10
= a^s*a^r MOD 10
= a^((n-r) MOD p)*a^r MOD 10
You can just replace n= (b^c) in the previous equation.
You will only compute (b^c-r) MOD p where p <= 10 which is easily done and then compute a^((b^c-r) MOD p)*a^r MOD 10.
Like I mentioned in my comments, this really doesn't have much to do with smart algorithms. The problem can be reduced completely using some elementary number theory. This will yield an O(1) algorithm.
The Chinese remainder theorem says that if we know some number x modulo 2 and modulo 5, we know it modulo 10. So finding a^b^c modulo 10 can be reduced to finding a^b^c modulo 2 and a^b^c modulo 5. Fermat's little theorem says that for any prime p, if p does not divide a, then a^(p-1) = 1 (mod p), so a^n = a^(n mod (p-1)) (mod p). If p does divide a, then obviously a^n = 0 (mod p) for any n > 0. Note that x^n = x (mod 2) for any n>0, so a^b^c = a (mod 2).
What remains is to find a^b^c mod 5, which reduces to finding b^c mod 4. Unfortunately, we can use neither the Chinese remainder theorem, nor Fermat's little theorem here. However, mod 4 there are only 4 possibilities for b, so we can check them separately. If we start with b = 0 (mod 4) or b = 1 (mod 4), then of course b^c = b (mod 4). If we have b = 2 (mod 4) then it is easily seen that b^c = 2 (mod 4) if c = 1, and b^c = 0 (mod 4) if c > 1. If b = 3 (mod 4) then b^c = 3 if c is even, and b^c = 1 if c is odd. This gives us b^c (mod 4) for any b and c, which then gives us a^b^c (mod 5), all in constant time.
Finally with a^b^c = a (mod 2) we can use the Chinese remainder theorem to find a^b^c (mod 10). This requires a mapping between (x (mod 2), y (mod 5)) and z (mod 10). The Chinese remainder theorem only tells us that this mapping is bijective, it doesn't tell us how to find it. However, there are only 10 options, so this is easily done on a piece of paper or using a little program. Once we find this mapping we simply store it in an array, and we can do the entire calculation in O(1).
By the way, this would be the implementation of my algorithm in python:
# this table only needs to be calculated once
# can also be hard-coded
mod2mod5_to_mod10 = [[0 for i in range(5)] for j in range(2)]
for i in range(10):
mod2mod5_to_mod10[i % 2][i % 5] = i
[a,b,c] = [int(input()) for i in range(3)]
if a % 5 == 0:
abcmod5 = 0
else:
bmod4 = b % 4
if bmod4 == 0 or bmod4 == 1:
bcmod4 = bmod4
elif bmod4 == 2:
if c == 1:
bcmod4 = 2
else:
bcmod4 = 0
else:
if c % 2 == 0:
bcmod4 = 1
else:
bcmod4 = 3
abcmod5 = ((a % 5)**bcmod4) % 5
abcmod2 = a % 2
abcmod10 = mod2mod5_to_mod10[abcmod2][abcmod5]
print(abcmod10)

Calculating the level of insertion based on the size of the tree

If I have a graph structure that looks like the following
a level-1
b c level-2
c d e level-3
e f g h level-4
...... level-n
a points to b and c
b points to c and d
c points to d and e
and so on
how can i calculate the n from the size(number of existing nodes) of the graph/tree?
The number of nodes present if the height is h is given by
1 + 2 + 3 + ... + h = h(h + 1) / 2
This means that one simple option would be to take the total number of nodes n and do a simple binary search to find the right value of h that such that h(h + 1) / 2 = n.
Alternatively, since n = h(h + 1) / 2, you can note that
n = h(h + 1) / 2
2n = h2 + h
0 = h2 + h - 2n
Now you have a quadratic equation (in h) that you can solve to directly get back the value of h. The solution is
h = (-1 ± √(1 + 8n)) / 2
If you take the minus branch, you'll get back a negative number, so you should take the positive branch and compute
(-1 + √(1 + 8n)) / 2
to directly get back h.
Hope this helps!

Can an expression with multiple modulo operators (a % b % c) be reduced to something else?

Note: I'm assuming all operators below are left-associative.
a - b - c is equal to a - (b + c).
a / b / c is equal to a / (b * c).
Are there any similar equivalences for the modulo operator?
I've figured that a % b % c is equal to a % b if b <= c and a % c if b > c && b % c == 0. However, I can't figure out what a % b % c equals when b > c && b % c != 0. Is there a more general or elegant way to think about chained modulo operators? Additionally, can you think of any algorithms or usages of chained modulo operators of the last type?
Let's take b = q0*c + r0
You decompose
a = q1 * b + r1
r1 = q2 * c + r2
a = q1 * (q0 * c + r0) + q2 * c + r2
a = (q1 * q0 + q2) * c + q1 * r0 + r2
So you can say that
r2 = (a - q1 * r0) % c
Or in other words
(a%b%c) = (a - (a/b)*(b%c)) % c
This also works when b<c, because (a - (a/b)*b) is just a%b
But I doubt that this is very usefull
Chained modulo operators have no particular meaning in general, and the result may actually be ill-defined.
If you think of modulo p as a projection from integral numbers to some set {k, ... k + p - 1} then chaining two projections may not be clearly defined mathematically since the chaining depends on the choice of k (more precisely, chaining % p and % q depends on k % q).
To take this to the programming world, note that the C standard mandates that the sign of a % b is implementation defined when a is not positive.
As an example: -1 % 3 may be -1 or 2 depending on the implementation. Then, (-1 % 3) % 2 may be 0, 1, or -1 one depending on the implementation...

Average of 3 numbers using integers

I would like to compute the average of three numbers, like:
d = int(round((a + b + c) / 3.0))
Where a, b, c, d are integers.
Is it possible to get the same result using just integers?
I'm interested in this because of performance reasons, I assume doing the math using integers should be faster than using floats.
The example above converts the integers to floats, calculates the result, rounds it and converts back to integer. Is it possible to avoid the int <-> float conversions?
Given the requirements for 1, 1, 2 -> 1; 1, 2, 2 -> 2 then this can be done using integer division.
Using // for the integer division and n for number of elements.
average = ( a+ b + c + .... + n//2 ) // n
ie sum up all the values and then add a number to deal with rounding.
As noted in #Henrik's answer this assumes that all numbers are positive.
(a + b + c + 1) / 3
Explanation: if (a + b + c) % 3 == 1, it's rounded down; if (a + b + c) % 3 == 2, it's rounded up.
At least this should work for a + b + c >= 0. You may need to treat negative values separately.
A variant of Mahmoud's answer using only one division:
d = (((a+b+c) * 10) + 15) / 30
It should be as simple as that:
d = (((a+b+c) * 10) / 3 + 5) / 10;

Resources