当前位置:网站首页>AtCoder Beginner Contest 261E // 按位思考 + dp
AtCoder Beginner Contest 261E // 按位思考 + dp
2022-07-25 12:31:00 【Jakon_】
题目链接:E - Many Operations (atcoder.jp)
题意:
给定一个数x,以及n个操作(ti,ai):
当 t = 1 时,将 x & a
当 t = 2 时,将 x | a
当 t = 3 时,将 x ^ a
然后分别打印n个数:x进行前1个操作后打印,进一步地再进行前2个操作后打印,... ,进一步地再进行前n个操作后打印。
思路:
由于x、ai 均小于
,每个操作又都是位运算,那么按二进制的每位去思考的话,最多有30个位,每位的初始值只能为0、1,只要先预处理出,每位为初始分别为0、1时,进行前 i 次(1~n)操作后的值,就可以很快地求出任意数进行 前 i 次操作后的值。
代码:
//dp
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
int n, x, t[N], a[N];
bool f[N][30][2]; //fi,j,k:对于二进制位下的第j位,初始为k的情况下,进行前i次操作后的值
int main()
{
cin >> n >> x;
for(int i = 1; i <= n; i++) scanf("%d%d", &t[i], &a[i]);
for(int i = 0; i < 30; i++) f[0][i][0] = 0, f[0][i][1] = 1;
for(int i = 1; i <= n; i++)
for(int j = 0; j < 30; j++)
for(int k = 0; k < 2; k++)
if(t[i] == 1) f[i][j][k] = f[i - 1][j][k] & (a[i] >> j & 1);
else if(t[i] == 2) f[i][j][k] = f[i - 1][j][k] | (a[i] >> j & 1);
else if(t[i] == 3) f[i][j][k] = f[i - 1][j][k] ^ (a[i] >> j & 1);
for(int i = 1; i <= n; i++)
{
int res = 0;
for(int j = 0; j < 30; j++) res += f[i][j][x >> j & 1] << j;
printf("%d\n", x = res);
}
return 0;
}边栏推荐
- 吕蒙正《破窑赋》
- What is ci/cd?
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 2 supplementary problem solution (g, J, K, l)
- 基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
- 2022.07.24 (lc_6125_equal row and column pairs)
- 2022 Henan Mengxin League game (3): Henan University I - Travel
- Detailed explanation of flex box
- 阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?
- go : gin 自定义日志输出格式
- 弹性盒子(Flex Box)详解
猜你喜欢
![[rust] reference and borrowing, string slice type (& STR) - rust language foundation 12](/img/48/7a1777b735312f29d3a4016a14598c.png)
[rust] reference and borrowing, string slice type (& STR) - rust language foundation 12

2022.07.24 (lc_6126_design food scoring system)
![[problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.](/img/00/65eaad4e05089a0f8c199786766396.png)
[problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.

word样式和多级列表设置技巧(二)

Moving Chinese figure liushenglan
![[fluent -- example] case 1: comprehensive example of basic components and layout components](/img/d5/2392d9cb8550aa2692c8b41303d507.png)
[fluent -- example] case 1: comprehensive example of basic components and layout components
What does the software testing process include? What are the test methods?

2022 年中回顾 | 大模型技术最新进展 澜舟科技

cmake 学习使用笔记(二)库的生成与使用

Communication bus protocol I: UART
随机推荐
485 communication (detailed explanation)
想要做好软件测试,可以先了解AST、SCA和渗透测试
论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
The first scratch crawler
mysql实现一张表数据插入另一张表
Leetcode 0133. clone diagram
conda常用命令:安装,更新,创建,激活,关闭,查看,卸载,删除,清理,重命名,换源,问题
Detailed explanation of switch link aggregation [Huawei ENSP]
2022.07.24 (lc_6126_design food scoring system)
Use of hystrix
SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable
State mode
Deep learning MEMC framing paper list
使用vsftpd服务传输文件(匿名用户认证、本地用户认证、虚拟用户认证)
2022河南萌新联赛第(三)场:河南大学 I - 旅行
"Autobiography of Franklin" cultivation
A method to prevent SYN flooding attacks -- syn cookies
启牛开的证券账户安全吗?是怎么开账户的
想要白嫖正则大全是吧?这一次给你个够!
【高并发】通过源码深度分析线程池中Worker线程的执行流程