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 » December 2011 Cook-off Problem Editorials

December 2011 Cook-off Problem Editorials

 

Problem Setter for the contest was Hiroto Sekido.
Problem Tester for the contest was Jingbo Shang.

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

CIEL8STR : Ciel and New Menu

 

Let S[a..b] denote the substring from a-th character to b-th character of S.

One of the solutions for this problem is to use the fact that 1000 is multiple of 8. Let consider the number of the integers S[i..digit] mod 8 = 0 for all i. This number can be calculated as following:

 

  • count := 0.
  • If S[digit] mod 8 = 0 and S[digit] ≠ 0, count := count + 1.
  • If S[digit-1..digit] mod 8 = 0 and S[digit-1] ≠ 0, count := count + 1.
  • If S[digit-2..digit] mod 8 = 0, count := count + the number of non-zero digits in S[1..digit-2].

 

Therefore, at first, calculate the number of non-zero digits in S[1..i], for all i, then this problem can be solved with O(|S|) time.

This problem also can be solved by using DP (Dynamic Programming). Let dp[digit][num] = the number of the integers S[i..digit] mod 8 = num for all i. Then dp[digit+1][aft] = (sum of dp[digit][bef]) + a, where (10 * aft + S[digit+1]) mod 8 = bef, and if aft = S[digit+1] ≠ 0, a = 1, otherwise a=0. Note that this algorithm will work for finding the number of the numbers multiple of 7, for example.

 

Problem Setter's solution.
Problem Tester's solution.

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

CIELAB : Ciel and A-B Problem

 

This is the easiest problem in the set. This problem can be solved various methods. For instance, if A - B mod 10 = 9, print A - B - 1, otherwise print A - B + 1. Be careful not to print 0 if A - B = 1. Your answer must be a positive integer.

 

Problem Setter's solution.
Problem Tester's solution.

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

CIELBTL : Ciel and Battle Arena

 

This problem can be solved by using DP (Dynamic Programming). Basically, the relationship dp[VA][VB][MA] = max(dp[ceil(VA/2)][ceil(VB/2)][MA-1], sum of dp[VA-i][VB-j][MA] / ((SA+1)*(SB+1)), for 0 ≤ i ≤ SA, 0 ≤ j ≤ SB) is hold, where dp[VA][VB][MA] is the answer for the input VA, SA, VB, SB, MA. However there are some troubles.

First, dp[VA][VB][MA] is appeared both of the left hand side and the right hand side. So, dp[VA][VB][MA] cannot be calculated directly by using above expression. But this is not a hard problem. Transpose that term, then we obtain dp[VA][VB][MA] = max(dp[ceil(VA/2)][ceil(VB/2)][MA-1], sum of dp[VA-i][VB-j][MA] / ((SA+1)*(SB+1)-1), for 0 ≤ i ≤ SA, 0 ≤ j ≤ SB, i+j > 0).

Second, dp[a][b][c] is unknown for a ≤ 0, b ≤ 0. We can use binary search for solving this issue. Let the correct answer be P. If we set dp[a][b][c] = X for a ≤ 0, b ≤ 0, then X ≤ dp[VA][VB][MA] ≤ P or P ≤ dp[VA][VB][MA] ≤ X is hold. Therefore, we can calculate the correct answer by using binary search. Note that, if VA ≤ 0 and VB > 0, dp[VA][VB][MA] = 0, and if VA > 0 and VB ≤ 1, dp[VA][VB][MA] = 1.

Third, if dp[a][b][c] is calculated naively with O(SA*SB) time, we will get TimeLimitExceeded. To avoid this, a cumulative sum can be used. Let sm[a][b][c] be sum of dp[x][y][c] for M ≤ x ≤ a, M ≤ y ≤ b, where M is a small integer such as -101. Then, sum of dp[a-i][b-j][c] for 0 ≤ i ≤ SA, 0 ≤ j ≤ SB = sm[a][b][c] - sm[a-SA-1][b][c] - sm[a][b-SB-1][c] + sm[a-SA-1][b-SB-1][c], and sm[a][b][c] = sm[a-1][b][c] + sm[a][b-1][c] - sm[a-1][b-1][c] + dp[a][b][c]. By using this relationship, dp[a][b][c] can be calculated with O(1).

 

Problem Setter's solution.
Problem Tester's solution.

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

CIELQUIZ : Ciel and Quiz Game

 

