跳到正文

题解

[ABC319E] Bus Stops 题解

FFTotoro约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年9月10日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 2 分钟

版本

  1. v1当前

因为公交车到达的时间为各个 PiP_i 的倍数,所以我们不用考虑具体的时间,只用考虑时间对各个 PiP_i 取模的值。可以发现,实际上只要考虑时间对 lcm⁡i=1NPi\operatorname{lcm}_{i=1}^NP_i 取模的值。

为方便处理,这里令 L=lcm⁡{1,2,3,4,5,6,7,8}=840L=\operatorname{lcm}\{1,2,3,4,5,6,7,8\}=840,先预处理出发时间为 0,1,…,8390,1,\ldots,839 的答案 r0,r1,…,r839r_0,r_1,\ldots,r_{839},对于询问的具体时间 xx,答案即为 rx mod 840+840⌊x840⌋r_{x\bmod 840}+840\left\lfloor\frac{x}{840}\right\rfloor。

放代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> pii;
int r[840];
int f(int n,int x){
  if(n%x)return (n/x+1)*x;
  else return n;
} // 找大于等于 n 且是 x 的倍数的最小整数
main(){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0);
  int n,x,y; cin>>n>>x>>y;
  vector<pii> a(n-1);
  for(auto &[p,t]:a)cin>>p>>t;
  for(int i=0;i<840;i++){
    r[i]=i+x;
    for(auto [p,t]:a)r[i]=f(r[i],p)+t;
    r[i]+=y;
  } // 模拟答案
  int q; cin>>q;
  while(q--){
    int b; cin>>b;
    cout<<r[b%840]+b/840*840<<'\n';
  } // 处理询问
  return 0;
}
原文
存档评论2 条抓取于