diff --git a/math/src/primes.cpp b/math/src/primes.cpp new file mode 100644 index 000000000..53e07173a --- /dev/null +++ b/math/src/primes.cpp @@ -0,0 +1,21 @@ + +#include + +#include + +std::vector get_primes(int n) { + assert(n > 0); + std::vector primes(true, n + 1); + for (int i = 2; i < n + 1; ++i) { + if (!primes[i]) { + continue; + } + for (int j = i + 1; j < n + 1; ++j) { + if (j % i == 0) { + primes[j] = false; + } + } + } + + return {}; +} diff --git a/src/leetcode/0000.largest_lcm/Cargo.toml b/src/leetcode/0000.largest_lcm/Cargo.toml new file mode 100644 index 000000000..c4a530e53 --- /dev/null +++ b/src/leetcode/0000.largest_lcm/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "lc-0000-largest-lcm" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] diff --git a/src/leetcode/0000.largest_lcm/Makefile b/src/leetcode/0000.largest_lcm/Makefile new file mode 100644 index 000000000..b686101fc --- /dev/null +++ b/src/leetcode/0000.largest_lcm/Makefile @@ -0,0 +1,3 @@ + +build: main.cpp + g++ -g main.cpp -o main diff --git a/src/leetcode/0000.largest_lcm/index.md b/src/leetcode/0000.largest_lcm/index.md new file mode 100644 index 000000000..c3046ee07 --- /dev/null +++ b/src/leetcode/0000.largest_lcm/index.md @@ -0,0 +1,12 @@ + +# 求能整除1-n的最小正整数 + +```text +输入一个n (1 <= n <= 10000), +求能整除1-n的最小正整数,即最小公倍数. +由于数可能比较大,输出结果 mod 987654321. + +例子: +输入: 3 +输出: 6 (6是能整除1, 2, 3的最小正整数) +``` diff --git a/src/leetcode/0000.largest_lcm/main b/src/leetcode/0000.largest_lcm/main new file mode 100755 index 000000000..30cb53e76 Binary files /dev/null and b/src/leetcode/0000.largest_lcm/main differ diff --git a/src/leetcode/0000.largest_lcm/main.cpp b/src/leetcode/0000.largest_lcm/main.cpp new file mode 100644 index 000000000..2b901e9f5 --- /dev/null +++ b/src/leetcode/0000.largest_lcm/main.cpp @@ -0,0 +1,81 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +#include + +#include +#include + +int solution(int n) { + // 解题思路: + // 1. 先计算 1..n 之间的所有质数 + // 2. 创建一个数组, 用于存放每个质因数的最大出现次数 + // 3. 遍历 1..n 之间所有整数, 进行因式分解, 然后更新最大质因数数组 + // 4. 将最大质因数数组中的值相乘, 即可得到结果. + + // 得到质数 + const int max_num = n + 1; + std::vector primes(max_num, true); + for (int i = 2; i <= n; ++i) { + if (!primes[i]) { + continue; + } + for (int j = i + 1; j <= n; ++j) { + if (j % i == 0) { + primes[j] = false; + } + } + } + + std::vector max_factors(max_num, 0); + // 遍历所有整数 + for (int num = 2; num <= n; ++num) { + // 遍历所有质数 + int rem = num; + for (int j = 2; j <= num; ++j) { + if (!primes[j]) { + continue; + } + + // 确定当前质数作为质因数的次数. + int factor_count = 0; + while (rem % j == 0) { + rem /= j; + factor_count += 1; + } + + // 更新质因数的最大个数 + if (factor_count > max_factors[j]) { + max_factors[j] = factor_count; + } + + // 当前整数 num 已经被除尽了 + if (rem == 0) { + break; + } + } + } + + // 最后, 计算最大质因数的积. + int ans = 1; + const int kModular = 987654321; + for (int num = 2; num <= n; ++num) { + if (max_factors[num] > 0) { + for (int i = 0; i < max_factors[num]; ++i) { + ans = ans * num % kModular; + } + } + } + + return ans; +} + +void check_solution() { + assert(solution(3) == 6); +} + +int main() { + check_solution(); + return 0; +} diff --git a/src/leetcode/0000.largest_lcm/src/main.rs b/src/leetcode/0000.largest_lcm/src/main.rs new file mode 100644 index 000000000..9edcfeb7d --- /dev/null +++ b/src/leetcode/0000.largest_lcm/src/main.rs @@ -0,0 +1,27 @@ +// Copyright (c) 2024 Xu Shaohua . All rights reserved. +// Use of this source is governed by General Public License that can be found +// in the LICENSE file. + +pub fn solution1() { + todo!(); +} + +pub type SolutionFn = fn(); + +fn check_solution(_func: SolutionFn) { + todo!(); +} + +fn main() { + check_solution(solution1); +} + +#[cfg(test)] +mod tests { + use super::{check_solution, solution1}; + + #[test] + fn test_solution1() { + check_solution(solution1); + } +}