The following equation (which actually evaluates to zero) is not being simplified to zero by Mathematica. I have tried the commands known to me, like Simplify, FullSimplify, Cancel, etc. The equation is given below in InputForm. The image of the equation from Mathematica is also given below.
\[Rho]^3 (Subscript[r, to]/\[Rho])^((
6 Subscript[\[Gamma], 1])/(\[Beta] +
2 Subscript[\[Gamma], 1])) (\[Beta] +
2 Subscript[\[Gamma],
1])^3 - (\[Rho] (\[Beta] + 2 Subscript[\[Gamma], 1]))^((
3 \[Beta])/(\[Beta] +
2 Subscript[\[Gamma], 1])) (Subscript[r,
to] (\[Beta] + 2 Subscript[\[Gamma], 1]))^((
6 Subscript[\[Gamma], 1])/(\[Beta] + 2 Subscript[\[Gamma], 1]))
How do I get Mathematica to evaluate this to zero? Original Expression Image
I thought the problem could be with addition and cancellation of exponents. So I tried this simpler expression given below:
(\[Beta] + 2 Subscript[\[Gamma], 1])^((
3 \[Beta])/(\[Beta] +
2 Subscript[\[Gamma], 1])) (\[Beta] + 2 Subscript[\[Gamma], 1])^((
6 Subscript[\[Gamma], 1])/(\[Beta] + 2 Subscript[\[Gamma], 1]))
This worked fine, as can be see in the image. Image with Simpler expression
How can we make Mathematica cancel and evaluate the original expression to zero? Thanks in advance.
Apparently needs some assumptions
expr = ρ^3 (Subscript[r, to]/ρ)^((
6 Subscript[γ, 1])/(β +
2 Subscript[γ, 1])) (β +
2 Subscript[γ, 1])^3 - (ρ (β + 2 Subscript[γ, 1]))^((3 β)/(β +
2 Subscript[γ, 1])) (Subscript[r, to] (β + 2 Subscript[γ, 1]))^((
6 Subscript[γ, 1])/(β + 2 Subscript[γ, 1]))
FullSimplify[expr, Assumptions ->
β > 0 && Subscript[γ, 1] > 0 &&
ρ > 0 && Subscript[r, to] > 0]
0
FullSimplify[expr, Assumptions -> ρ > 0 && Subscript[r, to] > 0]
0
Related
Given a set of N numbers in an array. Given Q queries. Each Query contains 1 number x.
For each query, you need to add x to each element of the array and then report the sum of absolute values in the array.
Note : Changes to the array are permanent. See Sample for more clarification.
Input Format
First line contains N , number of elements in the array.
Next line contains N space separated integers of the array.
Next line contains Q(number of queries).
Next line contains Q space separated integers(the number x).
Output Format
For each query , output the sum in a newline.
Constraints
1 ≤ N ≤ 500000
1 ≤ Q ≤ 500000
-2000 ≤ number in each Query ≤ 2000
-2000 ≤ value of the array element ≤ 2000
Sample Input
3
-1 2 -3
3
1 -2 3
Sample Output
5
7
6
Explanation
After Query 1 : [ 0 , 3 , -2 ] => sum = 0 + 3 + 2 = 5
After Query 2 : [ -2 , 1 , -4 ] => sum = 2 + 1 + 4 = 7
After Query 3 : [ 1 , 4 , -1 ] => sum = 1 + 4 + 1 = 6
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,*a,q,*aq;
long int sum=0;
scanf("%d",&n);
a=(int*)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&q);
aq=(int*)malloc(sizeof(int)*q);
for(int i=0;i<n;i++)
scanf("%d",&aq[i]);
for(int i=0;i<q;i++)
{
for(int j=0;j<n;j++)
{
sum+=abs(aq[i]+a[j]);
a[j]=aq[i]+a[j];
}
printf("%ld\n",sum);
sum=0;
}
}
Some test cases are timing out.
Your solution is performing N.Q operations, which is huge.
First notice that the range of the data is moderate, so that you can represent the N numbers using an histogram of 4001 entries. This histogram is computed in N operations (plus initializing the bins).
Then the requested sum is obtained as the sum of the absolute differences with every bin, weighted by the bin values. This lowers the workload from N.Q to B.Q (B is the number of bins).
If I am right, we can do much better by decomposing the sum in a subsum for the negative values and another in the positives. And these sums are obtained by computing prefix sums. This should lead to a solution in Q operations, after preprocessing the histogram in B operations.
Here's an outline of an algorithm:
Sample Input
3
-1 2 -3
Sort the data and compute prefix sums:
-3, -1, 2
-3, -4, -2 (prefix sums)
(Using a histogram as Yves Daoust suggested would eliminate the initial sort and any binary search to find the three sections below, which would significantly optimise complexity.)
Maintain a running delta:
delta = 0
For each query of
1 -2 3
Query 1:
* update delta:
delta = 0 + 1 = 1
* identify three sections:
[negative unaffected] [switches sign] [positive unaffected]
-3, -1, 2
* Add for each section abs(num_elements * delta + prefix_sum):
abs(2 * 1 + (-4 - 0)) + abs(1 * 1 + (-2 -(-4)))
= abs(2 - 4) + abs(1 + 2)
= 5
Query -2:
* update delta:
delta = 1 - 2 = -1
* identify three sections:
[negative unaffected] [switches sign] [positive unaffected]
-3, -1, 2
* Add for each section abs(num_elements * delta + prefix_sum):
abs(2 * (-1) + (-4 - 0)) + abs(1 * (-1) + (-2 -(-4)))
= abs(-2 - 4) + abs(-1 + 2)
= 7
Query 3:
* update delta:
delta = -1 + 3 = 2
* identify three sections:
[negative unaffected] [switches sign] [positive unaffected]
-3, -1, 2
* Add for each section abs(num_elements * delta + prefix_sum):
abs(1 * 2 + (-3 - 0)) + abs(1 * 2 + (-4 - (-3))) + abs(1 * 2 + (-2 -(-4)))
= abs(2 - 3) + abs(2 - 1) + abs(2 + 2)
= 6
Sample Output
5
7
6
I'm wondering if there is a trick with number theory to compute this remainder without need to implement a BigInt division algorithm.
Haha, it's easy!
I can iterate over all digits, adding each parcel...
Using the properties:
1) (a+b) mod c = (a mod c + b mod c) mod c
2) (a*b) mod c = (a mod c * b mod c) mod c
The power of ten can be increased mod 1500 each step.
Its simple, just check these three things:
Divisibility by 1500
it has to be divisible by 100 (last two digits must be 00)
it has to be divisible by 5 (third digit from right has to be 0 or 5)
it has to be divisible by 3 (iterate over all digits, sum them, and the result has to be divisible by 3)
And if you want to know the remainder, its again simple:
Check for divisible by 5 and get remainder
get remainder from last 4 digits after division by 500, it will be from 0 to 499.
Check for divisible by 3 and get remainder
iterate over all digits, sum them, and get remainder from that after division by 3, it will be from 0 to 2.
and depending from this remainder increase the remainder from 1st step by this remainder multiplied by 500.
Example 1
1234567890 % 1500 = 390
7890 % 500 = 390
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 = 45 and 45 % 3 = 0, so nothing has to be added to 390 and the result is then 390.
Example 2
12345678901 % 1500 = 901
8901 % 500 = 401
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 0 + 1 = 46 and 46 % 3 = 1, so we have to add 1 * 500 to the result from 1st step, so 401 + 1 * 500 = 901.
Example 3
1357913579 % 1500 = 1079
3579 % 500 = 79
1 + 3 + 5 + 7 + 9 + 1 + 3 + 5 + 7 + 9 = 50 and 50 % 3 = 2, so we have to add 2 * 500 to the result from 1st step, so 79 + 2 * 500 = 1079.
Hope this helps you.
In chess, one player can have different material combinations, for example:
"1 queen, 2 rooks, 2 knights, 2 bishops, 8 pawns + the king" is one combination
if the player loses one bishop:
"1 queen, 2 rooks, 2 knights, 1 bishop, 8 pawns + the king" is another combination
..afterwards, if a pawn is promoted to a knight, then:
"1 queen, 2 rooks, 3 knights, 1 bishop, 7 pawns + the king" is another combination
OK, the following combination is not valid:
"5 queens, 5 rooks, 5 knights, 5 bishops, 2 pawns + the king"
since you lack of pawns to promote. (5 queens = 4 pawns needed) (5 rooks = 3 pawns needed) , etc. so 4 + 3 + 3 + 3 = 13 pawns needed. Since 2 pawns on the board, then at most 6 pawns could be promoted. Not valid.
How many valid material combinations are there?
I computed 8694 combinations using the following C code. The question is:
Do you find simpler/efficient algorithm to calculate it? (less cycles, less calculations, clearer code, etc.) ... or even a math formulae??
total = 0;
for (queens=0;queens<=9;queens++)
for (rooks=0;rooks<=10;rooks++)
for (bishops=0;bishops<=10;bishops++)
for (knights=0;knights<=10;knights++)
for (pawns=0;pawns<=8;pawns++)
{
pawnsRequested = 0;
if (queens>1) pawnsRequested += queens - 1;
if (rooks>2) pawnsRequested += rooks - 2;
if (bishops>2) pawnsRequested += bishops - 2;
if (knights>2) pawnsRequested += knights - 2;
if (8-pawns < pawnsRequested) continue;
total++;
}
printf("%i\n",total);
If the piece types were independent, then we could just multiply: 10 possibilities for the queens times 11 possibilities for the rooks times etc. We need to track pawn usage, however. There's a mathematical trick called generating functions where we can encode the possibilities for, e.g., rooks as
3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8,
where the power of x denotes the number of pawns used, and the coefficient denotes the number of possibilities. Here, there are three possibilities that require no promoted pawns (0, 1, 2), one that requires one promoted pawn (3), one that requires two promoted pawns (4), etc. Now we can multiply each of the factors together (respectively, queens, rooks, bishops, knights, pawns).
(2 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
* (3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
* (3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
* (3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
* (1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
Here it is from Wolfram Alpha.
The coefficients of 1 through x^8, which are the number of possibilities for 0 to 8 pawns required, are 54, 135, 261, 443, 693, 1024, 1450, 1986, 2648, summing to 8694.
if we have n different things and we need to distribute them among m different people then how many ways can we do it such that for each of the m persons there is conditions that:
person 1 can have at least a things and at most b things
person 2 can have at least c things and at most d things
.. and so on ?
e.g if n = 5 and m =3 and the conditions are:
person 1 can receive at least 0 and at most 1 gift
person 2 can receive at least 1 and at most 3 gift
person 3 can receive at least 1 and at most 4 gift
then the number of ways of distributing these 5 gifts is 6((0 1 4), (0 2 3), (0 3 2), (1 1 3), (1 2 2), (1 3 1)).
One way i believe is to iterate through all possible combinations for each range and see which ones sum upto n , but can't think of an efficient algorithm.
Thanks
You probably want to use a generating function approach. Represent the number of objects that person i gets by the exponents of x. This means that if person i can have at least 3 and at most 7 things, this corresponds to the term
x^3 + x^4 + x^5 + x^6 + x^7
Remember to think of + as OR and * as AND. If we want to impose conditions and person 1 and person 2, then multiply their functions together. For example, with person 1 having between 3 and 7 things, and say person 2 has at least 5 things, and add a third person with at most 10 things. Then we get:
(x^3 + x^4 + x^5 + x^6 + x^7) * (x^5 + x^6 + ... ) * (1 + x + x^2 + ... + x^10)
which can also be written as
(x^3 + x^4 + x^5 + x^6 + x^7) * ( x^5/(1+x) ) * (1 + x + x^2 + ... + x^10)
The way to get information back from this is the following. The coefficient of x^M in the expansion of these terms gives the number of ways to distribute a total of M things among all the people subject to the given constraints.
You can work this out from the formulas, or write a program to extract the coefficient, but the idea is to use generating functions as a convenient and efficient way to encode the constraints along with the answer.
I am testing an infix-to-postfix-to-infix converter and found some kind of uncertainty. For example, a simple infix sum
1 + 2 + 3 + 4
can be converted to postfix one
1 2 + 3 + 4 +
assuming that operators with equal precedence are not accumulated. If they are then I get
1 2 3 4 + + +
On the other hand, all the following postfix expressions can be converted to the initial sum
1 2 + 3 + 4 +
1 2 + 3 4 + +
1 2 3 4 + + +
Are all these postfix expressions correct?
UPDATE1
If you would make such converter, to which form would you choose? I need to choose one for testing.
You need to define an extra constraint.
Mathematically, your postfix expressions are all the same. But on a computer integer addition is not really commutative because of overflow.
Replace 1 2 3 4 with a b c d and consider the possibility of overflow. Most programming languages define that a + b + c + d must be evaluated left-to-right so that a b + c + d + is the only correct translation.
Only when you define that the order of evaluation is 'unspecified' all the postfix versions are equivalent. That was the case for (older) C Compilers.
Yep, all correct. They correspond to the following bracketed infix expressions:
((1 + 2) + 3) + 4
(1 + 2) + (3 + 4)
1 + (2 + (3 + 4))
+ is confusing - it is commutative, so in fact, every result seems correct.
Consider replacing + with other operators: 1 a 2 b 3 c 4.
The correct result here, for left-associative operators, is
1 2 a 3 b 4 c
So, in your case, I'd expect 1 2 + 3 + 4 +