[区间DP]JZOJ 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
 

Data Constraint

​ 30% n<=10 0<=a[i]<=7
70% n<=150 0<=a[i]<=3
100% n<=150 0<=a[i]<=7

NOIP2017TG背景。。。

那个位运算式子相信都知道是a+b>>1吧

然后最早想的区间DP,是对的

(可我算时间会爆炸?玄学时间复杂度O(能过))

设f[l][r][k]表示l~r能否合成k

转移显然(我错了),枚举断点和a,b就行

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=160;
int n;
int a[N],f[N][N][8],ans[N];

int main() {
    freopen("math.in","r",stdin);
    freopen("math.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]),f[i][i][a[i]]=1;
    for (int len=2;len<=n;len++)
        for (int l=1;l<=n-len+1;l++) {
            int r=l+len-1;
            for (int i=l;i<=r;i++)
                for (int a1=0;a1<8;a1++)
                    for (int a2=0;a2<8;a2++)
                        f[l][r][a1+a2>>1]|=f[l][i][a1]&f[i+1][r][a2];
        }
    for (int i=0;i<8;i++)
        if (f[1][n][i]) printf("%d ",i);
}
View Code