From 07927a43925167203b78e7c184cf06d2d1a79ef5 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 5 Apr 2025 15:11:45 +0800 Subject: [PATCH] 20250405 finished. --- src/problem/mod.rs | 2 + .../p1863_sum_of_all_subset_xor_totals.rs | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/problem/p1863_sum_of_all_subset_xor_totals.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 4af1c24..32d1d7a 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -580,3 +580,5 @@ mod p2873_maximum_value_of_an_ordered_triplet_i; mod p2874_maximum_value_of_an_ordered_triplet_ii; mod p1123_lowest_common_ancestor_of_deepest_leaves; + +mod p1863_sum_of_all_subset_xor_totals; diff --git a/src/problem/p1863_sum_of_all_subset_xor_totals.rs b/src/problem/p1863_sum_of_all_subset_xor_totals.rs new file mode 100644 index 0000000..cae7070 --- /dev/null +++ b/src/problem/p1863_sum_of_all_subset_xor_totals.rs @@ -0,0 +1,43 @@ +/** + * [1863] Sum of All Subset XOR Totals + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn subset_xor_sum(nums: Vec) -> i32 { + let mut result = 0; + + Self::search(0, 0, &nums, &mut result); + + result + } + + fn search(mut sum: i32, i: usize, nums: &Vec, result: &mut i32) { + if i == nums.len() { + return; + } + + // 不选择i + Self::search(sum, i + 1, nums, result); + // 选择i + sum = sum ^ nums[i]; + *result += sum; + Self::search(sum, i + 1, nums, result); + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1863() { + assert_eq!(6, Solution::subset_xor_sum(vec![1, 3])); + assert_eq!(28, Solution::subset_xor_sum(vec![5, 1, 6])); + assert_eq!(480, Solution::subset_xor_sum(vec![3, 4, 5, 6, 7, 8])); + } +}