1. 程式人生 > >JZOJ-senior-5935. 【NOIP2018模擬10.29】小凱學數學

JZOJ-senior-5935. 【NOIP2018模擬10.29】小凱學數學

Time Limits: 1000 ms Memory Limits: 262144 KB

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

Solution

O ( n

3 m 2 ) O(n^3m^2) DP

Code

#include<algorithm>
#include<cstdio>

#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i) using namespace std; const int N=155,M=10; int n,a[N],f[N][N][M]; int get(int a,int b) { return ((a&b)+(a|b))>>1; } int main() { freopen("math.in","r",stdin); freopen("math.out","w",stdout); scanf("%d",&n); fo(i,1,n) scanf("%d",&a[i]),f[i][i][a[i]]=1; fo(len,1,n-1) fo(l,1,n-len) { int r=l+len; fo(k,l,r-1) fo(i,0,7) if(f[l][k][i]) fo(j,0,7) if(f[k+1][r][j]) f[l][r][get(i,j)]=1; } fo(i,0,M) if(f[1][n][i]) printf("%d ",i); }