Geometric Balance

시간 제한3초메모리 제한2048 MB

요약
거북이의 이동, 회전, 그리기 명령이 최대 50000개 주어질 때, 번역 후 그림을 자기 자신에 겹치게 하는 가장 작은 회전 각도를 구한다.
난이도

어려움10점 중 8점

유형
기하, 시뮬레이션, 해시맵, 수학
정답자
아직 제출이 없습니다

문제

Peter's little brother Ivan likes to play with a turtle. The turtle is a special toy that lives on the plane and can execute three commands:

  • Rotate aa degrees counterclockwise.
  • Draw dd units in the direction it is facing while dispensing ink. No segment of the plane will be covered by ink more than once.
  • Move dd units in the direction it is facing without drawing.

Ivan just learned about the compass, so he will only rotate his turtle so it faces one of eight cardinal or ordinal directions (angles aa in rotate commands are always divisible by 45). Also, he will perform at least one draw command.

Peter has noted all the commands Ivan has given to his turtle. He thinks that the image drawn by the turtle is adorable. Now Peter wonders about the smallest positive angle bb such that he can perform the following operations: move the turtle to a point of his choosing, rotate it by bb degrees, and execute all the commands in the same order. These operations should produce the same image as the original one. Can you help Peter?

Note, two images are considered the same if the sets of points covered by ink on the plane are the same in both of the images.

출력

The first line of the input contains a single integer n(1≤n≤50000)n(1 \le n \le 50000) --- the number of commands Ivan has given.

The next nn lines contain commands. Each command is one of:

  • "rotate aa" (45≤a≤36045 \le a \le 360) where aa is divisible by 4545;
  • "draw dd" (1≤d≤1091 \le d \le 10^9);
  • "move dd" (1≤d≤1091 \le d \le 10^9).

At least one and at most 2000 of the commands are draw. It is guaranteed that no segment of the plane will be covered by ink more than once.

제한

Output a single number, the answer to the question. The answer always exists.

예제3

  1. 예제 1

    입력
    1
    draw 10
    
    예상 출력
    180
    
  2. 예제 2

    입력
    7
    draw 1
    rotate 90
    draw 1
    rotate 90
    draw 1
    rotate 90
    draw 1
    
    예상 출력
    90
    
  3. 예제 3

    입력
    3
    draw 1
    move 1
    draw 2
    
    예상 출력
    360