CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Wiki » September 2011 Contest Problem Editorials

September 2011 Contest Problem Editorials

 

 

Problem Tester for the September 2011 Contest was Shilp Gupta.

-------------------------------------------------------------------------------------------------------

CHEFBRO (written by Vamsi Kavala)

 

The problem saw a lot of successful submissions with mostly the same approach(but different implementations).

This problem is a simple application of Sprague-Grundy theorem (http://en.wikipedia.org/wiki/Sprague%E2%80%93Grundy_theorem) .

It is easy to see that if a coin is at (i,j) on a board with dimensions n x m, it is equivalent to the coin being on a board with dimensions (n-i) x (m-j). It means we can replace the board with a new board of dimensions (n-i) x (m-j). So we can look at it as a game where the given boards are getting smaller and smaller with every move until the size of the all the boards becomes 1x1.

We can associate grundy number with a board of dimensions n x m. Let g[n][m] be the grundy number associated with a board of dimension n x m. The board can be “resized” according to any of the VALID moves. So using the property of grundy numbers (http://en.wikipedia.org/wiki/Grundy_number) we can easily see that:

g[n][m] = mex( g[n-1][m], g[n-2][m], g[n-1][m-1], g[n-2][m-2], g[n][m-1], g[n][m-2])

Now, once we have the grundies of every given board, g[n1][m1] XOR g[n2][m2] XOR ......... XOR g[nc][mc] = 0 will tell us that the brother wins. If it is non-zero, our Chef wins!

Calculating the grundy numbers for every test case will give a TLE. If you observe carefully, the grundy numbers associated with every dimension will remain the same (i.e. do not depend on the test case). So we can precompute all the grundy numbers and use these values later.

The time limit for this problem was a little on the relaxed side. The above approach would pass without any trouble. But a lot of users solved it using the following approach which uses lesser space and time.

This approach is based on a simple observation which is not hard to see (working out a few examples manually would help you see it).  The observation being: Grundy number of any board will not exceed 2. More specifically, grundy of a board of dimensions of n x m will be given by:

(n+m-2)%3. We have the grundy of every board in O(1) time and space. We can proceed similar to the previous approach now.

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

CNTHEX (written by Imran Sunny)

 

This problem looks quite mathematical and most of the accepted solutions were mathematical too. But the time limit was quite relaxed to allow DP solutions too. I am going to explain a DP approach which does not involve any kind of math.

Fix the biggest edge, say 'M'. First calculate all the 5-tuples maintaining the "good" condition (hexagon or not hexagon). This is the easy part, you can just use backtrack to calculate that.

 

Now calculate all the 5-tuples which are not a hexagon and subtract those from the first to get the count of valid hexagons. This is the hard part.

A DP on digits with some precalculations can be used for that. Place the numbers in non-increasing order. So you are counting the number of ways to place 5 non-increasing integers such that the first integer is <=’X’ and their sum is <=’M’. The DP goes from left to right digit by digit of ‘M’.

The states of DP are: ( position, prefixX, prefixM, carry, an encoding of digits ( say S) )

position = position of the current digit of ‘M’ from left ( 0 ~ length of 'M')

prefixX  = whether the first number currently is a prefix of 'X' or less than 'X' ( 0 or 1)

prefixM  = whether the summed number currently is a prefix of 'M' or less than 'M' ( 0 or 1)

carry    =  the carry after placing the digits at this position ( 0 ~ 4)

S  =  a state encoding the relative orders of the partially filled numbers.

For example, say you are at position=3. And the partially formed numbers are:  {123,120,120,085,085}.

Now this can be encoded as : { 9,8,8,7,7}  [ The second and third number currently are equal and also the fourth and fifth and the numbers are placed in non-increasing order]. There are 16 different such encodings for five numbers.

Some precalculations can help in the DP part.

cnt1[ a][b][c1][S][c2][T][ z]= Number of ways to go( ways by placing 5 digits) from state S, carry c1 to state T, carry c2 such that the first digit is = 'a' and the digit after summing is=  'b'.

cnt2[ a][b][c1][S][c2][T][z] = .... first digit <= 'a' and the digit after summing is = 'b'

cnt3[ a][b][c1][S][c2][T] [z]= .... first digit  = 'a' and the digit after summing is <= 'b'

cnt4[ a][b][c1][S][c2][T][z]= .... first digit <= 'a' and the digit after summing is <='b'

For all the above arrays, z = 1 counts where last digit is zero and z=0 counts with any last digit.

All these can be precomputed by backtracking.

 

You can view solutions here:

Problem Setter Solution

-------------------------------------------------------

HAUNTED (written by David Stolp)

 

The challenge here is to simulate Chef's potential escape from the maze within the time constraint. For each time index from 0 until Chef faints from hunger, we calculate every position in the maze Chef could safely occupy. To advance from one time index until the next, for every safe square in the current time step, we mark all of its neighbors as safe in the next step, then mark squares that contain ghosts as unsafe, then move the ghosts, and mark their new locations as unsafe as well. Without optimizations, this approach will time out.

The key is to use bitsets to store the safe positions. Suppose we use one bitset per row of the maze. Then to simulate a north or south move, we need only apply a bitwise "or" between two rows. To simulate an east or west move, we shift the bitmask right or left. The walls of the maze can also be stored as a bitset, and a bitwise "and" operation is used to make sure Chef doesn't try to pass through a wall. Thus it only takes O(M+C) to simulate one time step, instead of O(M*N+C).

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------

SHORT (written by Gennady)

 

You can view the editorial here.

Anton Lunyov provided an alternative explanation to this problem. You can view his version here.

 

You can view solutions here:

Problem Setter Solution

-------------------------------------------------------

TILT (written by David Stolp)

 

This problem had the lowest number of successful submissions ever for a challenge problem, which I found quite surprising. I did not mean for the problem to be so difficult.

The author's solution works as follows:

The main solver function takes 2 parameters. The first parameter is the number of moves to look ahead, and the second parameter is a value that will change how the heuristic is calculated. From any state, consider all possible sequences of moves of length equal to the "look ahead" parameter. Rank these sequences based on the change in score, the heuristic, and whether any balls fell into holes. Execute the first move of the best sequence (or if there's a tie, choose randomly).

The heuristic is calculated as follows:

If there are no inverter balls left, add m for every remaining positive ball, and subtract m for every negative ball, where m is a parameter that varies from run to run. Otherwise, add m for every remaining positive ball and negative ball. Then, subtract cieling((remaining balls)/holes).

Since the algorithm may not always yield a solution, it is executed repeatedly with different parameters and random number seeds until a solution is found.

A better solution is to use a Beam Search. See ACRush's winning solution for an implementation.

 

You can view solutions here:

Problem Setter Solution

-------------------------------------------------------

TRMAG (written by Saransh Bansal)

 

You can view the editorial here.

 

You can view solutions here:

Problem Setter Solution

Problem Tester Solution

-------------------------------------------------------


Comments

  • Login or Register to post a comment.

Thanks for the editorial. An

hacker007 @ 11 Sep 2011 04:47 PM
Thanks for the editorial. An issue: one month has passed since August long contest finished, problem COMPLEXT still been "will be updated soon" status(http://www.codechef.com/wiki/august-2011-contest-problem-editorials). Here we have two this month. Better later than never.

hi, it will be very grateful,

blackBird @ 11 Sep 2011 06:46 PM
hi, it will be very grateful, if the test cases for SHORT be made available. Thanks.

I suggest that the test cases

hacker007 @ 11 Sep 2011 07:29 PM
I suggest that the test cases for all the problems are available like TopCoder, Codeforces always do when the contest is over. I believe CodeChef will benefit from this(more openness) and become more popular.

I have written explanation of

anton_lunyov @ 11 Sep 2011 07:37 PM
I have written explanation of my solution for SHORT. It is a simple text of size 4,5 Kb. But I don't how to post it properly here. I'm afraid that it would be absolutly not readable. Can I use html code here? Or it is better to make it a goodviewed and give the pdf-file?

Testcases for all problems

bknarendra @ 11 Sep 2011 10:24 PM
Testcases for all problems should be made available

seems someone has messed up

gultus @ 12 Sep 2011 01:47 AM
seems someone has messed up with the editorials. You can view an earlier revision at http://www.codechef.com/node/746905/revisions/747622/view

@anton_lunyov ... you can

blackBird @ 12 Sep 2011 12:01 PM
@anton_lunyov ... you can blog about it and share the link here :)

Hey Guys: Anton's explanation

admin @ 12 Sep 2011 12:52 PM

Hey Guys: Anton's explanation to the problem SHORT has been added to the editorial.

Some Solutions of Counting

utkarsh_lath @ 12 Sep 2011 02:51 PM
Some Solutions of Counting Hexagons are not visible. It says "Solution to this problem are not public." when i try to see submissions of rudradevbasak/Oleg/ hanshuai/ddr
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com