AtCoder Beginner Contest 169
比赛链接:https://atcoder.jp/contests/abc169/tasks
A - Multiplication 1
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b; cin >> a >> b;
cout << a * b << "\n";
}
B - Multiplication 2
题意
计算 $a_1 \times a_2 \times ... \times a_n$ 。($2≤n≤10^5, 0≤ a_i ≤ 10^{18}$)
题解一
若 $ans$ 乘以某个 $a_i$ 大于 $10^{18}$ 则溢出,即:
$ans \times a_i > 10^{18}$,
为避免运算过程中乘积溢出,移项得:$a_i > \frac{10^{18}}{ans}$ 。
代码
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const ll MAXN = 1e18;
int main() {
int n; cin >> n;
ll a[n] = {};
for (int i = 0; i < n; i++)
cin >> a[i];
if (count(a, a + n, 0)) {
cout << 0 << "\n";
return 0;
}
ll ans = 1;
for (int i = 0; i < n; i++) {
if (a[i] > MAXN / ans) {
cout << -1 << "\n";
return 0;
}
ans *= a[i];
}
cout << ans << "\n";
}
题解二
转化为 __int128 可以避免溢出。
代码
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
const ll MAXN = 1e18;
int main() {
int n; cin >> n;
ll a[n] = {};
for (int i = 0; i < n; i++)
cin >> a[i];
if (count(a, a + n, 0)) {
cout << 0 << "\n";
return 0;
}
ll ans = 1;
for (int i = 0; i < n; i++) {
if ((__int128)ans * a[i] > MAXN) {
cout << -1 << "\n";
return 0;
}
ans *= a[i];
}
cout << ans << "\n";
}
题解三
如果能由下一个积除以 $a_i$ 得到现在的积说明不会发生溢出,但是这个方法需要用 $unsigned\ long\ long$ 才行。
代码
#include <bits/stdc++.h>
using ull = unsigned long long;
using namespace std;
const ull MAXN = 1e18;
int main() {
int n; cin >> n;
ull a[n] = {};
for (int i = 0; i < n; i++)
cin >> a[i];
if (count(a, a + n, 0)) {
cout << 0 << "\n";
return 0;
}
ull ans = 1;
for (int i = 0; i < n; i++) {
ull next = ans * a[i];
if (next / a[i] != ans or next > MAXN) {
cout << -1 << "\n";
return 0;
}
ans *= a[i];
}
cout << ans << "\n";
}
题解四
使用 python 中自带的大数运算。
代码
def main():
N = int(input())
A = list(map(int, input().split())) if 0 in A:
print(0)
return ans = 1
for a in A:
ans *= a
if ans > int(1e18):
print(-1)
return print(ans) main()
C - Multiplication 3
题意
$a$ 为整数,$b$ 为保留小数点后两位的小数,计算 $a \times b$ 。($0≤a≤10^{15}, 0≤ b < 10$)
题解
把 $b$ 转化为整数再计算。
代码
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ll a; string s; cin >> a >> s;
s.erase(1, 1);
ll b = stoll(s);
cout << a * b / 100;
}
D - Div Game
题意
给出一个正整数 $n$,每次操作可以选择一个正整数 $z$,要求:
- $z = p^e$,$p$ 为正素数,$e$ 为正数
- $z$ 整除 $n$
- $z$ 不同于任一之前选择的 $z$
- 如果以上条件满足,$n = \frac{n}{z}$
找出最多可以进行多少次操作。
题解
找出每个质因数的个数,依次减去 $1,2,3...$ 即可。
代码
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ll n; cin >> n;
map<ll, ll> mp;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
++mp[i];
n /= i;
}
}
if (n != 1) ++mp[n];
ll ans = 0;
for (auto i : mp) {
for (int j = 1; j <= i.second; j++) {
i.second -= j;
++ans;
}
}
cout << ans << "\n";
}
E - Count Median
题意
有 $n$ 个数 $x_1,x_2,...,x_n$,$a_i≤x_i≤b_i$,找出中值 $x_{mid}$ 可能的值的个数。
- 中值为将所有 $x_i$ 排序,位于中间的 $x_i$
- 如果 $n$ 为奇数,中值为 $x_{(n+1)/2}$
- 如果 $n$ 为偶数,中值为 $(x_{n/2} + x_{n/2 + 1}) / 2$
题解
中值的最小值为 $a_i$ 的中值,最大值为 $b_i$ 的中值,中值的个数即:$b_{mid} - a_{mid} + 1$ 。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
int n; cin >> n;
int a[n] = {}, b[n] = {};
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
sort(a, a + n);
sort(b, b + n);
int mi = 0, mx = 0;
if (n & 1) {
mi = a[n / 2];
mx = b[n / 2];
} else {
mi = a[n / 2 - 1] + a[n / 2];
mx = b[n / 2 - 1] + b[n / 2];
}
cout << mx - mi + 1;
}
F - Knapsack for All Subsets
题意
有 $n$ 个数 $a_1,a_2,...,a_n$,计算 ${1,2,...,n}$ 的 $2^n - 1$ 个非空子集有多少子集作为下标求和可以得到 $s$ 。
题解
$dp_i$ 表示和为 $i$ 的集合共有多少个。
每次添加一个数 $x$,会产生两种变化:
- $x$ 与 $dp_i$ 中的每个集合构成的新集合都可以构成 $i + x$:$dp_{i + x}\ +=\ dp_i$
- $x$ 与 $dp_i$ 中的每个集合构成了一个包含原先集合的新集合:$dp_i\ *=\ 2$
代码
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int dp[6010];
int main() {
int n, s; cin >> n >> s;
dp[0] = 1;
for (int i = 0; i < n; i++) {
int x; cin >> x;
for (int j = s; j >= 0; j--) {
(dp[j + x] += dp[j]) %= mod;
(dp[j] *= 2) %= mod;
}
}
cout << dp[s] << "\n";
}
AtCoder Beginner Contest 169的更多相关文章
- AtCoder Beginner Contest 169 题解
AtCoder Beginner Contest 169 题解 这场比赛比较简单,证明我没有咕咕咕的时候到了! A - Multiplication 1 没什么好说的,直接读入两个数输出乘积就好了. ...
- 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 ...
随机推荐
- LeetCode108 将有序数组转为二叉搜索树
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...
- 【C++】《C++ Primer 》第九章
第九章 顺序容器 一.顺序容器概述 顺序容器(sequential container):为程序员提供了控制元素存储和访问顺序的能力.这种顺序不依赖于元素的值,而是与元素加入容器时的位置相对应. 不同 ...
- 系统吞吐量与QPS/TPS
QPS/TPS QPS:Queries Per Second意思是"每秒查询率",是一台服务器每秒能够相应的查询次数,是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准. ...
- 【Java】运算符(算术、赋值、比较(关系)、逻辑、条件、位运算符)
运算符 文章目录 运算符 1. 算术运算符 2. 赋值运算符 3. 比较运算符 4. 逻辑运算符 5. 条件运算符 6. 位运算符 7. 运算符优先级 8. 运算符操作数类型说明 9.code 算术运 ...
- 【Linux】saltstack 安装及简单使用
准备三台server,一台为master(10.96.20.113),另两台为minion(10.96.20.117,10.96.20.118) 主机名(master.minion1.minion2) ...
- LeetCode617. 合并二叉树
题目 1 class Solution { 2 public: 3 TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { 4 if(!t1 && ...
- 入门OJ:最短路径树入门
题目描述 n个城市用m条双向公路连接,使得任意两个城市都能直接或间接地连通.其中城市编号为1..n,公路编号为1..m.任意个两个城市间的货物运输会选择最短路径,把这n*(n-1)条最短路径的和记为S ...
- Android事件分发机制二:viewGroup与view对事件的处理
前言 很高兴遇见你~ 在上一篇文章 Android事件分发机制一:事件是如何到达activity的? 中,我们讨论了触摸信息从屏幕产生到发送给具体 的view处理的整体流程,这里先来简单回顾一下: 触 ...
- Vue的核心思想
Vue的核心思想主要分为两部分: 1.数据驱动 2.组件系统 1.数据驱动 在传统的前端交互中,我们是通过Ajax向服务器请求数据,然后手动的去操作DOM元素,进行数据的渲染,每当前端数据交互变化时 ...
- Atlas 2.1.0 实践(3)—— Atlas集成HIve
Atlas集成Hive 在安装好Atlas以后,如果想要使用起来,还要让Atlas与其他组件建立联系. 其中最常用的就是Hive. 通过Atlas的架构,只要配置好Hive Hook ,那么每次Hiv ...