Editorial for VPEX P3 - Coding Club
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1
The values are so small, it is enough to simply simulate the rectangle, changing its direction every time it bounces off a wall.
When the box has the same position and is travelling in the same direction as it started in, the animation will begin to loop.
Time Complexity: about , depending on implementation
Subtask 2
Instead of an logo bouncing around a screen, you can focus on the bottom left point bouncing around a box. This can then be imagined as a point travelling in a line through an infinite grid of cells.
When the point travels through an even number of cells vertically, it will have bounced an even number of times vertically. Same thing for the horizontal direction. When this has happened, the box will be travelling in its starting direction.
The box also needs to have travelled to its original position, so the vertical and horizontal displacement must be divisible by the cell height and width.
If is the horizontal displacement, and is the vertical displacement, you have and . Write this as and .
From the slope, also .
Then and because it's the first time this happens. So:
The distance is , and travelling at one unit/second, it's also the time taken.
You can also see that the output will never be -1
.
Remember the special cases where or , where the point only travels in one direction.
Also, remember to store your output as a double to avoid integer overflow.
Time Complexity:
Comments