DYNAMO : CodeChef January 2020 Editorial

Author: amit_dwivedi
Prerequisite: Basic Math
Difficulty: Easy
Editorialist: amosaidoo2

Basically, given a number A, you are to make a prediction S based on A such that four other numbers in addition to A sum up to S. This is a variant of a popular game math problem. I will give an illustration below.
A = 15, S = 213, B = 9, C = 90 , D = 15, E = 84. What just happened? When A was given, I subtracted 2 from A and appended 2 in front of A. That is my predicted sum. Now I make sure that anytime a value is given I make a prediction such that the sum is 99. So C = 99 - B and E = 99 - D.

Now to our problem, the only different thing is that we only use N perfect numbers. That is a number cant be zero. Meaning, we have to take care of any zero that may occur. If Cheffa tells me 9, 9 - 9 is 0 meaning that I will get WA. What is I added 1 and tell Cheffa 9 - 9 + 1, then I will be correct. This means I will add 1 to C and add 1 to E. This contributes 2 to the total sum, therefore I will add 2 to the total sum S. The algorithm below illustrates this idea:

  1. Get N from Cheffa
  2. Get A from Cheffa
  3. Predict S = (10 ^ N) * 2 + A //This time I don’t subtract 2
  4. Get B from Cheffa
  5. Predict C = (10 ^ N) - B
  6. Get D from Cheffa
  7. Predict E = (10 ^ N) - D

My Solution: amosaidoo2

Thank you.

Can anyone share his c++ code …i want to know the flushing method

inorder to flush in c++ all u have to do is:

cout << “xyz” << endl;

the endl statement flushes

1 Like

You may go through this if you want to explicitly flush your characters without using an endl. :slightly_smiling_face:

1 Like

What is the difference b/w endl and explicit flushing ? Which one is more effective

Here is something useful.

endl when written to an output stream has the effect of writing a newline to the output and flushing the buffer associated with that device. By flushing the buffer, we ensure that the user will see the output written to the stream immediately.

If you don’t want a newline but only want to flush your buffer, you use flush but if you want to do both, use endl and if you just want a newline, simply use '\n'.

Here's some code
#include <iostream>

int main(void)
{
	std::cout << "This is being followed by a flush" << std::flush;
	std::cout << "This is being followed by an endl" << std::endl;
	std::cout << "The end\n";
}
Output
This is being followed by a flushThis is being followed by an endl
The end

Hope you find this helpful. :slightly_smiling_face:

2 Likes

From where have you studied this maths techique as i am new to coding can you help me ???
thank you in advance

1 Like

http://burningmath.blogspot.com/2014/03/magic-addition-trick-predicting-sum-of.html
http://www.dr-mikes-math-games-for-kids.com/addition-trick.html