比赛链接

A

题意

给一个字符串 \(s\) ,对其加倍,即每个字符后面追加一个相同字符。

加倍后可以重排列,要求构造一个回文串。

题解

知识点:构造。

既然可以重排列了,那顺序是随意的了,直接翻转加在原来的后面。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#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

题意

构造有 \(n\) 个数的序列 \(a(1\leq a_i \leq 10^9)\) ,满足:

\[a_1 \oplus a_2 \oplus \cdots \oplus a_n = \frac{1}{n} \sum_{i=1}^{n} a_i
\]

题解

知识点:构造。

方法一

  1. \(n\) 为奇数,显然构造一样的 \(n\) 个数就行。

  2. \(n\) 为偶数,仿造奇数情况,尝试在 \(n-1\) 个数 \(a\) 后加一个数 \(b\) ,于是我们只要找到满足 \(n(a \oplus b) = (n-1)a + b\) 的 \(a\) 和 \(b\) 即可。

假设 \(a>b\) ,根据需要满足的条件可以得到 \(a \oplus b < a\) ,因此我们需要用 \(b\) 通过异或缩小 \(a\) 。

我们假设 \(b\) 只会消去一些 \(a\) 的二进制位,而不会增加,那么 \(a \oplus b = a-b\) ,从而原方程变为 \(n(a-b) = (n-1)a + b\) ,解得 \(a = (n+1)b\) 。

我们取 \(b = 1\) ,那么 \(a = n+1\) ,刚好满足两条假设 \(a>b\) 和 \(a \oplus b = a-b\) ,因此是合法的。

方法二

  1. \(n\) 为奇数,构造 \(n\) 个 \(2\) 。
  2. \(n\) 为偶数,构造 \(n-2\) 个 \(2\) ,随后 \(1\) 和 \(3\) 。

时间复杂度 \(O(n)\)

空间复杂度 \(O(1)\)

代码

方法一

#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

题意

给出 \(n,x\) ,构造一个长为 \(n\) 的排列 \(p\) ,满足 \(p_1 = x,p_n = 1\) ,且 \(p_i\) 是 \(i\) 的倍数,其中 \(2\leq i \leq n-1\) ,多个答案输出字典序最小的。

题解

方法一

知识点:构造,数论,质因子分解。

注意到 \(n \mod x \neq 0\) 时,一定不存在方案。因为 \(x\) 不是合法的位置,那么假设 \(n\) 放在任意合法的位置,那个位置的数一定会替换在他前面合法位置的数。但这些数一定是 \(n\) 的因子,那么一定不是 \(x\) 的倍数,替换到最后一定会有一个素数没地方放,因此无解。

如果有解,我们要让字典序最小。因为 \(x\) 空出来了,我们可以每次往前提最小的合法的数字,这样字典序最小。

我们可以分解 \(d = \frac {n}{x}\) 的质因子,得到 \(d = a_1^{k_1}a_2^{k_2}\cdots a_n^{k_n}\) ,每次让当前位置下标乘上目前最小质因子的数填到当前位置,即 $p_x = a_1x,p_{a_1x} = a_12x,\cdots,p_{a_1{k_1-1}} = a_1^{k_1}x, p_{a_1^{k_1}} = a_1{k_1}a_2x,\cdots,p_{a_1{k_1}a_2^{k_2}\cdots a_n^{k_n-1}} = dx = n $ 。

时间复杂度 \(O(n)\)

空间复杂度 \(O(\sqrt n)\)

方法二

知识点:构造,数论。

\(n \mod x \neq 0\) 无解。

如果有解,我们先令排列 \(x,2,\cdots,x-1,n,x+1,\cdots,n-1,1\) ,然后把 \(n\) 往后移。设当前 \(p_{cur} = n\) ,如果一个位置 \(i\) 满足 $n \mod i = i \mod cur = 0 $ 那么可以把 \(p_i\) 和 \(p_{cur}\) 交换,这样就将小的数字往前提了。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

方法一

#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

题意

构造含有 \(n\) 个数的序列 \(a(1\leq a_i\leq 10^9)\) ,满足:

