Elevator
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 1572 Accepted: 424
Case Time Limit: 2000MS
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
思路
一个同余类最短路,在 \(a\),\(b\),\(c\) 中较小的值(不妨设是 \(a\) )为同余系,在 \(0\) ~ \(a-1\) 中每个 \(i\) 向点 \((i+b)\)%\(a\)、\((i+c)\)%\(a\) 连一条边权分别为 \(b\)、\(c\) 的边,跑一遍最短路,所得的 \(dis\) 数组求得的就是 \(b\) 和 \(c\) 在 \(mod\) \(a\) 同余系中的最低可达层,注意起始位置就是从第 \(1\) 层开始的,所以 \(dis[1\)%\(a]\) 的最低可达层要先预处理成 \(1\) ,统计答案见代码即可。
代码
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<iostream>
#define rg register
#define int long long
using namespace std;
inline int read(){
rg int f = 0, x = 0;
rg char ch = getchar();
while(!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while( isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return f? -x: x;
}
const int N = 100010;
int h, a, b, c, head[N], tot, dis[N], ans;
bool vis[N];
struct edge{
int nxt, to, w;
}e[N<<1];
inline void add(rg int u, rg int v, rg int w){
e[++tot].nxt = head[u];
e[tot].to = v;
e[tot].w = w;
head[u] = tot;
}
inline void spfa(){
memset(dis,0x7f7f7f7f,sizeof(dis));
deque<int> q;
dis[1%a] = 1;
vis[1%a] = 1;
q.push_back(1%a);
while(!q.empty()){
int u = q.front();
q.pop_front();
vis[u] = false;
for(rg int i = head[u]; i; i = e[i].nxt){
int v = e[i].to;
if(dis[v] > dis[u] + e[i].w){
dis[v] = dis[u] + e[i].w;
if(!vis[v]){
vis[v] = true;
if(q.empty()||dis[v] > dis[q.front()]) q.push_back(v);
else q.push_front(v);
}
}
}
}
}
signed main(){
h = read(), a = read(), b = read(), c = read();
if(a > b) swap(a, b); //从较小的做同余系可以跑的更快
if(a > c) swap(a, c);
for(rg int i = 0; i < a; ++i){
add(i, (i + b) % a, b);
add(i, (i + c) % a, c);
}
spfa();
for(rg int i = 0; i < a; ++i)
if(h >= dis[i]) //注意要判断 dis 值可能为 inf 的情况
ans += (h - dis[i]) / a + 1;
printf("%lld", ans);
return 0;
}
后话
这里提一下NOIP2017D1T1小凯的疑惑,这道题在推公式的时候不仅可以用 \(exgcd\) ,也可以用同余类最短路来推,推出来的结果其实就是最大最低可达层的层数 \(-1\) ,因为这之上的都可以到达。
原文地址:https://www.cnblogs.com/horrigue/p/9703603.html