ENG  RUSTimus Online Judge
Online Judge
Online contests
About Online Judge
Frequently asked questions
Site news
Problem set
Submit solution
Judge status
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
back to board

Discussion of Problem 1214. Strange Procedure

Math explanation
Posted by mberdyshev 1 Sep 2016 02:59
We are given positive x and y. Let's go in the first loop.
Let's do some method refactoring for better understanding:
y0 = x*x+y;
x0 = x*x+y0;
y1 = sqrt(x0+(y0/labs(y0))*(-labs(y0)));
for (j = 1; j <= 2*y1; j++)
    x0 = x0-y1;
x = x0;
y = y1;
Let's go through the lines:
y0 = x*x + y
x0 = x*x + y0 = 2*x*x + y
As y0 > 0 (x, y are positive) => y0/labs(y0) = 1
So y1 = sqrt(x0+(y0/labs(y0))*(-labs(y0))) = sqrt(x0-labs(y0)) = sqrt(2*x*x + y - (x*x + y)) = sqrt(x*x) = x
Next 2 lines equals to this:
x0 = x0 - 2*y1*y1 = 2*x*x + y - 2*x*x = y
So, x=y and y=x. x and y are swapped. After that you need to count amount of swaps and print appropriate answer
Re: Math explanation
Posted by D4nick 27 Mar 2020 22:02
Thank you very much.