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.

@harsh1995: Please check now, they are corrected.

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”);

}

}