描述
链接:http://uoj.ac/problem/2
21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳。作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争。通过研究相关文献,他找到了该病的发病原因:在深邃的太平洋海底中,出现了一条名为 drd 的巨龙,它掌握着睡眠之精髓,能随意延长大家的睡眠时间。正是由于 drd 的活动,起床困难综合症愈演愈烈,以惊人的速度在世界上传播。为了彻底消灭这种病,atm 决定前往海底,消灭这条恶龙。
历经千辛万苦,atm 终于来到了 drd 所在的地方,准备与其展开艰苦卓绝的战斗。drd 有着十分特殊的技能,他的防御战线能够使用一定的运算来改变他受到的伤害。具体说来,drd 的防御战线由
n
n
n 扇防御门组成。每扇防御门包括一个运算
o
p
op
op 和一个参数
t
t
t,其中运算一定是
O
R
,
X
O
R
,
A
N
D
OR,XOR,AND
OR,XOR,AND 中的一种,参数则一定为非负整数。如果还未通过防御门时攻击力为
x
x
x,则其通过这扇防御门后攻击力将变为
x
o
p
t
x op t
xopt。最终drd 受到的伤害为对方初始攻击力
x
x
x 依次经过所有
n
n
n扇防御门后转变得到的攻击力。
由于 atm 水平有限,他的初始攻击力只能为
0
0
0 到
m
m
m 之间的一个整数(即他的初始攻击力只能在
0
,
1
,
…
,
m
0,1,…,m
0,1,…,m 中任选,但在通过防御门之后的攻击力不受
m
m
m 的限制)。为了节省体力,他希望通过选择合适的初始攻击力使得他的攻击能让 drd 受到最大的伤害,请你帮他计算一下,他的一次攻击最多能使 drd 受到多少伤害。
解析
很有意思的题目,想到二进制拆位的话,思路就很清晰了.
虽然我由于不知名的原因一直50分
我们按位运算,因为每一位的运算是独立的,而每一位只可能是
0
0
0或
1
1
1.
只需要注意几点即可
- 原数第 k k k位如果是 0 0 0,出来的结果是 1 1 1,我们就存下来
- 原数的第 k k k位如果是 1 1 1,出来的结果是 1 1 1,且原数小于m,我们就存下来.
97分代码
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <cstring>
#include <cmath>
#include <algorithm>
#ifndef NULL
#define NULL 0
#endif
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int mod = 10007;
int n, m;
struct vec {
int t;
char op[10];
int check(int i, int now) {
int temp = 1 & (this->t >> i);
if (op[0] == 'A')
now &= temp;
else if (op[0] == 'O')
now |= temp;
else
now ^= temp;
return now;
}
}p[MAXN];
bool check(int i, int now) {
for (vec x : p)
now = x.check(i, now);
return now;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> p[i].op >> p[i].t;
ll ans=0;
for (int i = 30; i >= 0; i--) {
int k = (1 << i);
if (check(i, 0))
ans += k;
else if (m > k && check(i, 1))
ans += k, m -= k;
}
printf("%lld\n", ans);
return 0;
}
不知道是哪位带佬出的hack数据,
2 13983
XOR 12
XOR 12
答案是13983
同样的思路,不同的做法,我们可以将
11111...
11111...
11111...和
0
0
0分别扔进去,那么分别将出来的非
0
0
0的位加起来,就是我们的答案.
同样,优先级
0
0
0>
1
1
1.
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <cstring>
#include <cmath>
#include <algorithm>
#ifndef NULL
#define NULL 0
#endif
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int mod = 10007;
int n, m;
struct vec {
int t;
char op[10];
int check(int now) {
if (op[0] == 'A')
now &= t;
else if (op[0] == 'O')
now |= t;
else
now ^= t;
return now;
}
}p[MAXN];
int main() {
int x = 0, y = (1LL << 31) - 1;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> p[i].op >> p[i].t;
x = p[i].check(x);
y = p[i].check(y);
}
ll ans=0;
for (int i = 29; i >= 0; i--) {
int k = 1 << i;
if ((k&x) == 0&& (k&y) == 0)
continue;
if ((k&x) > 0)
ans += k;
else if ((k&y) > 0&&m>k) {
m -= k;
ans += k;
}
}
printf("%lld\n", ans);
return 0;
}
这里
i
i
i如果从
30
30
30开始算,额外数据就是错的,
29
29
29开始算,额外数据就能过.
e
m
m
m
m
emmmm
emmmm