AtCoder Beginner Contest 169 题解
AtCoder Beginner Contest 169 题解
这场比赛比较简单,证明我没有咕咕咕的时候到了!
A - Multiplication 1
没什么好说的,直接读入两个数输出乘积就好了。
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<a*b;
return 0;
}
B - Multiplication 2
让你连乘,同时在答案过大(大于\(10^{18}\))的时候输出-1。
你可以使用__int128_t。这是一种神奇的类型,可以用上足足128个位,绰绰有余。记得特判乘积是\(0\)的情况。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll a[100005];
__int128_t ans;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
ans=1;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]==0){
cout<<0<<endl;
return 0;
}
}
for(int i=0;i<n;i++){
ans*=a[i];
if(ans>1000000000000000000ll){
cout<<-1<<endl;
return 0;
}
}
cout<<(ll)ans<<endl;
return 0;
}
C - Multiplication 3
给你一个整数、一个固定位数的小数,让你计算他们的乘积并舍去小数点后的部分。
由于long long存得下,直接把\(B\)乘以\(100\)化为整数与\(A\)相乘,然后在输出前整除\(100\)即可。最初读入的小数要注意精度问题。
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull a,b;
string s;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>a>>s;
b=(s[0]-'0')*100+(s[2]-'0')*10+s[3]-'0';
cout<<a*b/100<<endl;
return 0;
}
D - Div Game
给你一个数\(N\),每次让你把它除以一个质数的幂(指数为正整数)。最多可以除以多少个不同的数(操作之间互相影响)。
把\(N\)分解质因数,注意到不同的质因子之间操作是互不影响的。那么我们可以把操作归类到多个质数的幂上。
对于一个单独的质数的幂,显然除以这个质数的一次幂,二次幂,三次幂……这么操作下去是最优的,于是我们可以得到这样的程序:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
vector<pair<ll,int>> dvs;//dvs以配对<质数,幂次>的形式把N分解了质因数(其实质数本身不用存)
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(ll i=2;i*i<=n;i++){
if(n%i==0){
dvs.push_back(make_pair(i,0));
while(n%i==0){
dvs.back().second++;
n/=i;
}
}
}
if(n>1){
dvs.push_back(make_pair(n,1));
}//以上是分解质因数环节
int ans=0;
for(pair<ll,int> &p:dvs){
int i=1;
for(;(1+i)*i/2<=p.second;i++);
i--;//求出操作中最大的那一个数是质数的多少次幂,用到等差数列求和公式
ans+=i;
}
cout<<ans<<endl;
return 0;
}
E - Count Median
给你\(N\)个数,每个的范围在\(A_i\)到\(B_i\)之间。问你中位数有多少种取值可能。特别地,\(N\)为偶数时,中位数是中间两个数的平均数(可能有\(0.5\)的出现)。
容易证明,最小和最大的中位数中间的所有可能的中位数都是一定可以取到的。
现在我们只要求出最小和最大的中位数就好,这个可以贪心地取\(A_i\)或\(B_i\)做到。
程序中使用了一些位运算,可以参考下列文章:
基本位运算芝士:https://blog.csdn.net/jason314/article/details/5719933
运算符的优先级:https://blog.csdn.net/nicky_zs/article/details/4053146
#include<bits/stdc++.h>
using namespace std;
int n,a[200005],b[200005];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i];
}
sort(a+1,a+1+n);
sort(b+1,b+1+n);
if(n&1){//n为奇数时
cout<<b[n+1>>1]-a[n+1>>1]+1<<endl;//最后的+1是为了把答案从左闭右开或左开右闭区间转换为闭区间
}else{//n为偶数时
cout<<b[n>>1]+b[(n>>1)+1]-a[n>>1]-a[(n>>1)+1]+1<<endl;//这里除了最后的+1,前面的被我乘以2计算了,方便写代码
}
return 0;
}
F - Knapsack for All Subsets
能做到F的应该不需要听题意了吧。你个懒猪!写题解还这么懒就给我去**啦!哼!
我们可能会走入一种误区,就是求出集合内元素恰好和是\(S\)的这些集合,然后再去求包含它的这些集合的个数,然而是错的。(也可能是我没有想到,至少它没那么好写(逃))
假如直接按照题目的意思去考虑,可以想到一种很方便的DP方式,那就是dp[i][j]表示大小小于等于i,有至少一个子集的元素和为\(j\)的集合个数。
这样,加入\(A\)中元素的顺序就和这个DP状态无关了,我们可以先把\(i\)的顺序确定下来:从\(1\)到\(N\)遍历数组\(A\)。\(j\)更简单,从小到大枚举即可。
那么转移方程怎么得到呢?首先,对应第\(i\)位的元素\(A_i\)在“元素和为\(j\)的子集”中选不选,都可以加上第一维减一的状态的答案。(假如之前就有一种子集和为\(j\)的选法,那么这个元素对于选法的贡献就不重要了)
然后,假如选了这个元素进“元素和为\(j\)的子集”,就又要加上第一维减一,第二位减去\(A_i\)的状态的答案。方程就可以得到了:
\]
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
int n,s,a[3005];
ll dp[3005][3005];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
dp[0][0]=1;
for(int i=1;i<=n;i++){
cin>>a[i];
for(int j=0;j<=s;j++){
dp[i][j]=dp[i-1][j]*2%mod;
if(j>=a[i])dp[i][j]=(dp[i][j]+dp[i-1][j-a[i]])%mod;
}
}
cout<<dp[n][s]<<endl;
return 0;
}
AtCoder Beginner Contest 169 题解的更多相关文章
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 153 题解
目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...
- AtCoder Beginner Contest 177 题解
AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...
- AtCoder Beginner Contest 184 题解
AtCoder Beginner Contest 184 题解 目录 AtCoder Beginner Contest 184 题解 A - Determinant B - Quizzes C - S ...
- AtCoder Beginner Contest 173 题解
AtCoder Beginner Contest 173 题解 目录 AtCoder Beginner Contest 173 题解 A - Payment B - Judge Status Summ ...
- AtCoder Beginner Contest 172 题解
AtCoder Beginner Contest 172 题解 目录 AtCoder Beginner Contest 172 题解 A - Calc B - Minor Change C - Tsu ...
- AtCoder Beginner Contest 148 题解
目录 AtCoder Beginner Contest 148 题解 前言 A - Round One 题意 做法 程序 B - Strings with the Same Length 题意 做法 ...
- AtCoder Beginner Contest 151 题解报告
总的来说,这次的题目比较水,然而菜菜的我并没有把所有题目都做完,话不多说,直接来干货: A:Next Alphabet 题目链接:https://atcoder.jp/contests/abc151/ ...
- AtCoder Beginner Contest 115 题解
题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit ...
随机推荐
- 关于CSS的粘性定位sticky失效问题
CSS的粘性定位sticky可以起到吸顶灯的作用,用法如下 <body> <div> <nav style="postion:sticky; top: 0;&q ...
- ServerBootstrap的handler()和childHandler()区别
无论服务端还是客户端都进行了handler的设置,通过添加hanlder,我们可以监听Channel的各种动作以及状态的改变,包括连接,绑定,接收消息等. 区别: 1. handler在初始化时就会执 ...
- 洛谷 P4548 - [CTSC2006]歌唱王国(概率生成函数)
洛谷题面传送门 PGF 入门好题. 首先介绍一下 PGF 的基本概念.对于随机变量 \(X\),满足 \(X\) 的取值总是非负整数,我们即 \(P(v)\) 表示 \(X=v\) 的概率,那么我们定 ...
- 【R】如何去掉数据框中包含非数值的行?
目录 1. 去掉指定列中包含NA/Inf/NaN的行 2. 去掉指定列中包含其他乱七八糟字符串的行 3. 去掉整个数据框中包含非数值的行 只包含NA.NaN和Inf的情况 针对其他字符情况 4. 总结 ...
- 面向对象编程—self,继承
目录 1. self 2. init 2.1 使用方式 2.2 init()方法的调用 2.3 总结 3. 继承 3.1 继承的概念 3.2 继承示例 3.2.1 说明 3.3 总结 3.4 多继承 ...
- 巩固javaweb第十二天
巩固内容: HTML 图像- 图像标签( <img>)和源属性(Src) 在 HTML 中,图像由<img> 标签定义. <img> 是空标签,意思是说,它只包含属 ...
- Typora数学公式输入指导手册
Markdown 公式指导手册 公式大全的链接 https://www.zybuluo.com/codeep/note/163962#mjx-eqn-eqsample 目录 Markdown 公式指导 ...
- day13 cookie与session和中间件
day13 cookie与session和中间件 今日内容概要 cookie与session简介 django操作cookie与session django中间件简介 如何自定义中间件 csrf跨站请 ...
- 【leetcode】337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this a ...
- 容器之分类与各种测试(四)——unordered_set和unordered_map
关于set和map的区别前面已经说过,这里仅是用hashtable将其实现,所以不做过多说明,直接看程序 unordered_set #include<stdexcept> #includ ...