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 ...
随机推荐
- 【JavaWeb】i18n 国际化
i18n 国际化 什么是 i18n 国际化(Internationalization)指的是同一个网站可以支持多种不同的语言,以方便不同国家,不同语种的用户访问. 希望相同的一个网站,不同人访问的时候 ...
- Java向指定Excel写入读取数据
今天在开发中遇到用户列表导入导出的功能实现,这里了解到使用POI函数库可以完成此任务!特此记录一下 POI Apache POI是Apache软件基金会开放的源码函数库,POI提供API给Java程序 ...
- python_元组(tuple)
#tuple(),元组不可以修改,不能对其进行增加或删除操作,元组是有序的 #1.定义 tu_1 = () #定义一个空元组 tu_2 = (1,2,'alex',[3,4],(5,6,7),True ...
- Docker haproxy应用构建 (五)
编写dockerfile from centos-base:v1 MAINTAINER 57674891@qq.com RUN mkdir -p /data/{soft,src,logs,script ...
- (十三)利用BASE_DIR来import模板
实际工程的组织架构一般是这样的: bin包下的bin.py是实际的执行文件,my_mould包下的是业务逻辑的实现模板 bin.py需要import my_mould下的py文件,而bin和my_mo ...
- 用percona monitoring plugins 监控mysql
下载:http://www.percona.com/redir/downloads/percona-monitoring-plugins/1.1.1/percona-zabbix-templates- ...
- 使用 gRPCurl 调试.NET 5的gPRC服务
介绍 你用过 Curl 吗?这个工具允许你通过 http 来发送数据,现在有一个适用于gGRPC的工具,gRPCurl,在本文中,我将介绍如何下载安装这个工具,然后通过这个工具调试我们.NET 5上面 ...
- os-hackos-3-docker提权
0x00 cewl http://192.168.43.179/websec/爬取页面所有的单词做成字典 hydra -l contact@hacknos.com -P cewl.txt 192.16 ...
- bat批处理积累
1 ::所有命令不回显,包含echo off自身也不回显 2 @echo off 3 4 ::rem或双冒号都为注释行 5 6 rem 变量赋值,注意变量和等号之间不能有空格,等号后的空格会作为变量值 ...
- 图像分割论文 | DRN膨胀残差网络 | CVPR2017
文章转自:同作者个人微信公众号[机器学习炼丹术].欢迎交流沟通,共同进步,作者微信:cyx645016617 论文名称:'Dilated Residual Networks' 论文链接:https:/ ...