思路是预处理出八个余数的值,然后再 O(1) 回复询问。
预处理的常数为 k=2×3×2×2×5×7×2=1680。
那么复杂度就是 O(kn+q),大概 2e8 左右,但是卡不过去。
代码如下:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAXN 100001
int n, x, y, Q, q, now;
int p[MAXN], t[MAXN];
int a[9];
int ans[2][3][4][5][6][7][8];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> x >> y;
for (int i(1); i<n; ++i) cin >> p[i] >> t[i];
for (a[2]=0; a[2]<2; ++a[2])
for (a[3]=0; a[3]<3; ++a[3])
for (a[4]=a[2]; a[4]<4; a[4]+=2){
for (a[5]=0; a[5]<5; ++a[5])
for (a[6]=a[2]; a[6]<6; a[6]+=2){
for (a[7]=0; a[7]<7; ++a[7])
for (a[8]=a[4]; a[8]<8; a[8]+=2){
now = x;
for (int i(1); i<n; ++i){
now += (p[i]-((now+a[p[i]]) % p[i])) % p[i];
now += t[i];
}
ans[a[2]][a[3]][a[4]][a[5]][a[6]][a[7]][a[8]] = now + y;
}
}
}
cin >> Q;
while (Q--){
cin >> q;
cout << q+ans[q&1][q%3][q&3][q%5][q%6][q%7][q&7] << '\n';
}
return 0;
}