CF1582F1. Korney Korneevich and XOR ——dp*

1582F1

题意:

给出一个长度为n的数组,问你有多少个数可以通过其中的某个严格单增子序列xor得到

1 <=n<=1e5, 0 <= ai <= 500.

思路:

用数组b[i][j] = k表示前i个数能凑出来异或和为j时,最后一项最小为k

从1到n遍历a数组,可以把第一维i优化掉b[j] = k表示当前凑出j的最后一项最小为k

每次遍历时先比较b[a[i]] = min(b[a[i]], a[i]);

然后遍历0-511,如果b[j]!=INF && b[j] < a[i]         只有在最后一项k小于a[i]时才可能更新

则更新b[j ^ a[i]],比较其最后一项和a[i]的大小。

注意最大值是511而不是500

// Decline is inevitable,
// Romance will last forever.
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define LL long long
//#define int long long
const int maxn = 2e5 + 10;
const int maxm = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int dx[] = {0, 0, -1, 1}; //{0, 0, 1, 1, 1,-1,-1,-1}
const int dy[] = {1, -1, 0, 0}; //{1,-1, 1, 0,-1, 1, 0,-1}
const int P = 1e9 + 7;
int n;
int a[maxn];
int mx = 511;
int b[520];
void solve() {
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= mx; i++)  b[i] = INF;
    for(int i = 1; i <= n; i++) {
        b[a[i]] = min(b[a[i]], a[i]);
        for(int j = 0; j <= mx; j++) {
            if(b[j] != INF && b[j] < a[i]) {
                b[j ^ a[i]] = min(b[j ^ a[i]], a[i]);
            }
        }
    }
    vector<int> ans;
    for(int i = 0; i <= mx; i++) {
        if(b[i] != INF)
            ans.pb(i);
    }
    cout << ans.size() << endl;
    for(auto i : ans)
        cout << i << ' ';
    cout << endl;
                                  
}
int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//    int T; scanf("%d", &T); while(T--)
//    freopen("1.txt","r",stdin);
//    freopen("2.txt","w",stdout);
//    int T; cin >> T;while(T--)
    solve();
    return 0;
}
/*
 
 
 
 */

猜你喜欢

转载自blog.csdn.net/m0_59273843/article/details/120960679