https://atcoder.jp/contests/abc319/tasks/abc319_e
多くても1~8の最小公倍数しかパターンがないはずなので、最初にそのときの移動時間を計算しておけばよいです。
// Bus Stops #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } //////////////////// BusStop //////////////////// struct BusStop { P: i64, T: i64 } impl BusStop { fn next_time(&self, t0: i64) -> i64 { (t0 + self.P - 1) / self.P * self.P + self.T } fn read() -> BusStop { let v = read_vec(); let (P, T) = (v[0], v[1]); BusStop { P, T } } } //////////////////// process //////////////////// fn read_input() -> (i64, i64, Vec<BusStop>, i32) { let v: Vec<i64> = read_vec(); let (N, X, Y) = (v[0], v[1], v[2]); let BSs: Vec<BusStop> = (0..N-1).map(|_| BusStop::read()).collect(); let Q: i32 = read(); (X, Y, BSs, Q) } fn calc_travel_time(t0: i64, X: i64, Y: i64, BSs: &Vec<BusStop>) -> i64 { let mut t: i64 = t0 + X; for bs in BSs.iter() { t = bs.next_time(t) } t + Y - t0 } fn f(X: i64, Y: i64, BSs: Vec<BusStop>, Q: i32) { let T: Vec<i64> = (0..840).map(|t0| calc_travel_time(t0, X, Y, &BSs)). collect(); for _ in 0..Q { let q: i64 = read(); println!("{}", q + T[(q%840) as usize]) } } fn main() { let (X, Y, BSs, Q) = read_input(); f(X, Y, BSs, Q) }