From 11e11afcd152285e85e0f136efe96cdc9bbbd68b Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 22 Nov 2024 11:41:28 +0800 Subject: [PATCH] 20241122 finished. --- src/problem/mod.rs | 2 + ..._count_of_numbers_which_are_not_special.rs | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/problem/p3233_find_the_count_of_numbers_which_are_not_special.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 41687e7..c6d3369 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -330,3 +330,5 @@ mod p3243_shortest_distance_after_road_addition_queries_i; mod p3244_shortest_distance_after_road_addition_queries_ii; mod p3248_snake_in_matrix; + +mod p3233_find_the_count_of_numbers_which_are_not_special; diff --git a/src/problem/p3233_find_the_count_of_numbers_which_are_not_special.rs b/src/problem/p3233_find_the_count_of_numbers_which_are_not_special.rs new file mode 100644 index 0000000..595b713 --- /dev/null +++ b/src/problem/p3233_find_the_count_of_numbers_which_are_not_special.rs @@ -0,0 +1,43 @@ +/** + * [3233] Find the Count of Numbers Which Are Not Special + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn non_special_count(l: i32, r: i32) -> i32 { + let n = (r as f64).sqrt() as usize; + let mut v = vec![true; n + 1]; + let mut result = r - l + 1; + + let (l, r) = (l as usize, r as usize); + for i in 2..=n { + if v[i] { + if i.pow(2) >= l && i.pow(2) <= r { + result -= 1; + } + + for j in ((i * 2)..=n).step_by(i) { + v[j] = false; + } + } + } + + result + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3233() { + assert_eq!(19, Solution::non_special_count(5, 25)); + assert_eq!(3, Solution::non_special_count(5, 7)); + assert_eq!(11, Solution::non_special_count(4, 16)); + } +}