Codeforces Round #499 (Div. 1)部分题解(B,C,D)
Codeforces Round #499 (Div. 1)
这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解。
B.Rocket
极其简单的一道交互题,有些位置会说反的,那么就选一个数来询问直接选出所有的这样的位置
显然,选择\(\rm 1\)和\(\rm m\)都可以,选择完之后直接二分就行了
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=41;
int n,m,now,flag;bool vis[maxn];
int check(int x,int now){
int g;
printf("%d\n",x),fflush(stdout),read(g);
if(vis[now])g=-g;return g;
}
int main(){
read(m),read(n);
for(rg int i=1;i<=n;i++){
printf("%d\n",m),fflush(stdout);read(now);
if(now==0)return 0;
else if(now==-1)vis[i]=1;
}
int l=1,r=m;now=1;
while(l<=r){
int mid=(l+r)>>1,f=check(mid,now);
if(!f)return 0;
if(f==-1)l=mid+1;else r=mid-1;
now++;if(now==n+1)now=1;
}
}
C.Border
这题其实也挺显然的,首先我们肯定只用管每个数\(\rm k\)进制下的最后一位的值,思考一下组合的过程,发现对于\(\rm x,y\),似乎\(\rm gcd(k,x,y)\)的倍数都能够被组合出来,然后大胆猜测这是对的,那么所有数和\(\rm k\)的最大公约数的答案就是能够组成的数
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e5+10;
int n,m,a[maxn],now;
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int main(){
read(n),read(m),now=m;
for(rg int i=1;i<=n;i++)read(a[i]),a[i]=a[i]%m,now=gcd(now,a[i]);
if(!now)return printf("%d\n%d\n",1,0),0;
printf("%d\n",m/now);
for(rg int i=0;i<m/now;i++)printf("%d ",i*now);
}
D.Mars rover
确实也不难,差不多是一眼秒的题
由于每次都只修改一个点,然后我们发现对于这次修改,只要它到根的路径上只要有一个点不受这次变化的影响,那么根节点就不会受到影响。
那么我们可以发现这个过程可以从上往下做,对于每一个有两个儿子的点,如果它的左儿子取反后它的值不受影响,那么根节点也不会受到影响,也就是说它的左儿子这个子树内的所有叶子节点的取反对根节点都没有影响,右儿子亦然
这样我们就可以\(O(n)\)扫一遍先算出初始树上的节点权值,然后在\(O(n)\)扫一遍得出每个叶子节点对根节点是否有影响就行了
总时间复杂度\(O(n)\)
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=1e6+10;
int n,m,f[maxn],ans,a[maxn],l[maxn][3];
char s[maxn][5];
bool vis[maxn];
void dfs(int x){
if(l[x][0]==1){
dfs(l[x][1]);
f[x]=!f[l[x][1]];
}
else if(l[x][0]==2){
dfs(l[x][1]),dfs(l[x][2]);
if(s[x][1]=='A')f[x]=f[l[x][1]]&f[l[x][2]];
if(s[x][1]=='O')f[x]=f[l[x][1]]|f[l[x][2]];
if(s[x][1]=='X')f[x]=f[l[x][1]]^f[l[x][2]];
}
}
void solve(int x,int now){
vis[x]=now;
if(l[x][0]==1)solve(l[x][1],now);
else if(l[x][0]==2){
if(s[x][1]=='A'){
int now1=(!f[l[x][1]])&f[l[x][2]],now2=(!f[l[x][2]])&f[l[x][1]];
if(now1!=f[x])solve(l[x][1],now);
else solve(l[x][1],0);
if(now2!=f[x])solve(l[x][2],now);
else solve(l[x][2],0);
}
if(s[x][1]=='O'){
int now1=(!f[l[x][1]])|f[l[x][2]],now2=(!f[l[x][2]])|f[l[x][1]];
if(now1!=f[x])solve(l[x][1],now);
else solve(l[x][1],0);
if(now2!=f[x])solve(l[x][2],now);
else solve(l[x][2],0);
}
if(s[x][1]=='X'){
int now1=(!f[l[x][1]])^f[l[x][2]],now2=(!f[l[x][2]])^f[l[x][1]];
if(now1!=f[x])solve(l[x][1],now);
else solve(l[x][1],0);
if(now2!=f[x])solve(l[x][2],now);
else solve(l[x][2],0);
}
}
}
int main(){
read(n);
for(rg int i=1;i<=n;i++){
scanf("%s",s[i]+1);
if(s[i][1]=='A'||s[i][1]=='O'||s[i][1]=='X')
l[i][0]=2,read(l[i][1]),read(l[i][2]);
else if(s[i][1]=='N')l[i][0]=1,read(l[i][1]);
else l[i][0]=0,read(f[i]);
}
dfs(1),solve(1,1);
for(rg int i=1;i<=n;i++)
if(s[i][1]=='I'){
if(vis[i])printf("%d",!f[1]);
else printf("%d",f[1]);
}
}
Codeforces Round #499 (Div. 1)部分题解(B,C,D)的更多相关文章
- Codeforces Round #499 (Div. 1)
Codeforces Round #499 (Div. 1) https://codeforces.com/contest/1010 为啥我\(\rm Div.1\)能\(A4\)题还是\(\rm s ...
- Codeforces Round #499 (Div. 2)
Codeforces Round #499 (Div. 2) https://codeforces.com/contest/1011 A #include <bits/stdc++.h> ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
随机推荐
- java try中包含return语句,finally中的return语句返回顺序
//结论: finally 中的代码比 return 和 break 语句后执行 public static void main(String[] args) { int x=new Test.tes ...
- STL memory.cpp
memory.cpp # // Filename: memory # # // Comment By: 凝霜 # // E-mail: mdl2009@vip.qq.com # // Blog: ht ...
- POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- python之路-进程
博客园 首页 新随笔 联系 管理 订阅 随笔- 31 文章- 72 评论- 115 python之路——进程 阅读目录 理论知识 操作系统背景知识 什么是进程 进程调度 进程的并发与并行 ...
- Java精度计算与舍入
用到的类: 类 BigDecimal:不可变的.任意精度的有符号十进制数.BigDecimal 由任意精度的整数非标度值 和 32 位的整数标度 (scale) 组成.如果为零或正数,则标度是小数点后 ...
- 资料:MVC框架+SQL Server 数据集成引擎
ylbtech-资料:MVC框架+SQL Server 数据集成引擎 1.返回顶部 1. 功能特点: MVC框架耦合性低视图层和业务层分离,这样就允许更改视图层代码而不用重新编译模型和控制器代码,同样 ...
- 愚人的linux内核2440移植札记(超曲折版)
http://blog.csdn.net/dreambegin/article/details/6904822 原来文章叫--编译内核之初体验.后来想了想,这篇文章让我体验了好多遍.不该叫这么大气的名 ...
- Ruby代码块(Block)
1.什么是代码块 在Ruby中,{}或do...end之间的代码是一个代码块.代码块只能出现在一个方法的后边,它紧接在方法最后一个参数的同一行上,由yield关键字调用.例如: [1,2,3,4,5] ...
- Mac搭建nginx+rtmp服务器
nginx是非常优秀的开源服务器,用它来做hls或者rtmp流媒体服务器是非常不错的选择,本人在网上整理了安装流程,分享给大家并且作备忘. 一.安装Homebrow 已经安装了brow的可以直接跳过这 ...
- ASP.NET Core 中文文档 第四章 MVC(2.2)模型验证【转载】
http://www.cnblogs.com/dotNETCoreSG/p/aspnetcore-4_2_2-validation.html 介绍模型验证 在一个应用程序将数据存储到数据库之前,这个应 ...