当前位置:网站首页>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;
}边栏推荐
- 阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?
- Pytorch environment configuration and basic knowledge
- [problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.
- “蔚来杯“2022牛客暑期多校训练营2 补题题解(G、J、K、L)
- Crawler crawls dynamic website
- Keeping MySQL highly available
- Numpy first acquaintance
- shell基础知识(退出控制、输入输出等)
- 【2】 Grid data display stretch ribbon (take DEM data as an example)
- 零基础学习CANoe Panel(13)—— 滑条(TrackBar )
猜你喜欢

2022.07.24(LC_6126_设计食物评分系统)

想要做好软件测试,可以先了解AST、SCA和渗透测试

Pytorch advanced training skills

The first scratch crawler

More accurate and efficient segmentation of organs-at-risk in radiotherapy with Convolutional Neural

基于JEECG制作一个通用的级联字典选择控件-DictCascadeUniversal

PyTorch进阶训练技巧

485 communication (detailed explanation)

卷积核越大性能越强?一文解读RepLKNet模型

Clickhouse notes 03-- grafana accesses Clickhouse
随机推荐
mysql实现一张表数据插入另一张表
微软提出CodeT:代码生成新SOTA,20个点的性能提升
Crawler crawls dynamic website
flinkcdc可以一起导mongodb数据库中的多张表吗?
More accurate and efficient segmentation of organs-at-risk in radiotherapy with Convolutional Neural
Fiddler packet capturing app
2022.07.24 (lc_6126_design food scoring system)
业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
艰辛的旅程
clickhouse笔记03-- Grafana 接入ClickHouse
2022.07.24 (lc_6125_equal row and column pairs)
1.1.1 welcome to machine learning
Implementation of recommendation system collaborative filtering in spark
2022.07.24(LC_6125_相等行列对)
[ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro
Detailed explanation of switch link aggregation [Huawei ENSP]
Is the securities account opened by qiniu safe? How to open an account
R language uses wilcox The test function performs Wilcox signed rank test to obtain the confidence interval of the population median (the default output result includes the confidence interval of 95%
Plus SBOM: assembly line BOM pbom
Leetcode 0133. clone diagram