CHEFSOCK - Editorial

Problem Link:

Practice
Contest

Author: Misha Chorniy
Tester: Pushkar Mishra
Editorialist: Praveen Dhinwa

Difficulty:

Cakewalk

Pre-Requisites:

Implementation, Basic maths.

Problem Statement

Chef has money rupees with him. He spends jacketCost rupees for buying a jacket. He buys as many socks as he can by the money left after purchasing the jacket. Cost of a sock is given by sockCost. Each day, he uses two socks. He never cleans his socks after using them. You have to find whether there will be a day in which Chef will have one sock remaining.

Quick Explanation

We just have to check whether \frac{(money - jacketCost)}{sockCost} is even or odd.

Explanation

Initial money that Chef have = money
We are guaranteed in the problem that jacketCost \leq money.
So the money left after buying a jacket (i.e. money - jacketCost) will be always non-negative.

Using the remaining money, Chef buys as many socks as we can, so he will buy S socks where S = \frac{(money - jacketCost)}{sockCost} where / denotes an integer division. By integer division, we mean floor function (e.g. \lfloor \frac{3}{2} \rfloor = 1 and \lfloor \frac{4}{2} \rfloor = 2).

Now, we need to check if Chef wear two socks daily, will there be a day when he will have to wear a single sock? This is equivalent to checking S is even or not.

For solving subtask 1, we can simply simulate the process of wearing two socks as follows:


int remS = S // remS denotes initial number of socks.
while (remS >= 2) {
  // wear two socks
  remS -= 2;
}
if (remS == 1) return "Unlucky Chef"
else return "Lucky Chef";

Time Complexity

O(S) or O(1) depending on the subtask.

Solution:

Setter’s solution can be found here
Tester’s solution can be found here

Please feel free to post comments if anything is not clear to you.

Problem links are wrong.

What’s wrong with my solution? I get WA for some of the cases but AC for others.

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

int main() {
	int j,s,m;
	cin >> j >> s >> m;
	m -= j;
	if(m % s != 0)
		cout << "Lucky Chef\n";
	else 
		cout << "Unlucky Chef\n";
	return 0;
}

include

include

include

int main()
{
long int jacketCost,sockCost,money,moneyLeft,socksObtained;
scanf("%ld%ld%ld",&jacketCost,&sockCost,&money);
moneyLeft=money-jacketCost;
//printf("%d",moneyLeft);
socksObtained=moneyLeft/sockCost;
//printf("%d",socksObtained);
if((socksObtained%2)==0)
{
printf(“Lucky Chef”);
}else
{
printf(“Unlucky Chef”);

}

}

include

include

include

int main()
{
long int jacketCost,sockCost,money,moneyLeft,socksObtained;
scanf("%ld%ld%ld",&jacketCost,&sockCost,&money);
moneyLeft=money-jacketCost;
//printf("%d",moneyLeft);
socksObtained=moneyLeft/sockCost;
//printf("%d",socksObtained);
if((socksObtained%2)==0)
{
printf(“Lucky Chef”);
}else
{
printf(“Unlucky Chef”);

}

}

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
long int jacketCost,sockCost,money,moneyLeft,socksObtained;
scanf("%ld%ld%ld",&jacketCost,&sockCost,&money);
moneyLeft=money-jacketCost;
//printf("%d",moneyLeft);
socksObtained=moneyLeft/sockCost;
//printf("%d",socksObtained);
if((socksObtained%2)==0)
{
printf(“Lucky Chef”);
}else
{
printf(“Unlucky Chef”);

}

}

@harsh1995: Please check now, they are corrected.

Please check and tell me why this works

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

and this doesn’t

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

I learnt about this way of inputting data from

And it works for every other problems but sometimes produces error

I have been googling this and am unable to find an satisfactory answer

Your help will be very much appreciated

Plzzzzzz help