做法:可 进行区间 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 }