[spreadsheet][libreoffice-calc] Efficient checks of: 1. row number between columns; 1. formulas between rows in same columns - compare

I looked up how I can check efficiently whether:
I. the row number of each cell address matches with the row number, e.g.
row number 1: A1 B1 C1 ... AA1
row number 2: A2 B2 C2 ... AA2
etc.
To rule out mistakes like:
row number 1: A1 B1 C3 ... AA1
II.the formula in a column is similar over all rows, e.g.
in C1 the formula =A1*B1
in C2 the formula =A2*B2
etc.
To rule out mistakes like:
in C1 the formula =A1*D1
in C2 the formula =A2*B2*
To my amazement, I could not find any information about these important checks. I searched with many terms in different combination, e.g.
match compare index adjacent check error correct AND, OR and NOT functions etc.
Best and thanks in advance,
Roberto

Related

A task for generating most variety sprites to build a landscape tileset

We are given a number of basic sprites that are spread out over several layers. To generate a single tile we need to take any sprite from each layer and overlay them one by one.
For example:
Layer 1: Sea/Shore/Land
Layer 2: Rocks
Layer 3: Peak/ Plateau
Layer 4: Grass
Layer 5: Decorative elements
The number of sprites in each layer is arbitrary, but can only be within [1...16].
The number of layers is also arbitrary, but can only be within [1...6]
You need to get 16 or less (if more is impossible to generate) unique tiles, composed of as many different combinations of original sprites as possible - that is, the less they or their combinations will be repeated for different tiles, the better. Except for the first layer, for which the sprites must go in sequence for every next resulting set (see example below).
Example:
For:
{{a0, a1, a2}, // layer 1
{b0, b1, b2}, // layer 2
{c0, c1, c2}} // layer 3
The result should be something like this:
a0 b0 c0
a1 b1 c1
a2 b2 c2
a0 b1 c2
a1 b2 c0
a2 b0 c1
a0 b2 c1
a1 b0 c2
a2 b1 c0
a0 b0 c1
a1 b1 c2
a2 b2 c0
a0 b1 c0
a1 b2 c1
a2 b0 c2
a0 b0 c2
It seems to me that there are two ways of solving this problem:
Each layer is basically a one-dimensional set. To obtain all possible combinations, it is enough to perform Cartesian product of these sets. Then sort the resulting sets by " variety". 
But in this case there are two difficulties:
There can be quite a lot of combinations (we need only 16 of them).
It is necessary to compare not only the sets among themselves, but also with the already selected ones, in terms of the frequency of occurrence of the combinations included in them.
Build the most variety combinations straight away.
I have to admit that I'm stuck on both options - the first on the sorting algorithm, the second on the generation algorithm. I ask for help or hints.
I write in C++, but hints and solutions could be in any - the main thing for me to understand the idea.

How to calculate specific cards combination index

There are 22100 combinations of 3 cards from the 52 cards deck (52 * 51 * 50 / 3! = 22100).
Here is an enumeration of all these combinations:
static const ins cards_count = 52;
int boardId = 0;
for (int b1 = 0; b1 < card_count; b1++)
{
for (int b2 = b1 + 1; b2 < card_count; b2++)
{
for (int b3 = b2 + 1; b3 < card_count; b3++)
{
cout << boardId << endl;
boardId++;
}
}
}
Is it posssible to write an indexer fucntion that converts b1, b2, b3 to the boardId (without using a map)? If this is hard maybe you can advise some hash fucntion that maps b1, b2, b3 to the int size hash.
There is. It's pretty straightforward, too, and I briefly thought about writing down how, but then I realized I need quite a lot of mathematical notation for it.
The idea is to calculate how many combinations come before (or after) the one you need an index for.
The 3 from 52 combinations you state are correct, of course. Now let's enumerate how many come before b1,b2,b3:
First of all, there are all combinations with the first card smaller than b1. You have b1 options for the first card (since you start at 0), and for each of these options b1_, you have 52-b1_ choose 2 combinations. So, this number is the sum over b1_ from 0 to b1-1 over said binomial coefficient. With a little of math knowledge, you can derive a closed formula for that without actually evaluting a sum.
Second, there is all combinations with the first card having index b1 and the second card having index below b2. This is again a sum, but an easier one. You have one choice for the first card (b1) and b2-b1 (might be off by one, check that before you implement it) options for the second card. For each of these options (sum over b2_) you have 52-b2 options for the third card. This is a sum over consecutive numbers, and getting a closed formula for that is simple (hint: the sum of the first n numbers, starting at 1, is n*(n+1)/2, the sum of consecutive numbers not starting at one is the difference between two sums starting at 1).
Finally, you are interested in the number of combinations with the first two cards being b1 and b2 and the third being smaller than b3. This is simple, since that sum is exactly b3.
Only thing you need to do now is add the result from each item, and you have your index. I advise you take a piece of paper, do the math as proposed and get to a simple, easy to evaluate formula you can implement.
I would simply ignore the fact that cards in a set must be unique and sorted. Then your index is simply card1 + 52 * card2 + 52 * 52 * card3. With well formed sets not every index is reachable and the distribution is skewed towards lower numbers. Try hash = index % prime * 52 * 52 * 52 + index to even out the distribution. Try a low prime. The index part added ensures the hash will remain unique.
If you are only interested to assign and get the unique id of a set of any 3 different cards from a standard deck, there is no need to calculate the number of combinations for various set.
Let the cards in the deck be assigned with 1 to 52.
You could do it programatically as :
int getSetId (int card1, int card2, int card3){
if (card1 == card2 || card2 == card3 || card1 == card3)
//throw an exception
return (card1 * card2 * card3);
}
This will give you an unique id for all set combinations.
Edit 1:
In case there will be a duplicated id due to the multipliers forming the same product, instead of using 1 to 52, you can use the first 52 prime numbers.