The model solution for this problem is very simple, and this problem can be solved for larger N. However I have set N ≤ 36 for keeping away from guessing the simple solution from the constraint. Naive bruteforce algorithms with O(N!/K!/(N-K)!) or O(N*2N/2) will get TimeLimitExceeded. I would like to say that the tester can solve this problem by an optimized bruteforce solution.

Let's start the explanation of the model solution. We don't have to check all combinations. At first, sort Pi, and consider the case where P1 ≤ P2 ≤ ... ≤ PN. There is at least one answer that first i problems and last K-i problems are chosen, that is, problem 1, 2, ..., i, and problem N-K+i+1, N-K+i+2, ..., N are chosen. This method is O(N2 + K3) time implemented naively as the setter's solution.

A short proof is the following. Let one of the optimal sets be the problem A1, A2, ..., AK, and let the set don't contain problem B and C, where B < AK < C. For the set of K-1 problems A1, A2, ..., AK-1, let x be the probability that all problems are solved correctly, and y be the probability that K-2 problems solved correctly. Then, the probability of solving K-1 problems correctly for the set of K problems A1, A2, ..., AK-1, Z is (1 - PZ/100) * x + PZ/100 * y. Because this is the linear function of PZ, the set of problems A1, A2, ..., AK-1, B and A1, A2, ..., AK-1, C are also optimal sets.

 

Problem Setter's solution.
Problem Tester's solution.

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

CIELWALK : Ciel and Random Walk

 

Let the matrix P satisfy Pi,j = the probability that Ciel is in j-th node at time 1 if Ciel is in i-th node at time 0. Then, (Pt)i,j is equal to the probability that Ciel is in j-th node at time t if Ciel is in i-th node at time 0.

At first, we need to compress the graph as the below figure. The compressed graph contains only the nodes having beautiful flowers, start node, and end node. And the weight of edges Qi,j is the probability that Ciel goes from i-th node to j-th node, without visiting other nodes having beautiful flowers. Qi,j can be calculated as (P∞)i,j, where Pi,j is the weight of the lower left of the below figure. Then we obtain the answers by calculating Q∞ corresponding the right graph of the below graph.

We may calculate PM instead of P∞, where M is an enough large integer. The expectation of the number of Ciel's move can be around 30! (2108 < 30! < 2109) for some graphs. (Think the graph that the edge from node i to node j exists iff i+1 ≥ j.) Therefore, M must be greater than 30!. For instance, 2110 is enough.

One important thing for calculating PM is thinking about a numerical error. The sums of Pi,k for all k should be 1. But in the computer, this may be slight different from 1. Therefore, the elements of PM will be 0 or inf. To avoid this trouble, a normalization is needed, that is, the sums of probabilities should be keep 1.

The total complexity for this algorithm is O(N4*log N!).

Note that P∞ may be calculated by solving linear equations with Gaussian elimination. However, because Gaussian elimination is not good algorithm in terms of a numerical error, this method may get WrongAnswer. If a bignum arithmetic is used, this method also can be accepted.

 

Problem Setter's solution.
Problem Tester's solution.

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


Comments

  • Login or Register to post a comment.

can someone pls explain quiz

rahulsingal14 @ 19 Dec 2011 12:31 AM
can someone pls explain quiz game problem in detail ( how the setter used the linearity property of Pz ?? )

There is something strange

anton_lunyov @ 19 Dec 2011 12:35 AM
There is something strange with complexity for CIELWALK. It seems that complexity is O(N^3 * log N!) = O(N^4 log N) by Stirling formula. (Here N^3 comes from matrix multiplication)

