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 » November CookOff Contest Problem Editorials

November CookOff Contest Problem Editorials

 

RECIPE (written by David Stolp aka PieGuy)

The problem here is divide all numbers by some constant so that the divisions have no remainder. We produce the smallest result by dividing by a number that is as large as possible, that is the greatest common divisor. The greatest common divisor can be computed efficiently by Euclid's algorithm, but in this case it was fast enough to simply check all numbers from 1 to 1000 for divisibility.

 

You can view the solutions here:

Problem Setter Solution

Problem Tester Solution

 

LIFE (written by David Stolp aka PieGuy)

First solution: If we choose an arbitrary assignment for any two adjacent cells of the original state, then the entire rest of the state can be determined from the current state. For example, if we choose values for original[0] and original[1], then current[1] is defined as original[0]^original[1]^original[2] (where '^' is exclusive or), thus original[2]=original[0]^original[1]^current[1]. Repeating the process allows us to determine all cells of the original state. Depending on the initial assignment, this may or may not yield a valid solution. There are only 4 initial assignments to try, and we simply count the number that yield valid solutions.

Second solution: Consider the state of the game as the binary representation of an N-bit integer. Let A represent the original state of the game, and B the current state. Then the simulation step simply implies that B=F(A), where F(A)=(A<<<1)^A^(A>>>1), '<<<' is left rotation (not left shift), and '>>>' is right rotation. Now consider these cases:

  • If N is divisible by 3, then F(011011...011011)=000...000, hence the solution cannot be unique (becase F(X^Y)=F(X)^F(Y), there will either be 0 solutions, or 4 solutions). Denote G(B)=(B<<<1)^(B<<<2)=A^(A<<<3). Now G(B)^(G(B)<<<3)^(G(B)<<<6)^...^(G(B)<<<(N-3)) should equal 0. If this is the case, then there are multiple solutions. Otherwise there is no solution.
  • If N has a remainder of 1 when divided by 3, then F(110110...1101101)=1000...000, hence there is always a unique solution, given by B^(B<<<1)^(B<<<3)^(B<<<4)^(B<<<5)^...^(B<<<(N-3))^(B<<<(N-1)).
  • If N has a remainder of 2 when divided by 3, then F(101101...10110110)=1000...000, again implying a unique solution, given by B^(B<<<2)^(B<<<3)^(B<<<5)^(B<<<6)^...^(B<<<(N-2))^(B<<<(N-1)).

 

You can view the solutions here:

Problem Setter Solution

Problem Tester Solution

 

SOCIAL (written by David Stolp aka PieGuy)

The chef chooses a random derangement of the chairs as the follow-ups. If we focus our attention on a single guest, that guest will be back in their original seat after R ringings if and only if the cycle they belong to has a length that divides R. Thus, for each divisor D of R not exceeding N, we count the number of derangements where a specific element (say, the first one) belongs to a cycle of length D, then divide by the total number of derangements. This will give us the probability of a single guest being back in his/her original seat. To get the expected number of guests, simply multiply by the total number of guests. The total number of derangements where the first element belongs to a cycle of length D is the total number of possible cycles ((N-1)!/(N-D)!), times the number of derangements on (N-D) elements. The number of derangements is calculated by the recurrence derange[0]=1, derange[1]=0, derange[i]=(i-1)*(derange[i-1]+derange[i-2]).

You can view the solutions here:

Problem Setter Solution

Problem Tester Solution

COOKOFF (written by David Stolp aka PieGuy)

Assume, without loss of generality, that C1≤C2 (otherwise swap them). There are 2 cases to consider.

If there are more than M available problems with difficulty ratings between C1 and C2, inclusive, then all problems with a difficulty rating less than C1 or greater than C2 can be ignored. Now, we can decide, given a maximum difficulty gap G, if it's possible to select M problems such that the difficutly gap does not exceed G in O(N) time (assuming the difficulty ratings have been sorted ahead of time). To accomplish this, we use a greedy method where we select P1 as the problem with the highest difficulty rating not exceeding C1+G, then Pias the problem with the highest difficulty rating not exceeding Pi-1+G for 1<i≤M. If PM≥C2, then there is a solution. The minimum value of G can be found using binary search.

If there are at most M available problems with difficulty ratings between C1 and C2, then we choose all of them. The remaining problems are chosen greedily, always choosing the problem that produces the smallest difficulty gap between itself and the nearest already chosen problem.

 

You can view the solutions here:

Problem Setter Solution

Problem Tester Solution

 

KNIGHTS (written by David Stolp aka PieGuy)

If we construct a graph where each vertex corresponds to a square of the chessboard, and an edge connects two squares if they cannot both hold knights. Our task is then to find the size of the maximum independent set of this graph. Note that if we color the squares of the chessboard black and white, a knight on a black square can only attack white squares, and a knight on a white square can only attack black squares. Thus, the graph is bipartite. Now, by König's theorem, The size of the maximum independent set is the total number of vertices minus the size of the maximum matching. The maximum matching can easily be found in O(VE) using an augmenting paths algorithm, where V is the number of vertices and E is the number of edges. However, in this case E is O(V), so the solution can be found in O(V2).

 

You can view the solutions here:

Problem Setter Solution

Problem Tester Solution

 


Comments

  • Login or Register to post a comment.

In problem SOCIAL, you

mmaxio @ 28 Nov 2010 03:43 AM

In problem SOCIAL, you calculate 50! in double. 50! has 65 digits in decimal, but double has only ~20 precise digits. So, why do you think result of your program will be precise up to 5 digits?

Could someone give a more

MarioYC @ 28 Nov 2010 01:44 PM

Could someone give a more detailed explanation of why the greedy approach works for COOKOFF?

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