Fill the Grid with three colors A and B and C

How to find the numbers of ways to fill a grid (3*n)array, with three colors A, B and C.
Under the following constraints:
1) All the n cells of the same row can't have the same color.
2) All the 3 cells of the same column can't have the same color.
Sample input : if n=2, then output or number of ways = 174.
Please explain the approach for this.
This answer given by sam29 on codefores.
We can solve this question using Inclusion-Exclusion principle. So, let's first consider only the first column of the matrix. We can easily deduce that there are 24 different ways to fill that column keeping in mind that we can't have the same letter in the complete column. Now, we can directly say that the total ways to fill the complete matrix will be 24^N (Name this value as X1). In this answer, we have made sure that all the column contains distinct alphabets. But we need to consider the cases a row contains the same letter. So, now we will use inclusion-exclusion principle.
Find the number of cases with one row same. Fix 'A' in the first row. Now, take only the first column and you can deduce that there are 8 distinct ways to fill the 2nd and the 3rd row of the first column keeping in mind we can't have the same letter in the complete column. So, now we can find the total number of ways to fill all the N rows as 8^N. Now, we can do the same thing with 'B' and 'C' in the first row and similarly, we can repeat the process for the 2nd and the 3rd row. So, the total number of ways will be 9*8^N (Name this value as X2).
Find the number of cases with two rows same (Name this value as X3). This is the trickiest part of the question. I'll explain this at last.
Find the number of cases with all the three rows same but we can't have the same letter in a single column. This is pretty easy and is equivalent to the number of ways to fill a single column and 3 rows. So, the answer is 24 for this scenario (Name this value as X4).
Now, the final answer will be X1-X2+X3-X4.
Now, coming back to the answer for 2nd scenario. So, we will try to find the answer for the case when the first row and second row is same and we can repeat that process for the 2nd row and 3rd row AND 1st row and 3rd row. Basically, we can multiply the answer we will calculate now with 3. Okay, now take only the first column. Now, you can see that there will be 2 scenarios, one will be when the first and second row contains the same letter and in that case, we have to place a different letter in the 3rd row because else we will violate our condition of the distinct column. So, the total number of ways in the first scenario will be 3*2^N (I have skipped some part but I have provided the exact reason and a little further thinking and you will get the solution). Now, for the next scene when the first and second row contains different letters. In that case, you can place any letter in the 3rd row. Again try to think a little more and you will get the answer as 6*3^N. So, the total answer will be 3*2^N + 6*3^N. And as I said before we need to multiply it by 3 (Number of ways to choose 2 rows from 3 rows). So, X3 will be 3*(3*2^N + 6*3^N).
The complexity is pretty direct, you can do precomputation or apply exponent function every time.
Thanks.
This is combinatorial question, for sure it is better to post questions like this on math.stackexchange.com.
A row can be in two different configurations: having two colors (ABA) and having three colors (ABC). If we have last row of some configuration, lets check possibilities for next row.
A | B B B C C
B | A A C A A
A | B C B B C
A | B B B C
B | A C C A
C | B A B B
Set:
A_n : number of dimension n matrices where last row is of ABA configuration,
C_n : number of dimension n matrices where last row is of ABC configuration,
X_n : number of dimension n matrices = A_n + C_n.
From upper list of possibile next row it holds:
A_n = 3 * A_(n-1) + 2 * C_(n-1) = 2 * X_(n-1) + A_(n-1)
C_n = 2 * A_(n-1) + 2 * C_(n-1) = 2 * X_(n-1)
=>
X_n = 4 * X_(n-1) + A_(n-1)
Result to question is X_n, for which calculation A_n is needed, and initial values are A_1=6, X_1=12.
Update:
Search in OEIS for values 2, 9, 41, 187 (upper sequence if colors are not important, real number divided by 6), produces sequence A020698. Sequence mentions similar problem, and suggests that upper recursion can be stated in simpler manner:
X_n = 4 * X_(n-1) + A_(n-1)
= 4 * X_(n-1) + A_(n-1) + X_(n-1) - X_(n-1)
= 5 * X_(n-1) + 2 * X_(n-2) + A_(n-2) - 4 * X_(n-2) - A_(n-2)
= 5 * X_(n-1) - 2 * X_(n-2)

