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 ≤ 10^18) $ .
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
题目大意
一部电梯,最初在 $ 1 $ 层。
电梯有 $ 4 $ 个按键:上升 $ a $ 层,上升 $ b $ ,上升 $ c $,回到 $ 1 $ 层。
求从 $ 1 $ 层出发,能到达 $ 1~h $ 之间的那些楼层
题解
不妨设 $ a \le b \le c $ ,把楼层按照除以 $ a $ 的余数分成 $ 0,1,2,\dots,a-1 $ 这 $ a $ 个类。
每个类 $ x $ 看作一个点,
向 $ (x+b) \quad mod \quad a $ 和 $ (x+c) \quad mod \quad a $ 分别连长度为 $ b,c $ 的有向边。从 $ 1 $ 出发求单源最短路
同余类 $ x $ 中能够到达的楼层就是 $ d[x],d[x]+a,d[x]+2 \times a, \dots $
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define int long long
int dis[100010],h,a,b,c,ans;
bool vis[100010];
void bfs(){
memset(dis,0x3f,sizeof(dis)); queue<int>q;
dis[1%a]=1; q.push(1%a); vis[1%a]=1;
while(!q.empty()){
int u=q.front(),v; q.pop(); vis[u]=0;
if(dis[v=(u+b)%a]>dis[u]+b){
dis[v]=dis[u]+b;
if(!vis[v]) q.push(v);
}
if(dis[v=(u+c)%a]>dis[u]+c){
dis[v]=dis[u]+c;
if(!vis[v]) q.push(v);
}
}
}
signed main(){
scanf("%lld %lld %lld %lld",&h,&a,&b,&c);
if(a>b) swap(a,b); if(a>c) swap(a,c);
bfs();
for(int i=0;i<a;++i)
if(dis[i]<=h) ans+=(h-dis[i])/a+1;
printf("%lld",ans);
return 0;
}
/*
Problem: 3539
User: potremz
Memory: 1028K
Time: 360MS
Language: C++
Result: Accepted
*/
本文详细解析了一道经典的算法题目——电梯可达楼层问题。通过设计四个按钮(上升a层、上升b层、上升c层及返回第一层),探讨了如何从第一层出发,计算能够到达的楼层总数。文章提供了详细的题解思路,包括使用同余类进行分类,建立有向图,并运用广度优先搜索算法求解最短路径。
160

被折叠的 条评论
为什么被折叠?



