Description
由于小凯上次在找零问题上的疑惑,给大家在考场上带来了很大的麻烦,他决心好好学习数学
本次他挑选了位运算专题进行研究 他发明了一种叫做“小凯运算”的运算符:
a$b =( (a&b) + (a|b) )>>1
他为了练习,写了n个数在黑板上(记为a[i]) 并对任意相邻两个数进行“小凯运算”,把两数擦去,把结果留下 这样操作n-1次之后就只剩了1个数,求这个数可能是什么?
将答案从小到大顺序输出
Input
4
1 4 3 2
Output
1 2
Sample Input
4
1 4 3 2
Sample Output
1 2
Data Constraint
30% n<=10 0<=a[i]<=7
70% n<=150 0<=a[i]<=3
100% n<=150 0<=a[i]<=7
题解
先分析一下题目给出的运算,
a $ b =( (a&b) + (a|b) )>>1
观察二进制位置上面,就是一个交集再加上一个并集,其实就是a+b嘛,
所以呢,这个东西就是(a+b)/2 。
知道了这个性质,出来起来就比较方便了,
设
表示从i到j这一段,是否最终是否可以变为一个数k,
转移就先枚举一个区间[i,j],然后枚举一个分界点,将一个区间分成两个区间,
最后再枚举这两个区间最终是什么数字。
code
#include <cstdio>
#include <iostream>
#include <cstring>
#define N 153
#define P putchar
#define G getchar
using namespace std;
char ch;
void read(int &n)
{
n=0;
ch=G();
while((ch<'0' || ch>'9') && ch!='-')ch=G();
int w=1;
if(ch=='-')w=-1,ch=G();
while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
n*=w;
}
int n,x;
bool f[8][N][N];
int main()
{
freopen("math.in","r",stdin);
freopen("math.out","w",stdout);
memset(f,0,sizeof(f));
read(n);
for(int i=1;i<=n;i++)read(x),f[x][i][i]=1;
for(int l=2;l<=n;l++)
for(int i=1;i+l-1<=n;i++)
for(int j=i+1;j<=n;j++)
for(int p1=0;p1<8;p1++)
for(int p2=0;p2<8;p2++)
f[(p1+p2)>>1][i][l+i-1]|=f[p1][i][j-1]&&f[p2][j][i+l-1];
for(int i=0;i<8;i++)
if(f[i][1][n])P(i+48),P(' ');
return 0;
}