Skip to content

Instantly share code, notes, and snippets.

@Nov05
Last active June 22, 2023 03:04
Show Gist options
  • Save Nov05/afca93559bd5f9310ed50d649821cd22 to your computer and use it in GitHub Desktop.
Save Nov05/afca93559bd5f9310ed50d649821cd22 to your computer and use it in GitHub Desktop.
20230621_Codeforces Problem 1552F Telepanting_Solution 2

https://www.youtube.com/watch?v=_DaTsI42Wvo&t=653s
@furkanunsal5814, 10 months ago (edited)

I solved the problem much differently and pure mathematically before watching the video. it is very hard to explain a solution in text form but the main idea was to see the portals as binary numbers. I split the problem into two. evaluating how many times the player has entered a portal (to teleport not passover) and then calculating the total distance traveled. The second problem's solution was easy I just had to calculate the difference in position between the portal and the teleportation target to calculate the "cost" of the teleportation. the total length of the line plus all the costs were equal to the total distance traveled. now for the solution to the first part, look at the first example with 4 portals (red, orange, yellow, green). in the solution of this part ignore all the distances. if you look at the first red portal it teleports the player right behind itself and is thus equivalent to a binary number with one digit. orange also is a one digit number but yellow perfectly encapsulates orange but no other portal so they collectively create two digit binary number. green is encapsulating the entire one digit and two digit numbers. green is not symbolized as 3 digit but (1+2)+1 digits. plus operation is not summation in this notation but symbolizes that those two numbers have to be calculated independently.
so collectively the table for the puzzle is 1+2+(1+2+1).

we have to take the initial positions of the portals into account. the first one digit portal is closed so it has the value of 1. notice 1 is the maximum value a one digit binary number can hold so the next stop over it will pass it.
orange and yellow collectively two digit number seems to have the value (0x01=1) but you can observe they act as inverted. so actually they have the value (0x10=2) of 2.
and lastly green was symbolizing the entire copy of the puzzle plus itself but when the player passes it, all the portals will be open. so the value for green is (0+0+0)

binary digit table = 1+2+(1+2+1)
initial value table = 1+2+(0+0+0)

we are close to be able to calculate total teleportation count. just remember that one digit binary numbers can hold maximum of 1 and leak for the value of 2. similarly two digit numbers can hold the maximum of 3 and leaks for the value of 4.

binary digit table = 1+2+(1+2+1) 
initial value table = 1+2+(0+0+0)
max value table = 1+3+(1+3+1)
difference table = 0+1+(1+3+1)

when we sum the difference table 1+1+3+1=6 you can see that player will teleport 6 times.

conveying the idea behind these tables isn't that easy in text form so if they are not clear try to read it again and calculate them by yourself this is the best that I can do.

lastls we have to calculate the costs. as I said calculating the cost for a portal is easy we have to calculate the distance between the portal and the target but when we unite two portals into a multi digit number since the two portals creating that multi digit (in this example two) number can have different costs we have to symbolize them independently. for this example in the case of orange and yellow portals, their costs are 1 and 3 so we will notate them as (1+3) for the 2 digit number. this says that if the player enters the first digit the cost is 1 and if it enters the second digit the cost is 3. we need to somehow evaluate how many times which digit will flip while counting in binary. I will calculate that for the general case later but for two digit binary (00 10 01 11) the first digit will flip every time and the second digit will flip every two times. collectively first 4 times and second 2 times.

let me write the tables again this time with the cost table too.

cost table = 1 + (1+3) + 1 + (1+3) + 7
binary digit table = 1 + 2 + (1+2+1)
initial value table = 1 + 2 + (0+0+0)
difference table = 0 +  1 + (1+3+1)
total cost table = 0 + (1+0) + 1 + (1+3+1) + 7 = 14
total distance travelled = cost + distance = 14 + 9 = 23

the reason for the cost for the second two digit number to be not (1+3) but (1+3+1) is because while counting (00 10 01 11) first digit is flipped from 0 to 1 twice and second digit once.

thus when the binary digit table is constructed total distance traveled can be evaluated mathematically with near zero computational cost.
I have spent around 2 hours for the solution and an additional 2 hours watching the video and writing this comment. I'm very happy if you have read it to here and I hope you like the solution. have a nice day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment