[poj 3539] Elevator (同余类bfs)

Posted menteur-hxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[poj 3539] Elevator (同余类bfs)相关的知识,希望对你有一定的参考价值。

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)的主要内容,如果未能解决你的问题,请参考以下文章

[poj 3539] Elevator (同余类bfs)

「POJ3539」Elevator - 同余类最短路

[POJ] 3539 Elevator

POJ3539 Elevator

POJ - 3696 同余

POJ 2115C Looooops[一元线性同余方程]