You are on page 1of 7

Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...

You are not logged in. Please login at www.codechef.com to post your questions! ×

<< Back to CodeChef

questions tags users badges unanswered ask a question about faq

CodeChef Discussion
questions tags
Search Here...
users

Unofficial Editorials January Cook Off Follow this question


By Email:

Hello Guys, The editorials for January Cook of are cooked (first two only). Once you sign in you will be
able to subscribe for any
11 Problem: SURVIVE updates here
By RSS:
Problem Statement:
Answers
Given a shop opening from Monday to Saturday, you are allowed to buy N sweets everyday the shop is
open and you eat K sweets everyday, determine if you can survive for next S days. Answers and Comments

Difficulty:Cakewalk
Solution: Question tags:
The constraints of problem are small enough to check for everyday.
editorial ×13,001
For every ith day (1<=i<=S) not being divisible by 7, buy N sweets. Eat K sweets everyday. If at any
cook90 ×81
day you have less than K sweets to eat, answer is impossible.
multhree ×29
If you survive S days, and supposing C is the number of remaining sweets and A being number of days
you bought sweets, just subtract C/N from A. (The number of sweet boxes untouched). survive ×28

Link to Solution here. question asked: 22 Jan, 00:41

question was seen: 2,398


Problem: MULTHREE times
Problem Statement: last updated: 2 days ago
Given K, d0 (first digit) and d1 (second digit), we care to check that K-digit number is divisible by 3. if
K> 2, Remaining digits of our Number are given by: \begin{equation} D_{i} = {\textstyle \sum^{j-
1}{0}} D{j} (mod 10) \end{equation} Related questions

Difficulty: Easy SURVIVE - Editorial

Solution: MULTHREE - Editorial


First thing to note is that if sum of digits of a number is divisible by 3, number if divisible by 3.
FINDA - Editorial
Another thing, (I would recommend running a brute force for all possible values of d0 and d1 to
FARGRAPH - Editorial
observe this pattern, do give a try before reading).
MEDIAN - Editorial
After first three digits, only the pattern "2486" repeats till end of the number. This means that we can
split our number into 4 parts. CIELLAND - Editorial

1. First 3 digits, manually take sum. PARALLEL - Editorial


2. Next 0 to 3 digits (pattern may start at 4, 8 or 6, Taking care of that).
CNTHEX - Editorial
3. "2486" X number of times. Add X*20 to sum.
4. Last 0 to 3 digits (pattern may end at 2, 4 or 8). TASUFFIX - Editorial

Finding X: Check for start point of pattern (will be from p = 3 to 6 in 0-based indexing), X = floor((K- SAD - Editorial
p)/4).

Edge case: If fourth digit is 0, we take sum of first three digits and check its divisibility by 3.

Link to Solution here.

PS: I have intentionally kept the length of editorials short. If you don't get it, feel free to post a
comment. :)

edited 22 Jan, 00:55 asked 22 Jan, 00:41


editorial 5★ taran_1407
[ 2.8k ]●8●29
accept rate: 22%

"After first three digits, only the pattern "2486" repeats till end of the number".

This is not true. It can also be all 0, e. g. 2, 3, 5, 0, 0, 0 ...

3★ eugalt (23 Jan, 09:20)

I have already mentioned it

See edge case mentione above :)

5★ taran_1407 (23 Jan, 09:45)

For 18 out of 90 valid d0 , d1 combinations d4 = 0. That's 20%. You call it "edge case"? :)
3★ eugalt (24 Jan, 13:30)

Yeah, i just called it Edge case. :P

My choice ;)

The actual reason to mention it as edge case was, that i forgot to mention it earlier.

5★ taran_1407 (2 days ago)

@eugalt - The fact here is, the pattern followed by that case is different which cannot be handled by current
algorithm. Plus, how many people were able to catch the case at first try? Not 20% I am sure :)

5★ vijju123 ♦ (2 days ago)

1 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...

You are not logged in. Please login at www.codechef.com to post your questions! ×
13 Answers:

I think the 3rd problem can be solved using dp.

2 We'll take the two cases(a1<a2>a3.. and a1>a2<a3..) separately.