@rahulsingal14 Let f(a, b, c,

hiroto_adm @ 19 Dec 2011 12:42 AM
@rahulsingal14 Let f(a, b, c, ..) = the probability that K-1 problems can be solved. And let P[x] < P[y] < P[z], then f(a, b, ..., x) <= f(a, b, ..., y) <= f(a, b, ..., z) or f(a, b, ..., z) <= f(a, b, ..., y) <= f(a, b, ..., x).

@anton_lunyov P^M is

hiroto_adm @ 19 Dec 2011 12:45 AM
@anton_lunyov P^M is calculating the O(S) times, for various start nodes. So time complexity of my solution is O(N^4 * log N!). I realize now that it is equivalent to O(N^5 log N), as you say.

@rahulsingal14 Because the

thomasahle @ 19 Dec 2011 03:09 AM
@rahulsingal14 Because the expression is linear in P_z, it is either at max if P_z is minimized or maximized.

I don't see how what is

thomasahle @ 19 Dec 2011 03:14 AM
I don't see how what is written about the quiz is a proof though. It is not a proof by contradiction, it only states that there is another set, which is 'at least as good'. It states that we can always change one problem for one with a higher or lower probability, to obtain a better or equal solution. This is a greedy process, but we have no proof that it will find the global maximum. Or am I mistaken?

@thomasahle Only the key

hiroto_adm @ 19 Dec 2011 05:24 AM
@thomasahle Only the key point of a proof is written in the editorial. I think the rest part is trivial. For example, if N=20, K=5, the set (1, 2, 9, 11, 20) is optimal, then (1, 2, 9, 19, 20) is also optimal, and (1, 2, 18, 19, 20) is also optimal.

@thomasahle It follows from

anton_lunyov @ 19 Dec 2011 05:24 AM
@thomasahle It follows from here that one of the optimal set will have the form prefix+suffix (if we sort problems by Pi). So we need check only sets of this form. Indeed, suppose a contrary that there is some set S1 that has strictly greater value than any pref+suf set. Then it is clear that there exist problems A, B, C such that P(A)<=P(B)<=P(C), A,C are not in the set and B is in the set (otherwise this is pref+suf set). But than as it is written in the editorial replacing B by A or by C will not decrease the value. So now let's consider this new set S2. If it has the form pref+suf than we obtain a contradiction since we suppose that any pref+suf set has strictly lower value than for S1. But because of our construction val(S2)>=val(S1). If S2 has not this form than we again can do a replacement described above and obtain a set S3 for which val(S3)>=val(S2)>=val(S1). So again if S3 is pref+suf we obtain a contradiction. Clearly the process of replacements is finite (for example, consider the value PREF + SUF where PREF is the number of problems in the set with lowest Pi (among all N problems) and SUF - with largest. We can always do a replacement in such manner that PREF + SUF is increased (choose the lowest possible A and the largest possible C). Since it is bounded and integer than we can't increase it forever). Once this process will stopped we obtain pref+suf set Sn with val(Sn)>=val(S1). This is a contradiction.

Regarding CIELQUIZ, I

shubh09 @ 19 Dec 2011 09:41 PM
Regarding CIELQUIZ, I approached the problem using Dynamic Programming, but got a 'Wrong Answer' for my solution. Here is how I went about it: Let S(n,k) denote the max probability of solving k-1 problems out of k problems, chosen from the problems [1...n]. Clearly, S(n,k)=max(S(n-1,k),((S(n-1,k-1)*P(n))+(S1(n-1,k-1)*(1-P(n))))) (The first argument of the max function is the case when the nth problem is not chosen. The second argument is for the case when it is chosen. S1(n,k) is the product of the probabilities associated with the k problems chosen out of the problems [1...n]. Here is how I update S1: If the first argument is chosen, S1(n,k)=S1(n-1,k) If the second argument is chosen, S2(n,k)=S1(n-1,k-1)*P(n).) I gather now that the solution provided in the editorial is much more elegant and simpler to implement, but I would really like to know what, if anything, is wrong with my solution.

@shubh09 If P[1] = 50, P[2] =

hiroto_adm @ 19 Dec 2011 10:41 PM
@shubh09 If P[1] = 50, P[2] = 100, P[3] = 0, then S(2,1) and S(2,2) should be 50%. Therefore S(3,2) will be also 50% if your recursion is used. However S(3,2) should be 100%, by choosing 2nd and 3rd problems.

@shubh09 I think your dp

jingbo_adm @ 19 Dec 2011 10:43 PM
@shubh09 I think your dp solution has some problems. You just maximize the S(n,k) while ignoring the S1(n,k) will affect the values later. For example, if two choice are same, which S1 will you choose? So it is not the best choice if you choose wrong S1. I hope you know my meaning.

http://pastebin.com/xapwXkLV

jainharshit @ 21 Dec 2011 04:43 PM
http://pastebin.com/xapwXkLV can anybody tell me why I am getting it as wrong answer on codechef for the very first problem of this contest ?? plz...

@admins - Thanks for the

shubh09 @ 22 Dec 2011 01:14 AM
@admins - Thanks for the prompt replies! Got my mistake.

In the problem "CIEL8STR :

jainharshit @ 22 Dec 2011 09:53 PM
In the problem "CIEL8STR : Ciel and New Menu" .. Can anyone explain the 4th star point which states that If S[digit-2..digit] mod 8 = 0, count := count + the number of non-zero digits in S[1..digit-2]. Why is this ?
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