[ABC261E] Many Operations – 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
We have a variable X and N kinds of operations that change the value of X. Operation i is represented as a pair of integers (Ti,Ai), and is the following operation:
- if Ti=1, it replaces the value of X with and X and Ai;
- if Ti=2, it replaces the value of X with or X or Ai;
- if Ti=3, it replaces the value of X with xor X xor Ai.
Initialize X with the value of C and execute the following procedures in order:
- Perform Operation 1, and then print the resulting value of X.
- Next, perform Operation 1,2 in this order, and then print the value of X.
- Next, perform Operation 1,2,3 in this order, and then print the value of X.
- ⋮
- Next, perform Operation 1,2,…,N in this order, and then print the value of X.
What are and,or,xorand,or,xor?
Input
Input is given from Standard Input in the following format:
N C T1 A1 T2 A2 ⋮ TN AN
Output
Print N lines, as specified in the Problem Statement.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 10 3 3 2 5 1 12 |
9 15 12 |
The initial value of X is 10.
- Operation 1 changes X to 9.
- Next, Operation 1 changes X to 10, and then Operation 2 changes it to 15.
- Next, Operation 1 changes X to 12, and then Operation 2 changes it to 13, and then Operation 3 changes it to 12.
Sample 2
Inputcopy | Outputcopy |
---|---|
9 12 1 1 2 2 3 3 1 4 2 5 3 6 1 7 2 8 3 9 |
0 2 1 0 5 3 3 11 2 |
解析 :
用dp打表,将所有可能的状态都算出来,需要注意,这里如果不使用dp打表可能会超时(dp真的是最强的算法,可以解决的问题很多,效率还高)
f[i][j][k] 表示:c的二进制的第 j 位是 i(0或1),且第 k 次操作所得的结果;
状态转移:
当 t[k]==1 : f[i][j][k]=f[i][j][k-1] & v;
当 t[k]==2:f[i][j][k]=f[i][j][k-1] | v;
当 t[k]==3:f[i][j][k]=f[i][j][k-1] ^v;
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5;
int n, c, t[N], a[N], f[2][35][N];
int main() {
cin >> n >> c;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &t[i], &a[i]);
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j <= 30; j++) {
f[i][j][0] = i;
for (int k = 1; k <= n; k++) {
int v = (a[k] >> j) & 1;
if (t[k] == 1) {
f[i][j][k] = f[i][j][k - 1] & v;
}
else if (t[k] == 2) {
f[i][j][k] = f[i][j][k - 1] | v;
}
else
f[i][j][k] = f[i][j][k - 1] ^ v;
}
}
}
for (int i = 1; i <= n; i++) {
int ans = 0;
for (int j = 0,k=c; j <= 30; j++,k>>=1) {
ans += f[k & 1][j][i] * (1 << j);
}
cout << ans << endl;
c = ans;
}
return 0;
}
原文地址:https://blog.csdn.net/Landing_on_Mars/article/details/134699988
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_7985.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!