颇具文案性质的解析。
我们把区间扩到 ,到时候减去一就行了。
显然一开始值域只有一段 ,每次取模会将 的段 劈成 个 和一个 ,我们将相同的段合并,用 map 维护,每次暴力取模即可,复杂度分析就是每次会将最大的一部分合并,这个均摊复杂度正确,并且产生的 必然会减小至少一半,所以每个段产生其他的段的数量不可能大于 ,所以复杂度 ,常数小跑不满,实测不到 100ms。
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int T, n, x, a[N];
void solve() {
cin >> n >> x;
for (int i = 1; i <= n; ++ i ) cin >> a[i];
map <int, int> cnt;
++ cnt[x];
for (int i = 1; i <= n; ++ i ) {
while (cnt.rbegin()->first >= a[i]) {
pair <int, int> tmp = *cnt.rbegin();
cnt.erase(tmp.first);
cnt[a[i] - 1] += tmp.second * (tmp.first / a[i]);
cnt[tmp.first % a[i]] += tmp.second;
}
}
int sum = 0;
for (auto i : cnt) sum += i.second;
cout << sum - 1 << '\n';
return;
}
signed main() {
ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> T;
while (T -- ) solve();
return 0;
}