August 2011 Contest Problem Editorials |
Problem Tester for the August 2011 contest was Shilp Gupta.
-------------------------------------------------------------------------------------------------------
BLOCKDRO (written by Ivan Mistreanu)
This problem is an exercise in BFS/DFS. The main point here seems to be how to encode the state so that the search to be fast enough. The author’s approach is as follows. There are not more than 25 stones. We sill keep the mask of which stones are left of the board. And we should add the number of the stone we are standing on to the state. So we have at most 25*2^25 states which surely too much, but actually many of the states are going to be inaccessible even in the worst case (also some of the states reachable from the initial state will be unsolvable and can be removed from the search). However we should still try to make the search as fast as possible. For each state we can move to at most 8 other states. In order to make calculation possible move fast for each stack of stones we will keep the number of the first stone on the stack and also for each move the number of the stack this moves lands on or -1 in the move is illegal for the stack. Also for each stone we will keep the number of the stone under it in the stack or -1 if it’s the last stone in the stack and the number of the stack this stone belongs to. Using this information the each state can be resolved rather fast. So now BFS can be used to solve the problem. Since the number of moves to reach any state in fixed DFS can be used as well.
Several optimizations can be applied in this problem. For example we can keep the number of stack instead of the number of stone we are on. Having that the test cases are random the amount of stacks should be lesser than the amount of stones. We can determine unsolvable states and remove them from the search. Since we know the final state – meet in the middle approach can be used for some additional time win. Seems that dynamic programming can be also used in this problem.
You can view solutions here:
Problem Setter Solution
Problem Tester Solution
-------------------------------------------------------
COMPLEXT (written by Vishal Anand)
Will be updated soon
You can view solutions here:
Problem Tester Solution
-------------------------------------------------------
DIVISORS (written by Anton Lunyov)



You can view the above image in PDF format here: http://www.codechef.com/download/Solutions/2011/August/DIVISORS_pdf.pdf
You can view solutions here:
Problem Setter Solution
Optimized Solution
-------------------------------------------------------
HPARITY (written by Abdullah Mahmud)
Lucas' Theorem: http://en.wikipedia.org/wiki/Lucas%27_theorem
You can view the above image from here.
You can view solutions here:
Problem Setter Solution
Problem Tester Solution
-------------------------------------------------------
IGAME (written by Snigdha Chandan)
The strategy of the game revolves around "LOSS" positions and "WIN" positions.In a "LOSS" position ,the player whose turn it is to move will lose with best play,while in a "WIN" position ,the player whose turn it is to move will win with best play.The optimal strategy from a "WIN" position is to move to any reachable "Loss" psition.
The classification of positions into "WIN" and "LOSS" can be carried out recursively with the following 4 rules:
Rule-1:(0,0) is the "TARGET" position.
Rule-2:Any position from which the "TARGET" position can be reached in a single move is a "WIN" position.
Rule-3:If every move from a particular position leads to a "WIN" position ,then that position is a "LOSS" position.
Rule-4:Any position from which the "LOSS" position can be reached in a single move is a"WIN" position.
Pre-processing step:Now following the rules of the game along with the above 4 rules the cells in a 1000X1000 matrix can be filled up as either "WIN" or "LOSS" for the first player ,i.e ALICE.
Solving the Problem:
Step-1:The actual target position (p,q) is shifted to the origin.
Step-2:Now the initial position (m,n) has also been accordingly changed and comparing the new initial position with the corresponding cell in the pre-processed matrix the winner can be decided.
You can view solutions here:
Problem Setter Solution
Problem Tester Solution
-------------------------------------------------------
SHORTCIR (written by Ivan Mistreanu)
The solution can be described in several not really hard steps:
1. Let us build a syntax tree for the expression with AND, OR, NOT as inner nodes and variables as leaves. Respect the standard priority of the operation when building the tree. However associativity doesn’t matter for the sake of this problem. Right away we can define P(V) for each node meaning the probability that this node will return true.
2. Now we remove all NOTs from the tree using the following rules:
a. NOT (NOT (E)) = E.
b. NOT (AND (E1, E2)) = OR (NOT (E1), NOT (E2)).
c. NOT (OR (E1, E2)) = AND (NOT (E1), NOT (E2)).
d. NOT (V) = U, where V and U are variables (U is a new variable), having P(U) = 1 – P(V). Using this rule is correct, because all the variables are different.
3. So after step 2 we have a tree with only AND and OR as inner node. Now we consider those operations to accept more than two operands and merge the nodes of the tree in the following way: AND(E1, AND(E2, E3)) = AND(E1, E2, E3), OR(E1, OR(E2, E3)) = OR(E1, E2, E3).
4. After step 3 we will end up with AND/OR tree: any child of AND node will be a variable or an OR node, and any child of OR node – a variable or an AND node. For such a tree we can recursively calculate the expected amount of evaluations needed to result this node. The best order of evaluation of children can be derived for each node. The answer is the expected amount of evaluations for the root.
For the best order of evaluations for some inner node let us consider the following node: X = AND(E1, E2, …, En). If we fix any order of evaluations of children: F1, F2, .. Fn. Then the expected number of evaluations will be: M(X) = M(F1) + P(F1)*(M(F2) + P(F2)*(M(F3) + …)). Let’s look at any two children. If we place Fi before Fj we get
[… + M(Fi) + P(Fi)*(… M(Fj) …)],
otherwise we get
[… + M(Fj) + P(Fj)*(… M(Fi) …)].
So the lesser is the value of this part the better is the order. We have:
M(Fi) + P(Fi)*M(Fj) ? M(Fj) + P(Fj)*M(Fj)
M(Fi)*(1 – P(Fj)) ? M(Fj)*(1 – P(Fi))
M(Fi)/(1 – P(Fi)) ? M(Fj)/(1 – P(Fj))
That means that in this case we should sort the children by M(E)/(1 – P(E)) to get the best order of evaluations. Similarly can be done for OR nodes.
However such approach turns to produce optimal solutions only for certain types of and/or trees. For example, for and/or trees of depths no more than two.
Additional information can be found in this paper: http://www.cs.toronto.edu/~molloy/webpapers/aotree.ps
You can view solutions here:
Problem Setter Solution
Problem Tester Solution
-------------------------------------------------------
Comments


Some really interesting
I was seeing some interesting
@gultus: Yeah, I solved IGAME
@gultus::In fact the solution
divisors solution absolutely
Some of these problems were
Please provide a full
Just commenting on the
@javadecoder, look at it this
@Snigdha Chandan can u
@rijin:http://en.wikipedia.or
@pagrame As they mentioned at
@Snigdha not about problem;
@admin: I think all these
@admin: I think all these
For the Infinite Grid Game,
my answer for igame was
@javacoder multinomical
So, who is the writer of
Also, wouldn't it be better
@mmaxio Try to increase the
Hey Guys, The PDF can be
Hey Guys,
The PDF can be found here: http://www.codechef.com/download/Solutions/August/DIVISORS_pdf.pdf
When will COMPLEXT be
I can describe how my optimal
@snapdragon: Thanks, that's
@admin, The next contest is