5935. 小凯学数学

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
 做法:可 进行区间 dp f[i][j][k]代表 i 到 j 的区间是否可能结果为 k 
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #define N 157
 5 #define rep(i,a,b) for(int i=a;i<=b;i++)
 6 using namespace std;
 7 bool f[N][N][8];
 8 int n,a[N];
 9 
10 void Init(){
11     scanf("%d",&n);
12     rep(i,1,n)    scanf("%d",&a[i]);
13 }
14 
15 void Work(){
16     rep(i,1,n) f[i][i][a[i]]=1;
17     
18     for(int i=n;i>=1;i--)
19         rep(j,i+1,n)
20             rep(k,i,j)    
21                 rep(x,0,7)
22                     rep(y,0,7)
23                         f[i][j][(x+y)/2]=f[i][j][(x+y)/2] | (f[i][k][x]&f[k+1][j][y]);
24 
25     rep(i,0,7)
26         if (f[1][n][i])    printf("%d ",i);
27 }
28 
29 int main(){
30     freopen("math.in","r",stdin);
31     freopen("math.out","w",stdout);
32     Init();
33     Work();
34 }
View Code
 
原文地址:https://www.cnblogs.com/traveller-ly/p/9892459.html