1. 程式人生 > >[poj 3539] Elevator (同余類bfs)

[poj 3539] Elevator (同余類bfs)

move higher 方法 最小 man acc emp HERE 最短路

Description

Edward works as an engineer for Non-trivial Elevators: Engineering, Research and Construction (NEERC). His new task is to design a brand new elevator for a skyscraper with h floors.

Edward has an idée fixe: he thinks that four buttons are enough to control the movement of the elevator. His last proposal suggests the following four buttons:

Move a floors up.
Move b floors up.
Move c floors up.
Return to the first floor.

Initially, the elevator is on the first floor. A passenger uses the first three buttons to reach the floor she needs. If a passenger tries to move a, b or c floors up and there is no such floor (she attempts to move higher than the h-th floor), the elevator doesn’t move.

To prove his plan worthy, Edward wants to know how many floors are actually accessible from the first floor via his elevator. Help him calculate this number.

Input

The first line of the input file contains one integer h — the height of the skyscraper (1 ≤ h ≤ 1018).

The second line contains three integers a, b and c — the parameters of the buttons (1 ≤ a, b, c ≤ 100 000).

Output

Output one integer number — the number of floors that are reachable from the first floor.

Sample Input

15
4 7 9

Sample Output

9

Source

Northeastern Europe 2007, Northern Subregion

題解

很神奇的構造方法。。
我們將所有樓層都對a取模,按余數分成a類
在每個分類中的樓層滿足這樣一個性質:
若有樓層x,y在同一類中且x<y那麽如果x可達,y必可達
之後由於我們只需處理b,c兩種連接方式即可

重點

我們每個同余類看成一個點(x)
枚舉x 在\(x\)\((x+b)%a、(x+c)%a\)之間連一條長度為b,c的邊
之後從1開始跑最短路 得到 d[x]
此時d[x] 表示如何用最快的方式達到%a余x的樓層
顯然,在這種情況下得到的d[x]還有一個含義便是能達到的%a余x的樓層的最小值
根據我們之前分析的同於類的性質可知滿足\(d[x]+a^k (d[x]+a^k<=h)\)的所有樓層均能被達到
那麽只需對所有同余類統計出的答案求和即可
code:(留坑)

[poj 3539] Elevator (同余類bfs)