T1 Coprime
数论。
核心思想是利用质因数分解来判断是否两两互质。
若多个数两两互质,则任意两个数没有共同的质因数。因此我们可以对每个数进行质因数分解,一旦发现某个质因数在两个不同的数中都出现过,就说明不是 pairwise coprime。
用线性筛预处理出每个数的最小质因子,然后对每个输入的数进行快速质因数分解。
用 标记质数 是否已经出现在之前的某个数中:
- 如果当前数的某个质因子 已经被标记过,则说明存在两个数共享该质因子,不满足两两互质。
- 否则将该质因子标记为已使用。
如果所有数都通过了这个检查,则为 pairwise coprime。
setwise coprime 按题意模拟即可。
时间复杂度为 。
#include<bits/stdc++.h>using namespace std;#define int long longconst int maxn = 1e6 + 5;int n, a[maxn], prime[maxn]; // prime[i] 存储 i 的最小质因子bool vis[maxn]; // 标记某个质数是否已出现void init() { for (int i = 2; i <= 1e6; i++) { if (prime[i] == 0) { // i 是质数 prime[i] = i; for (int j = i * i; j <= 1e6; j += i) { if (prime[j] == 0) prime[j] = i; // 标记最小质因子 } } }}
// 检查 x 的所有质因子是否之前未出现bool check(int x) { if (x == 0) return true; x = abs(x); if (x == 1) return true; // 1 没有质因子 while (x != 1) { int p = prime[x]; // 取出最小质因子 if (vis[p]) return false; vis[p] = true; while (x % p == 0) x /= p; // 去除所有 p 因子 } return true;}
signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); init(); // 预处理最小质因子 cin >> n; for (int i = 1; i <= n; i++) cin >> a[i];
bool flag = true; for (int i = 1; i <= n; i++) { if (!check(a[i])) { flag = false; break; } }
if (flag) { cout << "pairwise coprime"; return 0; }
// 判断 setwise coprime int t = a[1]; for (int i = 2; i <= n; i++) { t = __gcd(t, a[i]); if (t == 1) break; } if (t == 1) cout << "setwise coprime"; else cout << "not coprime";
return 0;}反思
注意边界条件,赛时调了 15 min 才找到问题。
T2 旅游
图论。
题目给出一棵树),部分城市有传送门,传送门之间可以 时间互相传送。
每次询问两点间最短时间,可以选择:
- 直接走树上路径(距离 = 深度差)
- 从起点走到任意一个传送门城市,传送到另一个传送门城市,再走到终点。
所以答案为:
其中:
- 是树上的距离。
- 表示从 到最近的传送门城市的最短距离。
由于传送门之间 时间传送,我们只需要知道每个点到最近传送门的距离。
用多源 BFS 将所有传送门城市加入队列,初始距离为 ,然后正常 BFS 即可。
树上距离通过 LCA 实现:
总时间复杂度为 。
#include<bits/stdc++.h>using namespace std;#define int long longconst int maxn = 2e5 + 5;int n, k, T, dep[maxn], fa[maxn][25], dis[maxn]; // dep:深度,fa:倍增数组,dis:到最近传送门距离vector<int> e[maxn]; // 邻接表queue<int> q;// 预处理void dfs(int u, int f) { dep[u] = dep[f] + 1; fa[u][0] = f; for (int i = 1; i <= 20; i++) { fa[u][i] = fa[fa[u][i-1]][i-1]; } for (auto v : e[u]) { if (v != f) dfs(v, u); }}// LCA 查询int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); for (int i = 20; i >= 0; i--) { if (dep[fa[u][i]] >= dep[v]) u = fa[u][i]; } if (u == v) return u; for (int i = 20; i >= 0; i--) { if (fa[u][i] != fa[v][i]) { u = fa[u][i]; v = fa[v][i]; } } return fa[u][0];}// 多源 BFSvoid bfs() { while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : e[u]) { if (dis[v] == INT_MAX) { dis[v] = dis[u] + 1; q.push(v); } } }}signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k >> T; fill(dis, dis + 1 + n, INT_MAX); // 初始化 for (int i = 1, u, v; i < n; i++) { cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } dfs(1, 0); // 建树 for (int i = 1, x; i <= k; i++) { cin >> x; q.push(x); dis[x] = 0; } bfs(); // 多源最短路 while (T--) { int u, v; cin >> u >> v; cout << min(dis[u] + dis[v] ,/*直接走*/ dep[u] + dep[v] - 2 * dep[lca(u, v)]/*用传送门*/) << '\n'; } return 0;}T3 考试
贪心。
所有操作都是“提前出成绩”,我们只关心最终每门课出成绩的时间。
设最终最晚出成绩时间为 ,尝试枚举 从大到小。
对于每个 ,计算:
- 有多少门课原定时间 ?这些课必须被提前,总提前量为 。
- 有多少门课原定时间 ?它们可以被推迟来“换时间”给其他课,最大可提供 天。
使用操作 1 可以转移时间,最多能转移 天,代价为 。
剩余仍需提前的天数用操作 2 处理,代价为 。
所有 的学生,每人产生 ,总为 。
总代价 = 操作代价 + 等待代价。
用桶计数优化, 统计每门课原定时间, 统计学生期望时间。
逆序枚举 i 时,动态维护所有上文提到的变量。
随着 i 减小,把这些值逐步更新即可。
时间复杂度为 ,即 。
#include<bits/stdc++.h>using namespace std;#define ull unsigned long longconst int maxn = 1e5 + 5;ull A, B, C, n, m, sum1, t1, sum2, t2, sum3, t3, cnt[maxn], cnt2[maxn];// cnt[i]: 原定第 i 天出成绩的课程数量// cnt2[i]: 希望第 i 天或之前出成绩的学生数量signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> A >> B >> C >> n >> m; for (ull i = 1, x; i <= n; i++) { cin >> x; cnt2[x]++; sum3 += x; // 累加原始期望总和 t3++; } for (ull i = 1, x; i <= m; i++) { cin >> x; cnt[x]++; sum2 += x; // 累加原定总和 t2++; } ull ans = ULLONG_MAX; // 倒序枚举最终最晚出成绩时间 i for (ull i = 100000; i >= 1; i--) { // 将原定时间为 i 的课程从“可推迟”组移到“必须提前”组 sum1 += i * cnt[i]; // 新增的必须提前的“时间量”(实际是 b_j > i 的部分,这里逐步加入) t1 += cnt[i]; sum2 -= i * cnt[i]; // 从可提供额度中减去 t2 -= cnt[i];
// 将希望时间为 i 的学生从“会等待”组中移出(因为他们希望在 i 或之前,当最终时间为 i 时刚好不等) sum3 -= i * cnt2[i]; t3 -= cnt2[i];
if (!sum1) continue; // 没有课程需要提前,跳过 ull sum = 0; // 当前总代价
// 使用操作1(调整)转移时间:最多转移 min(需提前总量, 可推迟总量) ull need = sum1 - t1 * i; // 实际需要提前的总天数 ull provide = t2 * i - sum2; // 可提供的推迟天数
if (A < B) { // 优先使用便宜的操作1 ull useA = min(need, provide); sum += useA * A; need -= useA; if (need) sum += need * B; // 剩余用操作2 } else { // 操作1 更贵,直接全用操作2 sum += need * B; } // 加上学生等待代价:所有希望时间 < i 的学生,每人等 (i - t_i) 天 sum += (t3 * i - sum3) * C; ans = min(ans, sum); } cout << ans; return 0;}反思
容易掉进写 dp 的误区里面。
T4 寻宝
BFS + DP + 贪心构造。
字典序最小意味着前面尽可能多的 a。
考虑 表示从 走到 所需的最少修改次数。
找到最大的 ,使得存在点 满足 。记这个最大步数为 。
从所有满足 且 的点开始 BFS。
BFS 过程中,维护 表示第 步能到达的最小字符。
只有当 处的字符不比当前 大时才扩展,确保路径字典序最小。
最终答案是前 位全是 a,后面从 BFS 得到的 数组中取字符。
时间复杂度:。
#include<bits/stdc++.h>using namespace std;#define int long longstruct node { int x, y, step; // 表示坐标和当前步数};int n, k, maxx, ans[4005], dp[2005][2005]; // maxx: 最远能全'a'的步数, ans: BFS记录每步最小字符char a[2005][2005]; // 存储网格字符bool vis[2005][2005]; // BFS访问标记queue<node> q; // BFS队列const int dx[] = {0, 1}, dy[] = {1, 0}; // 只能向右或向下
// BFS:从所有能以恰好k次修改到达的最远点开始,构造字典序最小路径void bfs() { if (q.empty()) q.push({1, 1, 1}); // 如果没有点,从起点开始(理论上不会发生) ans[1] = a[1][1] - 'a'; // 第一步的字符 while (!q.empty()) { node cur = q.front(); q.pop(); int x = cur.x, y = cur.y, step = cur.step; // 如果当前字符比已知最优的大,或者步数小于已确定的maxx,剪枝 if (a[x][y] - 'a' > ans[step] || step < maxx) continue; for (int i = 0; i < 2; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx > n || ny > n || a[nx][ny] - 'a' > ans[step + 1] || vis[nx][ny]) continue; vis[nx][ny] = true; ans[step + 1] = a[nx][ny] - 'a'; // 更新第step+1步的最小字符 q.push({nx, ny, step + 1}); } }}
signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; // 初始化ans和dp数组为极大值 memset(ans, 0x3f, sizeof(ans)); memset(dp, 0x3f, sizeof(dp)); // 从(0,1)或(1,0)出发,修改次数为0 dp[1][0] = dp[0][1] = 0; // 计算到每个点所需的最少修改次数 for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { // 从上方或左方转移 dp[i][j] = min(dp[i-1][j], dp[i][j-1]); // 如果当前字符不是'a',需要一次修改 dp[i][j] += (a[i][j] == 'a' ? 0 : 1); // 如果恰好需要k次修改,且这是目前最远的步数,则标记该点为BFS起点 if (dp[i][j] == k && maxx <= i + j - 1) { maxx = i + j - 1; vis[i][j] = true; q.push({i, j, i + j - 1}); } } }
// 特殊情况:如果整个路径修改'a'的次数小于k,说明可以全改成'a' if (dp[n][n] < k) { for (int i = 1; i < 2 * n; i++) cout << 'a'; return 0; } bfs(); for (int i = 1; i <= maxx; i++) cout << 'a'; for (int i = maxx + 1; i < 2 * n; i++) cout << (char)(ans[i] + 'a');
return 0;}反思
这道题综合性很强,结合了 DP、BFS 和贪心思想。
关键在于理解 字典序最小 与 前导 a 最多 之间的关系。
要处理好边界条件。
分享文章
生成精美分享图或复制链接,与更多人分享本文。
继续阅读
换条路线
从其他文章中稳定抽取
最后更新于 ,距今已过 465 天
部分内容可能已过时