AtCoder Beginner Contest 178
比赛链接:https://atcoder.jp/contests/abc178/tasks
A - not
题意
给出一个整数 $0 \le x \le 1$,如果 $x$ 是 $0$ 就输出 $1$,如果 $x$ 是 $1$ 就输出 $0$ 。
题解
输出 $x \oplus 1$ 或 $!x$ 均可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x;
cin >> x;
cout << (x ^ 1) << "\n";
return 0;
}
B - Product Max
题意
给出 $a,b,c,d$,问 $x \times y$ 的最大值。($-10^9 \leq a \leq x \leq b \leq 10^9$,$-10^9 \leq c \leq y \leq d \leq 10^9$)
题解
相似题目:CF1406B,枚举四个端点之积即可。
简单解释一下:
- 当 $x,y$ 均为正数或正端点之积较大,此时答案为 $b \times d$
- 当 $x,y$ 均为负数或负端点之积较大,此时答案为 $a \times c$
- 当 $x,y$ 一方为正数一方为负数,此时答案为 $a \times d$ 或 $b \times c$
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long a, b, c, d;
cin >> a >> b >> c >> d;
cout << max({a * c, a * d, b * c, b * d}) << "\n";
return 0;
}
C - Ubiquity
题意
找出满足以下条件的长为 $n$ 的不同序列的个数:
- $0 \le a_i \le 9$
- 序列中至少有一个 $a_i = 0$
- 序列中至少有一个 $a_i = 9$
答案对 $10^9+7$ 取模。
题解
总的序列个数为 $10^n$,
不含 $0$ 的序列个数为 $9^n$,
不含 $9$ 的序列个数为 $9^n$,
不含 $0$ 和 $9$ 的序列个数为 $8^n$,
根据容斥原理,答案即 $10^n - 2 \times 9^n + 8 ^ n$ 。
代码
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7; int binpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1LL * res * a % MOD;
a = 1LL * a * a % MOD;
b >>= 1;
}
return res;
} int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
cout << (0LL + binpow(10, n) - 2 * binpow(9, n) + binpow(8, n) + 2 * MOD) % MOD << "\n";
return 0;
}
D - Redistribution
题意
给出一个正整数 $s$,问有多少序列满足 $a_i \ge 3$,且序列和为 $s$,答案对 $10^9+7$ 取模。
题解一
设 $dp_i$ 为序列和为 $i$ 的序列个数。
初始时只有序列为空这一种情况,即 $dp_0 = 1$,之后每次向序列中添加大于等于 $3$ 的元素,最终答案即 $dp_s$ 。
代码
#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int s;
cin >> s;
vector<int> dp(2020);
dp[0] = 1;
for (int i = 0; i <= s; i++) {
for (int j = 3; i + j <= s; j++) {
(dp[i + j] += dp[i]) %= MOD;
}
}
cout << dp[s] << "\n";
return 0;
}
题解二
$ dp_i = \sum\limits_{j=0}^{i-3}dp_j $
$ dp_{i-1} = \sum\limits_{j=0}^{i-4}dp_j $
$ dp_i = \sum\limits_{j=0}^{i-3}dp_j = \sum\limits_{j=0}^{i-4}dp_j + dp_{i-3} = dp_{i-1} + dp_{i - 3} $
代码
#include <bits/stdc++.h>
using namespace std;
constexpr int MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int s;
cin >> s;
vector<int> dp(2020);
dp[0] = 1;
for (int i = 3; i <= s; i++)
dp[i] = (dp[i - 1] + dp[i - 3]) % MOD;
cout << dp[s] << "\n";
return 0;
}
题解三
已知序列和为 $s$ ,枚举序列的长度 $i$ 。
因为每个元素都大于等于 $3$,假设每个元素都为 $3$ ,可以将多出来的 $ s-3*i$ 个 $1$ 看作 $n$ 个相同的小球放入 $m=i$ 个可为空的相同的盒子中。
此时即转化为组合数学十二路问题中的 $ULA$ 问题,答案即 $\sum_{i = 1}^{\lfloor \frac{s}{3} \rfloor}C_{n+m - 1}^{m - 1}$ 。
代码
#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e6 + 100;
constexpr int MOD = 1e9 + 7; int fac[N], inv[N]; int binpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1LL * res * a % MOD;
a = 1LL * a * a % MOD;
b >>= 1;
}
return res;
} int C(int n, int m){
if(m < 0 or m > n) return 0;
return 1LL * fac[n] * inv[m] % MOD * inv[n - m] % MOD;
} void Init(){
fac[0] = 1;
for (int i = 1; i < N; i++) fac[i] = 1LL * fac[i - 1] * i % MOD;
inv[N - 1] = binpow(fac[N - 1], MOD - 2);
for (int i = N - 2; i >= 0; i--) inv[i] = 1LL * inv[i + 1] * (i + 1) % MOD;
} int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Init();
int s;
cin >> s;
long long ans = 0;
for (int i = 1; i <= s / 3; i++) {
(ans += C(s - 3 * i + i - 1, i - 1)) %= MOD;
}
cout << ans << "\n";
return 0;
}
E - Dist Max
题意
给出 $n$ 个二维平面中的点,问两点间的最远曼哈顿距离。
题解
$(x_i,y_i), (x_j, y_j)$ 间的曼哈顿距离为 $|x_i-x_j| + |y_i-y_j|$ 。
假设 $x_i \ge x_j$,那么两点间的距离可能有两种情况:
- $(x_i-x_j) + (y_i-y_j)$,移项得:$(x_i+y_i) - (x_j+ y_j)$
- $(x_i-x_j) + (y_j-y_i)$,移项得:$(x_i-y_i) - (x_j- y_j)$
所以对于每个点的坐标 $(x,y)$,分别用两个数组存储 $x+y$ 和 $x-y$,然后取两个数组较大的最值差即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a[i] = x + y;
b[i] = x - y;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
cout << max(a.back() - a.front(), b.back() - b.front()) << "\n";
return 0;
}
F - Contrast
题意
给出两个非递减序列 $a$ 和 $b$,问能否重排 $b$ 使得 $a_i \ne b_i $ 。
题解
将序列 $b$ 反转,因为 $a$ 为非递减序,$b$ 为非递增序,所以序列中至多存在一段区间 $[l,r]$ 使得 $a_i = b_i = c$ 。
如果区间外的某个 $b_j$ 与区间内的 $b_i$ 调换后满足题意,那么 $a_j \ne b_i,\ b_j \ne a_i$,即 $a_j \ne c$ 且 $b_j \ne c$ 。
如果这样的 $b_j$ 个数小于区间长度则无解,否则输出调换后的序列 $b$ 即可。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 0; i < n; i++)
cin >> b[i];
reverse(b.begin(), b.end());
int c = -1;
for (int i = 0; i < n; i++) {
if (a[i] == b[i]) {
c = a[i];
break;
}
}
int l = n, r = -1;
for (int i = 0; i < n; i++) {
if (a[i] == c and b[i] == c) {
l = min(l, i);
r = max(r, i);
}
}
for (int i = 0; i < n; i++) {
if (a[i] != c and b[i] != c and l <= r) {
swap(b[i], b[l]);
++l;
}
}
if (l <= r) {
cout << "No" << "\n";
return 0;
}
cout << "Yes" << "\n";
for (int i = 0; i < n; i++) {
cout << b[i] << " \n"[i == n - 1];
}
return 0;
}
AtCoder Beginner Contest 178的更多相关文章
- AtCoder Beginner Contest 178 E - Dist Max 题解(推公式)
题目链接 题目大意 给你n个点(n<=2e5)要你求所有点中两个点最短的曼哈顿距离 曼哈顿距离定义为d(i,j)=|x1-x2|+|y1-y2|. 题目思路 想了很久也没有什么思路,其实就是一个 ...
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
- AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...
随机推荐
- Face_to_object_design
二.实例 掷骰子游戏:三粒骰子,掷两次,比较两次的结果. 1.提炼 提炼对象:三粒骰子.游戏 提炼对象的属性和功能:掷骰子.比较点数 骰子: 属性:点数 功能:随机获取一个1~6之间的整数值. 游戏: ...
- 转载-Oracle 数据库导入导出 dmp文件
首先询问对方数据库的表空间名称和大小,然后在你的oracle中建立相应表空间,最后使用命令导入.导出数据.补充:1.要新建一个数据库: Oracle数据导入导出imp/exp就相当于oracle数据还 ...
- 【ORACLE】11g rac+dg
首先感谢群友分享的文档,在这里先感谢哆啦B梦,非常感谢 该文档主要指导如何利用现有的RAC环境搭建一套RAC与单实例的DG的环境 ============================主机配置信息 ...
- i春秋新春战疫—web—简单的招聘系统
打开靶机 打开后看到登录界面 利用万能密码,以admin身份登录 登录成功后看到如下界面 在Blank Page界面内发现注入点,抓包 保存在sqlmap目录下test.txt文件夹,使用sqlmap ...
- CTFhub刷题记录
一 [WesternCTF2018]shrine 没什么好说的,SSTI模版注入类问题,过滤了()但是我们不慌.开始注入,{{29*3}}测试通过. 发现是jinjia2的模版注入.关键点在于没有() ...
- C# ADO.NET连接字符串详解
C#中连接字符串包含以下内容 参数 说明 Provider 设置或者返回提供的连接程式的名称,仅用于OLeDbConnection对象 Connection Timeout 在终止尝试并产生异常前,等 ...
- SAP 摘录数据集
要在报表中创建并填充摘录数据集,需要执行三步骤:1.将要在摘录数据集中使用的记录类型定义为字段组FIELD-GROUPS该语句定义了字段组,字段组可以将几个字段组合到一个名称下,字段组不为字段保留存储 ...
- sentinel-实战
sentinel-实战笔记 什么是Sentinel Sentinel是阿里开源的项目,提供了流量控制.熔断降级.系统负载保护等多个维度来保障服务之间的稳定性. Sentinel主要特性: 获取Sent ...
- CF625E Frog Fights
有\(n\)只青蛙在一个长度为\(m\)的环上打架:每只青蛙有一个初始位置\(p_i\),和一个跳跃数值\(a_i\).从\(1\)号青蛙开始按序号循环行动,每次若第\(i\)只青蛙行动,则它会向前跳 ...
- 网络可视化工具netron详细安装流程
1.netron 简介 在实际的项目中,经过会遇到各种网络模型,需要我们快速去了解网络结构.如果单纯的去看模型文件,脑海中很难直观的浮现网络的架构. 这时,就可以使用netron可视化工具,可以清晰的 ...