AtCoder Beginner Contest 182 Person Editorial
Problem A - twiblr
直接输出 \(2A + 100 - B\)
Problem B - Almost GCD
这里暴力枚举即可
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) cin >> A[i];
int Max = 0, idx = -1;
for (int i = 2; i <= 1000; ++i) {
int cnt = 0;
for (int j = 0; j < N; ++j)
if (A[j] % i == 0) cnt++;
if (Max < cnt) Max = cnt, idx = i;
}
cout << idx << '\n';
return 0;
}
Problem C - To 3
利用一个数能被 \(3\) 整除当且仅当其各位之和能被 \(3\) 整除。
- 如果本身能被 \(3\) 整除,则不需要删除。
- 如果被 \(3\) 除余 \(1\),则首先看是否能删去 \(1\) 个 \(1\),然后看是否能删去 \(2\) 个 \(2\)。
- 如果被 \(3\) 除余 \(1\),则首先看是否能删去 \(1\) 个 \(2\),然后看是否能删去 \(2\) 个 \(1\)。
C++ 代码
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
string s;
cin >> s;
int cnt[3] = {0};
for (int i = 0; i < s.length(); ++i) cnt[(s[i] - '0') % 3]++;
int cur = (cnt[1] + 2 * cnt[2]) % 3;
int k = cnt[0] + cnt[1] + cnt[2];
int res;
if (!cur) res = 0;
else if (cur == 1) {
if (cnt[1])
if (k == 1) res = -1;
else
res = 1;
else {
if (k == 2) res = -1;
else
res = 2;
}
} else {
if (cnt[2]) {
if (k == 1) res = -1;
else
res = 1;
} else {
if (k == 2) res = -1;
else
res = 2;
}
}
cout << res << "\n";
return 0;
}
Python 代码
s = input()
n = int(s)
if n % 3 == 0:
print(0)
else:
a = list(map(int, list(s)))
c = [0] * 3
for i in a:
c[i % 3] += 1
if c[n % 3] >= 1 and len(a) > 1:
print(1)
elif c[3 - n % 3] >= 2 and len(a) > 2:
print(2)
else:
print(-1)
Problem D - Wandering
记录最远位置 \(ans\),当前位置 \(pos\),前缀和 \(sum\),以及前缀和的最大值 \(hi\)。
在每一轮中,首先更新前缀和,然后更新前缀和的最大值,本轮能达到的最大值显然是 \(pos+hi\),用其更新 \(ans\),再用 \(pos+sum\) 更新 \(pos\)。
时间复杂度 \(\mathcal{O}(N)\)。
using ll = long long;
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
ll a, sum = 0, hi = 0, ans = 0, pos = 0;
for (int i = 0; i < n; ++i) {
cin >> a;
sum += a;
hi = max(hi, sum);
ans = max(ans, pos + hi);
pos += sum;
}
cout << ans << "\n";
return 0;
}
Problem E - Akari
将所有灯和墙都放到矩形中,然后逐行从左到右扫描一遍,再从右到左扫描一遍;逐列从上到下扫描一遍,再从下到上扫描一遍。最后统计亮着的格子即可。
时间复杂度 \(\mathcal{O}(HW)\)。
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int e[1510][1510];
bool vis[1510][1510];
int cnt = 0;
struct node {
int x, y;
};
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int h, w, n, m;
cin >> h >> w >> n >> m;
vector<node> bulbs(n);
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y, e[x][y] = 1; // bulb
bulbs[i].x = x, bulbs[i].y = y;
}
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y, e[x][y] = 2; // block
}
for (int i = 0; i < n; ++i) {
int x = bulbs[i].x, y = bulbs[i].y;
if (vis[x][y] == false) {
cnt++, vis[x][y] = true;
}
while ((e[x][y] == 0 || (x == bulbs[i].x and y == bulbs[i].y)) &&
x > 0 && x <= h && y > 0 && y <= w) {
if (vis[x][y] == false) cnt++, vis[x][y] = true;
x++;
}
x = bulbs[i].x, y = bulbs[i].y;
while ((e[x][y] == 0 || (x == bulbs[i].x and y == bulbs[i].y)) &&
x > 0 && x <= h && y > 0 && y <= w) {
if (vis[x][y] == false) cnt++, vis[x][y] = true;
x--;
}
x = bulbs[i].x, y = bulbs[i].y;
while ((e[x][y] == 0 || (x == bulbs[i].x and y == bulbs[i].y)) &&
x > 0 && x <= h && y > 0 && y <= w) {
if (vis[x][y] == false) cnt++, vis[x][y] = true;
y--;
}
x = bulbs[i].x, y = bulbs[i].y;
while ((e[x][y] == 0 || (x == bulbs[i].x and y == bulbs[i].y)) &&
x > 0 && x <= h && y > 0 && y <= w) {
if (vis[x][y] == false) cnt++, vis[x][y] = true;
y++;
}
}
cout << cnt << '\n';
return 0;
}
Problem F - Valid payments
dalao 题解,Orz...
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 52;
ll a[N], mx[N], xx[N], dp[N][2];
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
ll x, t;
cin >> n >> x;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) mx[i] = a[i + 1] / a[i];
t = x;
for (int i = n; i; --i) {
xx[i] = t / a[i];
t %= a[i];
}
dp[1][0] = 1;
if (xx[1]) dp[1][1] = 1;
for (int i = 1; i < n; ++i) {
dp[i + 1][0] = dp[i][0];
if (xx[i + 1]) dp[i + 1][1] = dp[i][0];
dp[i + 1][1] += dp[i][1];
if (xx[i + 1] + 1 != mx[i + 1]) dp[i + 1][0] += dp[i][1];
}
cout << dp[n][0] << '\n';
return 0;
}
这道题,我们实际上就是要求出满足
\]
并且满足
\]
的整数元组 \(\{k_i\}\)的种数。
我们不妨从小到大进行选择。容易看到,我们其实只需要记录当前每一个可能达到的总数以及对应的方法数,而不需要记录对应的具体方案。因为\(a_{i+1}\)总是\(a_i\)的倍数,所以在选择完\(a_i\)的系数k_iki后,我们需要保证此时的总数能够被\(a_{i+1}\)整除。同时,因为\(|k_ia_i| < a_{i+1}\)的限制,因此,对于每一个原有的状态,我们实际上只能有两种选择。
我们以\(\{x,1\}\)作为初始状态开始递推。看起来,状态数会以指数规模增长,但实际上,任意时刻,我们最多同时保留两个状态,因此总时间复杂度为 \(\mathcal{O}(N)\)。
using ll = long long;
int main() {
int n;
ll x;
cin >> n >> x;
vector<ll> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
unordered_map<ll, ll> v;
v[x] = 1;
ll ans = 0;
for (int i = 0; i < n; ++i) {
unordered_map<ll, ll> nv;
for (auto [c, f] : v) {
if (i + 1 < n) {
ll rem = c % a[i + 1];
nv[c - rem] += f;
if (rem > 0) nv[c + a[i + 1] - rem] += f;
} else if (c % a[i] == 0)
nv[0] += f;
}
v = move(nv);
}
cout << v[0];
}
AtCoder Beginner Contest 182 Person Editorial的更多相关文章
- AtCoder Beginner Contest 182 D - Wandering (前缀和)
题意:在\(x\)轴上,你刚开始在\(0\)的位置,第\(i\)次操作需要走\(A_1,...,A_i\)个单位,如果\(A_i\)为正向右走,否则向左走,求你所能走到的最大坐标. 题解:我们一步一步 ...
- AtCoder Beginner Contest 182 F
F - Valid payments 简化题意:有\(n\)种面值的货币,保证\(a[1]=1,且a[i+1]是a[i]的倍数\). 有一个价格为\(x\)元的商品,付款\(y\)元,找零\(y-x\ ...
- 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 ...
随机推荐
- 多项目git账户用户名和邮箱设置以及局部github代理
因为公司使用自建的gitlab服务器所以需要配置两个git账户分别用来访问公司仓库和自己的github仓库. 前言: 首先给大家梳理一下多用户名或者说多邮箱使用git的理解误区.我们需要知道的是我们的 ...
- 【封装】Splay
注意确保操作合法性,否则可能陷入死循环 以点权作为排序依据 struct Splay{ #define ls p[u].son[0] #define rs p[u].son[1] #define ma ...
- EMCC13.5+Oracle19.13+Redhat8 In Silent Mode
问题描述:使用静默的方式来安装emcc13.5+Oracle19.13,准备好19c的oracle环境,一开始用21c的库+emcc13.5的安装检查一直过不去,但是19c是没有问题的,具体问题会在下 ...
- jmeter--jsr223组件使用和功能详解
相比于BeanShell 取样器,JSR223取样器具有可大大提高性能的功能(编译)如果需要,一定要使用JSR223取样器编写脚本是更好的选择!!! 属性描述名称:显示的此取样器的描述性名称,可自定义 ...
- 学习tinyriscv(1):安装tinyriscv的工具链
因为毕设是CPU的低功耗设计,所以开始看cpu,打算还是先从这个tinyriscv学起,昨天把环境下好了,第一步是用git去clone代码,这个首先要下载git,然后在目标文件夹鼠标右键,选择&quo ...
- Head First Java学习:第十一章-异常处理
第十一章 异常处理 1.方法可以抓住其他方法所抛出的异常:异常总是丢回给调用方 有风险.会抛出异常的程序代码: 负责声明异常:创建Exception对象并抛出 调用该方法的程序代码: 在try中调用程 ...
- 文心一言 VS 讯飞星火 VS chatgpt (161)-- 算法导论13.1 1题
一.用go语言,按照图 13-1(a)的方式,画出在关键字集合(1,2,-,15)上高度为 3 的完全二叉搜索树.以三种不同方式向图中加入 NIL 叶结点并对各结点着色,使所得的红黑树的黑高分别为2. ...
- STM32CubeMX教程3 GPIO输入 - 按键响应
1.准备材料 开发板(STM32F407G-DISC1) ST-LINK/V2驱动 STM32CubeMX软件(Version 6.10.0) keil µVision5 IDE(MDK-Arm) 2 ...
- k8s卷管理-2
目录 k8s卷管理-2 pv和pvc pv pv的定义 pvc pvc的定义 pv和pvc的绑定关系 手动指定pv与pvc绑定 pod使用pvc pod挂载pvc 访问模式的区别 k8s卷管理-2 之 ...
- MySQL进阶篇:详解索引概述
2.1 MySQL进阶篇:第二章_二.一_索引概述 2.1.1 介绍 索引(index)是帮助MySQL高效获取数据的数据结构(有序).在数据之外,数据库系统还维护着满足 特定查找算法的数据结构,这些 ...