没法dp,因为数组开得太大了
2
20
∗
2
20
∗
2
100000
的
空
间
,
没
法
d
p
2^{20}*2^{20}*2^{100000}的空间,没法dp
220∗220∗2100000的空间,没法dp
只能dfs剪枝
目前输入数组的长度最大是1000,否则会超时
extern crate rand;
use rand::{thread_rng, Rng};
pub fn dfs(a: i32, b: i32, arr: &[i32], remain: i32) -> i32 {
let mut remain_copy = remain;
let n = remain.count_ones() as usize;
if n == 1 {
let val = arr[remain.trailing_zeros() as usize];
let t = ((a ^ val) - b).max(a - (b ^ val));
return match t {
i32::MIN..=-1 => -1,
0 => 0,
1..=i32::MAX => 1,
};
}
let mut res = i32::MIN;
for i in 0..n {
let id = remain_copy.trailing_zeros() as usize;
let val = arr[id];
res = res.max(-dfs(b ^ val, a, arr, remain - (1 << id)));
if res == 1 {
return 1;
}
res = res.max(-dfs(b, a ^ val, arr, remain - (1 << id)));
if res == 1 {
return 1;
}
remain_copy &= remain_copy - 1;
}
res
}
fn main() {
let mut rng = thread_rng();
let mut arr = vec![];
for i in 0..1000 {
arr.push(rng.gen_range(0..i32::MAX));
}
// let arr = vec![1, 0];
// let arr = vec![992438, 1006399, 781139, 985280, 4729, 872779, 563580];
println!("{:?}", arr);
println!("{:?}", dfs(0, 0, &arr, (1 << arr.len()) - 1));
// let n = arr.len();
}