From 60a946e6d7a614b2902e1cc9ad04d8d5bd0daafa Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 12 Apr 2025 15:54:13 +0800 Subject: [PATCH] 20250412 finished. --- src/problem/mod.rs | 2 + .../p3272_find_the_count_of_good_integers.rs | 81 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/problem/p3272_find_the_count_of_good_integers.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 7d6ee5d..a9ef7c7 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -593,3 +593,5 @@ mod p2999_count_the_number_of_powerful_integers; mod p3375_minimum_operations_to_make_array_values_equal_to_k; mod p2843_count_symmetric_integers; + +mod p3272_find_the_count_of_good_integers; diff --git a/src/problem/p3272_find_the_count_of_good_integers.rs b/src/problem/p3272_find_the_count_of_good_integers.rs new file mode 100644 index 0000000..35b3b53 --- /dev/null +++ b/src/problem/p3272_find_the_count_of_good_integers.rs @@ -0,0 +1,81 @@ +/** + * [3272] Find the Count of Good Integers + */ +pub struct Solution {} + +// submission codes start here +use std::collections::HashSet; + +impl Solution { + pub fn count_good_integers(n: i32, k: i32) -> i64 { + let n = n as usize; + let k = k as i64; + let base_value = 10i64.pow((n as u32 - 1) / 2); + let length = (n + 1) / 2; + let mut dict = HashSet::new(); + + // 枚举n个数位的回文数 + for mut i in base_value..base_value * 10 { + let mut s = Vec::with_capacity(length); + + while i != 0 { + s.push(i % 10); + i = i / 10; + } + + // 这里拼接回文字符串有点困难 + // 因为上面存数字字符存的是倒序 + // 即 123 -> 3 2 1 + // 所以 12321 -> 1 2 3 2 1 + let mut real_str: Vec = s + .iter() + .rev() + .chain(s.iter().skip(n & 1)) + .map(|x| *x) + .collect(); + + let number = real_str.iter().fold(0, |v, i| v * 10 + *i); + if number % k == 0 { + real_str.sort_unstable(); + dict.insert(real_str); + } + } + + let mut factorial = Vec::with_capacity(n + 1); + factorial.push(1); + for i in 1..=n { + factorial.push(factorial[i - 1] * i as i64); + } + + let mut result = 0; + + for str in dict.into_iter() { + let mut counts = [0; 10]; + for i in str { + counts[i as usize] += 1; + } + + let mut total = (n - counts[0]) as i64 * factorial[n - 1]; + for x in counts { + total /= factorial[x] + } + result += total; + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3272() { + assert_eq!(27, Solution::count_good_integers(3, 5)); + assert_eq!(2, Solution::count_good_integers(1, 4)); + assert_eq!(2468, Solution::count_good_integers(5, 6)); + } +}