Codeforces Round #836 (Div. 2) A-D
A
题意
给一个字符串 ,对其加倍,即每个字符后面追加一个相同字符。
加倍后可以重排列,要求构造一个回文串。
题解
知识点:构造。
既然可以重排列了,那顺序是随意的了,直接翻转加在原来的后面。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> #define ll long long using namespace std; bool solve() { string s; cin >> s; cout << s; reverse(s.begin(), s.end()); cout << s << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
B
题意
构造有 个数的序列 ,满足:
题解
知识点:构造。
方法一
-
为奇数,显然构造一样的 个数就行。
-
为偶数,仿造奇数情况,尝试在 个数 后加一个数 ,于是我们只要找到满足 的 和 即可。
假设 ,根据需要满足的条件可以得到 ,因此我们需要用 通过异或缩小 。
我们假设 只会消去一些 的二进制位,而不会增加,那么 ,从而原方程变为 ,解得 。
我们取 ,那么 ,刚好满足两条假设 和 ,因此是合法的。
方法二
- 为奇数,构造 个 。
- 为偶数,构造 个 ,随后 和 。
时间复杂度
空间复杂度
代码
方法一
#include <bits/stdc++.h> #define ll long long using namespace std; bool solve() { int n; cin >> n; for (int i = 1;i <= n - 1;i++) cout << n + 1 << ' '; if (n & 1) cout << n + 1 << '\n'; else cout << 1 << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
方法二
#include <bits/stdc++.h> #define ll long long using namespace std; bool solve() { int n; cin >> n; if (n == 1) { cout << 2 << '\n'; return true; } for (int i = 1;i <= n - 2;i++) cout << 2 << ' '; if (n & 1) cout << 2 << ' ' << 2 << '\n'; else cout << 1 << ' ' << 3 << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
C
题意
给出 ,构造一个长为 的排列 ,满足 ,且 是 的倍数,其中 ,多个答案输出字典序最小的。
题解
方法一
知识点:构造,分解质因数。
注意到 时,一定不存在方案。因为 不是合法的位置,那么假设 放在任意合法的位置,那个位置的数一定会替换在他前面合法位置的数。但这些数一定是 的因子,那么一定不是 的倍数,替换到最后一定会有一个素数没地方放,因此无解。
如果有解,我们要让字典序最小。因为 空出来了,我们可以每次往前提最小的合法的数字,这样字典序最小。
我们可以分解 的质因子,得到 ,每次让当前位置下标乘上目前最小质因子的数填到当前位置,即
时间复杂度
空间复杂度
方法二
知识点:构造,数论。
无解。
如果有解,我们先令排列 ,然后把 往后移。设当前 ,如果一个位置 满足 那么可以把 和 交换,这样就将小的数字往前提了。
时间复杂度
空间复杂度
代码
方法一
#include <bits/stdc++.h> #define ll long long using namespace std; bool solve() { int n, x; cin >> n >> x; if (n % x) return false; int d = n / x; vector<int> ft; for (int i = 2;i <= d / i;i++) { while (d % i == 0) ft.push_back(i), d /= i; } if (d > 1) ft.push_back(d); reverse(ft.begin(), ft.end()); cout << x << ' '; int mul = 1; for (int i = 2;i <= n - 1;i++) { if (i == mul * x) cout << ((mul *= ft.back()) * x) << ' ', ft.pop_back(); else cout << i << ' '; } cout << 1 << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
方法二
#include <bits/stdc++.h> #define ll long long using namespace std; bool solve() { int n, x; cin >> n >> x; if (n % x) return false; vector<int> v(n + 1); for (int i = 2;i <= n - 1;i++) v[i] = i; v[1] = x; v[x] = n; v[n] = 1; int cur = x; for (int i = x + 1;i <= n - 1;i++) { if (i % cur == 0 && n % i == 0) swap(v[cur], v[i]), cur = i; } for (int i = 1;i <= n;i++) cout << v[i] << ' '; cout << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
D
题意
构造含有 个数的序列 ,满足:
题解
知识点:构造。
-
为偶数时,容易构造等式结果为 的序列 。
-
为奇数时,可以仿造 为偶数的操作,但发现构造等式结果为 的序列是不可能的,原因是数字之间的间隔太小,数字大小上没有操作空间,因此尝试构造等式结果为 的序列。
同样对称操作, 即可。
时间复杂度
空间复杂度
代码
#include <bits/stdc++.h> #define ll long long using namespace std; bool solve() { int n; cin >> n; if (n & 1) { int d = 2 * n / (n - 1); for (int i = 1;i <= n / 2;i++) cout << 3 * n + (i - 1) * d << ' '; cout << 4 * n << ' '; for (int i = n / 2;i >= 1;i--) cout << 5 * n - (i - 1) * d << ' '; } else { for (int i = n - n / 2;i <= n + n / 2;i++) if (i != n) cout << i << ' '; } cout << '\n'; return true; } int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { if (!solve()) cout << -1 << '\n'; } return 0; }
本文来自博客园,作者:空白菌,转载请注明原文链接:https://www.cnblogs.com/BlankYang/p/16928287.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)