Let dp[i][j] denotes the minimum swap needed to make array great for indices 1 to i and N-i+1 to N
with j denoting if this element is swapped or not.

We start from left and move to center.

We have following recurrence: Val1=dp[i-1][0],val2=dp[i-1][1] dp[i][0]=min(val1,val2) here first u


should check if these satisfy the relations(i mean lets say if i-1 is not swapped then check a[i-1],a[i]
and a[N-(i-1)+1],a[N-i+1]) if they dont accordingly set val1 Or val2 to inf.

Similarly, Dp[i][1]=1+min(dp[i-1][0],dp[i-1][1])

My ac solution(though implemented now and not in contest):

https://www.codechef.com/viewsolution/17125237

Edit:This solution should fail since i didnt consider the case for -1 when n>=4 and n is even and
middle 2 elements are same

(Lucky)

link edited 22 Jan, 22:06 answered 22 Jan, 02:35


5★ vivek_1998299
[ 404 ]●5
accept rate: 15%

Didn't get that.


1
2★ harrypotter0 (22 Jan, 11:39)

@vivek_1998299 could you explain that in a bit simpler way ....for noobs like me .

2★ harrypotter0 (22 Jan, 21:39)

Ohk so lets say that elements from 1 to i-1 and its opposite side n-i+1 to N are arranged in an order .So lets say
N=6 ,i=2 so we consider that a[1]<a[2] and="" a[5]="">a[6] and lets say that we know the minimum swaps
required to do this arrangement ,then we can calculate it for i=3.

So dp[i][2] indicates the minimum number of swaps required to make array great for indices 1 to i and its
opposite side ,dp[i][0] denotes minimum value if i and N-i+1 indices are not swapped ,similarly dp[i][1] denotes
minimum value if i and N-i+1 are swapped.

5★ vivek_1998299 (22 Jan, 21:51)

So i want to calculate dp[3][0] ,i can use dp[2][0],dp[2][1] as since i know that now values upto 2 are arranged
1
,i just have to check for 1 pair of element(3,4).Now i need to check if i can use dp[2][0],so just see if a[2]>a[3]
and a[4]>a[5],if it is then i can use dp[2][0],similaarly do for dp[2]1

Similarly we calculate dp[3][1] but now we swap a[3],a[4] and then check the same things,(we do 1+min(...) as
this swap contributes thaat 1)

5★ vivek_1998299 (22 Jan, 22:01)

Got it bro thanks :)

2★ harrypotter0 (2 days ago)

For the problem code SURVIVE 8 out of 10 randomly selected accepted AC's gave an output of 9 while
the correct output should be -1:
11
1
9 8 10

yet again,shows negligence in making the testcases!

link answered 22 Jan, 01:11


4★ preda2or
[ 144 ]●4
accept rate: 0%

Please post it at the announcement thread- which is the official thread for feedback. It will be seen by problem
setting panel there.

5★ vijju123 ♦ (22 Jan, 01:15)

your solution fails too @vijju123 :D


1
4★ preda2or (22 Jan, 02:10)

Lmao yeah XD. If anything, my solutions always find something in the cook off :3 . The most hilarious was, when
my brute force got accepted in p3 XD (I think @kingofnumber was the setter of that one)

5★ vijju123 ♦ (22 Jan, 02:14)

more than 75% solutions of SURVIVIE would have failed if only the corner cases were designed properly.

4★ preda2or (22 Jan, 02:24)

Yup, I did expected a corner case somewhere on lines of not buying on sunday giving -1. But I think those cases
also have N<K or some other condition which made them weaker- and hence lack of an individual case with
this feature alone.

BTW, do discuss that in feedback thread, the setter will value your insight surely.

5★ vijju123 ♦ (22 Jan, 02:26)

Here is the worst solution for 1st sum which i did.

3 Dp[i][j][k] denotes the minimum number of boxes taken upto ith day such that u have a balance of
j,and k denotes if on that day a box is taken.

Dp[i][j][0]=min(dp[i-1][j+k][0],dp[i-1][j+k][1])

Dp[i][j][1]=min(dp[i-1][j-(n-k)][0],dp[i-1][j-(n-k)][1]) + 1

2 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...

