SPCANDY - Editorial

PROBLEM LINKS

Practice
Contest

DIFFICULTY

CAKEWALK

PREREQUISITES

Simple Math

PROBLEM

You wish to equally distribute N candies among M people, using the following algorithm

nCandies = 0
candies-with-each-person = 0

while nCandies ≥ M
	++candies-with-each-person
	nCandies = nCandies - M

Print the values of candies-with-each-person and nCandies at the end of the algorithm.

EXPLANATION

The given algorithm is an ad-hoc simultation of the division operation.

At the end of the algorithm, it is clear that

candies-with-each-person =
	quotient of N divided by M

and

nCandies =
	remainder of N divided by M

CODING COMMENTARY

There were two common pitfalls. The hint to both of them lay discreetly within the Constraints section.

M could be equal to 0

A lot of submissions received

Runtime Error: Floating Point Exception

verdict because of this pitfall.

The exact error of course is the attempted division by 0. Handling it as a special case is one solution.

N,M could be equal to 8,589,934,591

This typically led to a Wrong Answer verdict. The range of integers handled by the 32-bit signed integer datatype is typically

[-2147483648 2147483647]

One had to use 64-bit integer datatype to solve this problem. This means

  • long long in C/C++ - as CodeChef uses the GNU C++ Compiler
  • long in Java

PSEUDO CODE

Given: N, M

if M == 0
	return {
		candies-with-each-person = 0,
		nCandies = N
	}
else
	return {
		candies-with-each-person = N / M,
		nCandies = N % M
	}

Here, we assume that

  • N and M are 64-bit integers
  • % is the modulo operation

SETTER’S SOLUTION

Can be found here.

TESTER’S SOLUTION

Can be found here.

6 Likes

Whats wrong with this ?
http://www.codechef.com/viewsolution/2523298

@ajit_a Why is it scanf("%lld%d",&K,&N); and not scanf("%lld%lld",&K,&N);

N also needs to be long long.

Problem statement, replacing K by M:

while she has more than M candies

Editorial:

while nCandies ≥ M

‘More than’ means >, not >=

Let’s assume the problem statement was wrong, and it should have said ‘more than’.

Problem statement:

Your job is to tell how many candies will each student and the teacher
receive after the splitting is performed.

Editorial:

Handling [M=0] as a special case is one solution.

If M=0, then the algorithm provided in both the problem statement and editorial never terminates; it is undefined what happens after an infinite loop, not a special case.

Actually, the M=0 case here was to be interpreted as a special case, since the teacher can’t divide the candies by any students, since there are 0 students. On this case, the teacher receives all N candies… I’m sad this problem failed on being as trivial as possible due to this sort of complications… About the usage of long long it was really intended :slight_smile:

hi codechef ,

my below code is not working but same code is one user post that is working

what problem, in my code

using System;

namespace SplittingCandies
{

class Program
{

static void Main(string[] args)
{

int t = int.Parse(Console.ReadLine());
   
 for (int i = 0; i < t; i++)
        {
        
string[] tc = Console.ReadLine().Split(' ');
        
if (ulong.Parse(tc[1]) == 0)
         
   Console.WriteLine(tc[0] + " " + tc[1]);

else

   Console.WriteLine((ulong.Parse(tc[0]) / ulong.Parse(tc[1])) + " " + (ulong.Parse(tc[0]) % ulong.Parse(tc[1])));
      }
     Console.ReadLine();
    }
}

}

below code working this is one of user code

using System;

namespace SplittingCandies
{

class Program
{

static void Main(string[] args)
{

int cases = int.Parse(Console.ReadLine());

for (int i = 0; i < cases; i++)
{

string[] input = Console.ReadLine().Split(’ ');

if(ulong.Parse(input[1]) == 0)

Console.WriteLine(input[1] + " " + input[0]);

else

Console.WriteLine((ulong.Parse(input[0]) / ulong.Parse(input[1])) + " " + (ulong.Parse(input[0]) % ulong.Parse(input[1])));
}

Console.ReadLine();
}
}
}

0 case was somehow hidden very well

2 Likes

note: just pay attention to k, whether if it is 0 or not

note: just pay attention to k, whether if it is 0 or not

Why do we need to subtract candies at each stage and find the modulus and quotient at each stage?
Can’t we do it directly and in one step? Divide N by K, the quotient gives the candies each student will receive and the remainder the candies left with the teacher

@workforce , u are write but we have to take care when k=0 or n =0

why unsigned long cannot be used or simply long … as they are also of 8 byte ??? why only long long ???

@eabby007 unsigned long int range is from 0 to 4,294,967,295 (4 bytes!) wheras long long int is from -2^63 to 2^63-1 (8 bytes)