\[\max(a_1,a_2,\cdots,a_n) - \min(a_1,a_2,\cdots,a_n) = \sqrt{\sum_{i=1}^n a_i}
\]

题解

知识点:构造。

  1. \(n\) 为偶数时,容易构造等式结果为 \(n\) 的序列 \(n - \frac{n}{2},\cdots ,n-1,n+1,\cdots ,n - \frac{n}{2}\) 。

  2. \(n\) 为奇数时,可以仿造 \(n\) 为偶数的操作,但发现构造等式结果为 \(n\) 的序列是不可能的,原因是数字之间的间隔太小,数字大小上没有操作空间,因此尝试构造等式结果为 \(2n\) 的序列。

同样对称操作,\(3n,3n+\lfloor \frac{2n}{n-1} \rfloor,\cdots ,3n+(\lfloor \frac{n}{2} \rfloor-1) \lfloor \frac{2n}{n-1} \rfloor, 4n,5n-(\lfloor \frac{n}{2} \rfloor-1) \lfloor \frac{2n}{n-1} \rfloor,\cdots ,5n-\lfloor \frac{2n}{n-1} \rfloor,5n\) 即可。

时间复杂度 \(O(n)\)

空间复杂度 \(O(n)\)

代码

#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;
}

Codeforces Round #836 (Div. 2) A-D的更多相关文章

  1. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  7. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

  10. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

随机推荐

  1. 《吐血整理》进阶系列教程-拿捏Fiddler抓包教程(19)-Fiddler精选插件扩展安装,将你的Fiddler武装到牙齿

    1.简介 Fiddler本身的功能其实也已经很强大了,但是Fiddler官方还有很多其他扩展插件功能,可以更好地辅助Fiddler去帮助用户去开发.测试和管理项目上的任务.Fiddler已有的功能已经 ...

  2. LFS(Linux From Scratch)构建过程全记录(二):磁盘分区

    写在前面 本文将会详细记录LFS中,构建分区,构建文件系统和挂载分区的全过程 准备新硬盘 为了更加符合"从零开始构建Linux"的要求,我在虚拟机中,新建了一个磁盘 我们将会在这个 ...

  3. Kubernetes生态架构图

    图片来源于:https://gitbook.curiouser.top/ 一.kubernetes 集群架构图 二.Openshift or Kubernetes 集群架构图 三.常见的 CI/CD ...

  4. 使用Filebeat传送多行日志

    文章转载自:https://blog.csdn.net/UbuntuTouch/article/details/106272704 在解决应用程序问题时,多行日志为开发人员提供了宝贵的信息. 堆栈跟踪 ...

  5. 使用port-forward访问集群中的应用程序,以Redis 为例

    为Redis创建Deployment和Service 创建 Redis Deployment,YAML文件如下: apiVersion: apps/v1 kind: Deployment metada ...

  6. My life of Honker Security Commando

    红客突击队 && 红客突击分队 红客突击队,于2019年,由队长k龙联合国内多位顶尖高校研究生牵头成立.其团队从成立至今多次参加国际网络安全竞赛并取得良好成绩,积累了丰富的竞赛经验.团 ...

  7. PAT (Basic Level) Practice 1031 查验身份证 分数 15

    一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8, ...

  8. C# 内存泄漏之 Internal 关键词代表什么?

    一:背景 1. 背景 前段时间有位朋友咨询说他的程序出现了非托管内存泄漏,说里面有很多的 HEAP_BLOCK 都被标记成了 Internal 状态,而且 size 都很大, 让我帮忙看下怎么回事? ...

  9. Mysql单表访问方法,索引合并,多表连接原理,基于规则的优化,子查询优化

    参考书籍<mysql是怎样运行的> 非常推荐这本书,通俗易懂,但是没有讲mysql主从等内容 书中还讲解了本文没有提到的子查询优化内容, 本文只总结了常见的子查询是如何优化的 系列文章目录 ...

  10. oracle expdp/exp ora-600/ora-39014报错处理

    在一次数据迁移的时候,expdp导出报错,错误信息如下: 版本号:11.2.0.1 没有打PSU,查看报错的aler部分日志如下: 其中的某一些trc日志文件截图: Trace file d:\ora ...