You are not logged in. Please login at www.codechef.com to post your questions! ×
Really good solution @taran_1407.

link edited 22 Jan, 01:45 answered 22 Jan, 01:42


5★ vivek_1998299
[ 404 ]●5
accept rate: 15%

#DP_Intensifies

5★ vijju123 ♦ (22 Jan, 01:49)

DP is in the air...

Nice solution @vivek_1998299

5★ taran_1407 (22 Jan, 01:54)

I think it was an overkill here btw thanks.

5★ vivek_1998299 (22 Jan, 02:06)

You just made 1718 people feel like genius with that xD
2
6★ abdullah768 (22 Jan, 23:23)

Yeah i was wondering how could so many people solve so fast


1
5★ vivek_1998299 (22 Jan, 23:32)

showing 5 of 6 show all

My both Solutions got AC in one go.

2 Problem: SURVIVE

My short solution in PYTHON-3.5:Mine is O(n) could be done in O(1) also.

te=int(input())
for _ in range(te):
n,k,s=input().split()
n,k,s=int(n),int(k),int(s)
capacity=k*s
availablity=n*s
availablity-=n*(s//7)
maans=0
if(capacity<=availablity):
answer=0
counter=0
for i in range(s):
counter+=1
counter%=7
if(counter!=0):
maans+=1
answer+=n
if(answer>=capacity):
break
print(maans)
else:
print("-1")

LOGIC:-
Maxmimum choclates he needs is ks which is capacity. And the availablity of chocolates is
ns-(n*(s//7)) as on Sunday shop is closed and he can't buy any chocolates. So if capacity >
availablity print("-1") as it is not possible to purchase. Else Run a loop to find the number of
days and dont forget not to calculate sundays and add n chocolates only on non sundays.

Problem: MULTHREE

My Short solution in PYTHON 3.5:-Time Complexity O(1)

test=int(input())
for y in range(test):
k,dig0,dig1=input().split()
k,dig0,dig1=int(k),int(dig0),int(dig1)
sum1=dig0+dig1
for i in range(3,k+2):
y=sum1%10
sum1+=y
if(y==2):
ter1=((k-i)//4)
ter1*=20
sum1+=ter1
ter=k-i
if(ter%4==3):
sum1+=18
elif(ter%4==2):
sum1+=12
elif(ter%4==1):
sum1+=4
break
if(y==0):
break
if(sum1%3==0):
print("YES")
else:
print("NO")

LOGIC:-

3 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...

You are not logged in. Please login at www.codechef.com to post your questions! ×
finding digits(sum%10); if digit is 0 then break as sum will remain same even after u do any
operation so break. Else break when u find a two as 2,4,8,6 block will keep on repeating.
therefore add ((k-i)//4)(2+4+6+8) to sum ((k-i)//4) is floor division plus remaining 4 12 or 18
depending on whether 4 or 4,8 or 4,8,6 is remaining. This block's size is got by (k-i)%4.Then
at last check whether sum is divisible by 3.

Essentially almost same as solution of @taran_1407

Like,Comment, Follow and award reputation points if u like. For any other queries or doubts
contact me.

For Codes as files to download visit:- THIS PAGE

link wikified 22 Jan, 14:58 answered 22 Jan, 09:46


This answer is marked "community wiki". 4★ anirudhjarvis
[ 11 ]●1
accept rate: 0%

Lovely editorial for 2nd problem. Even I followed the same idea- the solution was easy (had I just been
more careful and not done silly errors).
1
While implementing p2 tho, I did quite LOTS of silly errors-

1) Not updating K while calculating third digit. (i.e. After we check for first 2 digits, we should do K-2
as 2 digits are attached to the K digit num- I missed that).

2) In data type. At some point I used int (I think for sum)- should had avoided that since K is out of its
range, so generated sum will also be.

3) Be careful of any case when sum of digits becomes 5 ,which can lead to situation where 0 is
appended. Got TLE cause of this T_T.

When will the editorial for 3rd problem be there @taran_1407? That was some hell of casing (if-else,
case checks etc.)

link answered 22 Jan, 01:13


5★ vijju123 ♦
[ 11.8k ]●1●3●19
accept rate: 18%

I wasted two hours on third problem messing with if else blocks. and yes, if i manage to solve it soon (by
tomorrow i mean) i will write editorial.

Glad you liked it!!

5★ taran_1407 (22 Jan, 01:20)

Same here buddy. My entire time whooshed away in those if else. BTW, check out this solution, you might like
it-

http://codeforces.com/blog/entry/57259?#comment-409134

5★ vijju123 ♦ (22 Jan, 01:24)

Very much liked it @vijjju123 !! Just loving it...

A 200-line solution shoved under the nose of an innocent boy. :P

5★ taran_1407 (22 Jan, 01:29)

For the first problem, I used binary search on answer and for k no of days, we can use initial k days for
buying one box of chocolates.
1
Link to my solution.

link answered 22 Jan, 01:31


4★ arpn
[ 442 ]●3●16
accept rate: 29%

Though your solution is nice one, it have a worse Time complexity of O(Slog(7*S)) while above mentioned
2
solution has time complexity of O(S).

You can improve your check function to work in O(1) by a simple change, and even shorter code.

5★ taran_1407 (22 Jan, 01:38)

I think binary search is an overkill here. But well, Binary search is <3 .
1
5★ vijju123 ♦ (22 Jan, 01:40)

Initially I waited for few mins before coding and then I saw multiple WAs,so I thought O(S) might be missing
some corner cases. I was sure about BS solution,so implemented it :p

4★ arpn (22 Jan, 01:42)

Lol, I was hunting for cakewalk problem in first few min my order was-
1
>Opened p4. ".....He asked to find the matrix? DEFINITELY NOT CAKEWALK"
>p5 has "graph" in it. Lol graph no cakewalk.
>Great array...hmmm. Might be. (2 min after reading the statement and thinking "Noo....please this be
p4.....XD
>Multhree "YEAH This can be cakewalk!!" (Submitted a soln- got WA- "Hmm...Not cakewalk I guess lol")
>And finally chocoland lol.

5★ vijju123 ♦ (22 Jan, 01:45)

Happened with me too.

5★ taran_1407 (22 Jan, 01:52)

The first problem can be solved in O(1) time complexity per test case, here is my code in cpp

1
#include <bits/stdc++.h>
using namespace std;
int main(){

4 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...

You are not logged in. Please login at www.codechef.com to post your questions! ×
int t;
cin >> t;
while(t--){
int n,k,s;
cin>>n>>k>>s;
int sundays = s/7;
int TOTAL = k*s;
int buy = n*(s-sundays);
if(buy < TOTAL){
cout << "-1" << endl;
}else{
cout << ceil(float(TOTAL)/n) << endl;
}
}
return 0;
}

The logic: If total number of chocolates needed for S days is MORE that the number of chocolates
possible to buy in that (S-number of Sundays) days then it isn't possible to survive, else it is just
ceil(float(TOTAL Needed chocolates)/(maximum number of chocolates possible to buy per day i.e. N ))
.. ceil() function is used as integer division will exclude some days at the end. i.e. a box of chocolates
should be bought at the day number of chocolates left is less than K.

link edited 22 Jan, 20:50 answered 22 Jan, 15:08


4★ baniksudipto
[ 11 ]●2
accept rate: 0%

It fails on the corner case of "9 8 10" where it should print -1. Lucky that cases were weak :p

5★ vijju123 ♦ (22 Jan, 21:01)

Including you @vijju123 :P

5★ taran_1407 (22 Jan, 23:16)

Same kind of observation for 2nd question. Instead of 2486, I took 8624. Link to solution : Solution .

0 link answered 22 Jan, 00:48


4★ rohit_0801
[ 300 ]●9
accept rate: 10%

Doesn't change the solution though!!

5★ taran_1407 (22 Jan, 00:51)

Yupp.The solution remains same.

4★ rohit_0801 (22 Jan, 00:53)

Please help me in finding the error in the first question https://www.codechef.com/viewsolution


/17115932
0
link answered 22 Jan, 00:54
2★ mohit01
[1]
accept rate: 0%

Buy everyday, because if you don't buy everyday, it may cause problem on coming Sunday. Take care to exclude
X boxed later only if you can survive.

Second thing, why diff is initialized to N?? It should be 0.

5★ taran_1407 (22 Jan, 00:58)

for e.g if i take the test case 13 10 8 then on the first coming sunday the chocolates we would be having are
less than k. So the answer should be -1. But the correct answer is 7. What am i missing? coz for this test case
my code is giving output as -1

2★ mohit01 (22 Jan, 01:10)

Here's the simulation of first 7 days for your TC. Set dif = 0, count = 0,
2
Day 1: diff = diff+10-8 = 2, count = 1

Day 2: diff = 2+10-8 = 4, count = 2

Day 3: diff = 4+10-8 = 6, count = 3

Day 4: diff = 6+10-8 = 8, count = 4

Day 5: diff = 8-8 = 0, count = 4 Your mistake, as you didn't buy this day and later on concluded
insufficiency of sweets.

Day 5: diff= 8+10-8 = 10, count = 5

Day 6: diff = 10+10-8 = 12, count = 6

Day 7: diff = 12-8 = 4, count = 6.

Hope this makes clear.

And to all who read this comment, if stuck in a simulation, use print statements to do all this. ;)

5★ taran_1407 (22 Jan, 01:26)

thanks alot @taran_1407


1
2★ mohit01 (22 Jan, 01:41)

May I know where the announcement thread is located @vijju123 ?

0 link answered 22 Jan, 02:10


4★ gomu
[ 136 ]●3

5 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...

You are not logged in. Please login at www.codechef.com to post your questions! ×
Invitation threads, by default, are feedback threads as well. :)

5★ vijju123 ♦ (22 Jan, 02:15)

#include <bits/stdc++.h>
0 using namespace std;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
long long k;
int x,y,p=0;
int b[]={2,4,8,6};
int c[4];
cin>>k>>x>>y;
int z=2*(x+y);
int s=z%10;
if(k>2){
if(s==0){cout<<"NO\n";}
else{
int a=(((k-3)/4)%3)*2;
//cout<<"a="<<a;
for(int i=0;i<4;i++){c[i]=b[i];}
int r=1;
while(s!=c[0]){
for(int j=0;j<4;j++){
c[(j+r)%4]=b[j];
}
r++;
//for(int i=0;i<4;i++){cout<<c[i]<<" ";} cout<<"\n";
}
for(int i=1;i<=(k-3)%4;i++){
p=p+c[i-1];
}
//cout<<"p="<<p;
z=z+a+p;
//cout<<"z="<<z;
if(z%3==0){cout<<"YES\n";}
else{cout<<"NO\n";}
}
}
else{
if((x+y)%3==0){cout<<"YES\n";}
else{cout<<"NO\n";}
}
}
return 0;
}
what's wrong in my code to show a WA??

