跳到正文

题解

题解:AT_abc466_f [ABC466F] Many Mod Calculation

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

文章信息

发布
2026年7月13日
收录
2026年7月16日
更新
2026年11月8日
篇幅
约 2 分钟
获赞
3

版本

  1. v1当前

颇具文案性质的解析。

我们把区间扩到 [0,X][0,X],到时候减去一就行了。

显然一开始值域只有一段 [0,X][0,X],每次取模会将 ≥ai\ge a_i 的段 [0,x][0,x] 劈成 ⌊xai⌋\left \lfloor \frac{x}{a_i} \right \rfloor 个 [0,ai−1][0,a_i-1] 和一个 [0,x mod ai][0,x\bmod a_i],我们将相同的段合并,用 map 维护,每次暴力取模即可,复杂度分析就是每次会将最大的一部分合并,这个均摊复杂度正确,并且产生的 x mod aix\bmod a_i 必然会减小至少一半,所以每个段产生其他的段的数量不可能大于 log⁡X\log X,所以复杂度 O(Tnlog⁡nlog⁡X)O(Tn\log n\log X),常数小跑不满,实测不到 100ms。

代码:

C++
#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;
}
原文
存档评论1 条抓取于