Understanding Extended Euclid Algorithm

I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The boxes are of two types:
Type 1: each box costs c1 Taka and can hold exactly n1 marbles
Type 2: each box costs c2 Taka and can hold exactly n2 marbles
I want each of the used boxes to be filled to its capacity and also to minimize the total cost of buying them. Since I find it difficult for me to figure out how to distribute my marbles among the boxes, I seek your help. I want your program to be efficient also.
Input
The input file may contain multiple test cases. Each test case begins with a line containing the integer n (1 <= n <= 2,000,000,000). The second line contains c1 and n1, and the third line contains c2 and n2. Here, c1, c2, n1 and n2 are all positive integers having values smaller than 2,000,000,000.
A test case containing a zero for n in the first line terminates the input.
Output
For each test case in the input print a line containing the minimum cost solution (two nonnegative integers m1 and m2, where mi = number of Type i boxes required) if one exists, print "failed" otherwise.
If a solution exists, you may assume that it is unique.
Sample Input
43
1 3
2 4
40
5 9
5 12
0
Sample Output
13 1
failed

How to list all the possible sums of combinations in an array for C++?

I have my homework assignment and I have no idea how to start with the code for this kind of problem.
Let say I have an integer array with consist of n elements,
[A][B][C][D][E] (We have 5 elements for example)
I want to list out all the sum of possibility such that I want to print out the
sum of all combination (ABCDE, ABCD, ABCE, ACDE, BCDE, ABC, ABD, ABE, ACE, ADE, BDE, CDE, AB, AC, AD, AE, BC, BD, BE, CD, CE, DE, A, B, C, D and E)
Another example would be 4 elements in an array ([A][B][C][D])
I want to list all sum of combination of (ABCD, ABC, ABD, ACD, BCD, AB, AC, AD, BC, BD, CD, A, B, C and D).
Well, here's a simple rule to follow:
The set of all combinations of "ABCDE" is composed of those combinations which contain (and thus start with) "A" and those which don't contain "A". In both cases, all combinations of "BCDE" can occur. Of course, combinations of "BCDE" can be treated in the same way.
When you say "list out all the sum of possibility" do you mean you want to know how many combinations are actually possible?
If so, then search on combinations of N items taken K at a time; there are pages on this very site addressing this problem. You would then simply add the number of combinations of (by 5) + (by 4) + (by 3) + (by 2) + (by 1) to get your total "sum of possibilities".
Or do you mean you have an array of values and you literally want to print out the different sums represented by different combinations of the elements? In that case you need to actually enumerate all the combinations and evaluate the sums.
So given an array of { 1, 2, 3, 4, 5 } you can encode this as "A", "B", "C", "D", "E". Examples of tuples would be:
ABCDE = 1+2+3+4+5
ABE = 1+2+5
BCE = 2+3+5
etc, where you use the encoded enumeration to select the addends for your sum. Note that deciding whether to allow or disallow duplicates (i.e., is "DE" different from "ED") will have a very large effect on your results; in most cases this will probably not be what you want.
If you have 3 elements, you may imagine each element placed at a certain position from 1 to 3 (or 0 to 2) and a boolean array representing whether the element is contained in a certain set.
ABC remark
--- ---------------------
000 no element in the set
001 one element, C
010 one element, b
100 ...
011 two elements, b and c
...
111 all elements contained
Now if you calculate the number of solutions, which is 2³ in this case, and generate a function, which does a mapping from a binary representation to a set, from 011 to (b, c) for example, then you can easily program a loop, which iterates from 0 to max-1 and returns all sets, produced by your mapping function.