link edited 22 Jan, 02:43 answered 22 Jan, 02:22


5★ vijju123 ♦ 2★ buzz_95
[ 11.8k ]●1●3●19 [1]
accept rate: 0%

When will the editorial for problem MAGA be up ?

0 link answered 22 Jan, 23:02


4★ tihorsharma123
[ 332 ]●7
accept rate: 13%

I had been busy the whole day, so i didn't even solve the third problem, let alone writing editorial.

About official editorial, i don't know.

5★ taran_1407 (22 Jan, 23:15)

Ohh Sorry, I meant official editorial.

4★ tihorsharma123 (23 Jan, 00:32)

Do they plan to release the official editorial for the MAGA problem? -_-

0 link answered 24 Jan, 01:53


3★ skpro19
[0]
accept rate: 0%

We will soon provide the official editorial. So sorry for the delays :(
1
admin ♦♦ (24 Jan, 11:38)

6 of 7 27/01/18, 11:07 PM
Unofficial Editorials January Cook Off - CodeCh... https://discuss.codechef.com/questions/121790...

You are not logged in. Please login at www.codechef.com to post your questions! ×

[hide preview] community wiki:

Preview

Type the text


Privacy & Terms

Post Your Answer

About CodeChef About Directi CEO's Corner


CodeChef Campus Chapters CodeChef For Schools Contact Us

© 2009, Directi Group. All Rights Reserved.


Powered by OSQA

7 of 7 27/01/18, 11:07 PM

You might also like