Jumping Machine
Time limit1sMemory limit512 MB
Given n springs with jump lengths, the machine at (0,0) uses each spring once in any order, moving l up or l right; count the grid cells it can ever land on or pass over.
- Level
Hard8 of 10
- Topics
- Combinatorics, Math, Number theory, Dynamic programming
- Solved
- No attempts yet
Problem
A young inventor has built a new jumping machine. To test it, he brought it to a testing polygon. The polygon is an infinite square grid.
Initially the machine is in cell . The machine has springs, and the -th spring has force and lets the machine jump cells up or cells to the right. So this spring lets the machine go from cell either to cell or to cell . After a jump the spring is thrown back and cannot be reused. The machine can use the springs in any order.
During the tests, the cells the machine flies over get stained with machine oil. To avoid cleaning the grid afterwards, the inventor decided to put a protective mat on every cell the machine could possibly fly over.
Now the inventor wonders how many protective mats he needs to bring to the test.
Input
The first line of input contains , the number of springs the machine has (). The second line contains integers , the spring forces (; ).
Output
Output a single integer: the number of mats the inventor needs to bring.
Hint
All the cells that can get dirty when the machine jumps in the example test are colored orange in the figure below.

Cells that the machine can stain.