-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
#include <cassert> | ||
|
||
#include <vector> | ||
|
||
std::vector<int> get_primes(int n) { | ||
assert(n > 0); | ||
std::vector<bool> 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 {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "lc-0000-largest-lcm" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
build: main.cpp | ||
g++ -g main.cpp -o main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
# 求能整除1-n的最小正整数 | ||
|
||
```text | ||
输入一个n (1 <= n <= 10000), | ||
求能整除1-n的最小正整数,即最小公倍数. | ||
由于数可能比较大,输出结果 mod 987654321. | ||
例子: | ||
输入: 3 | ||
输出: 6 (6是能整除1, 2, 3的最小正整数) | ||
``` |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#include <cassert> | ||
|
||
#include <iostream> | ||
#include <vector> | ||
|
||
int solution(int n) { | ||
// 解题思路: | ||
// 1. 先计算 1..n 之间的所有质数 | ||
// 2. 创建一个数组, 用于存放每个质因数的最大出现次数 | ||
// 3. 遍历 1..n 之间所有整数, 进行因式分解, 然后更新最大质因数数组 | ||
// 4. 将最大质因数数组中的值相乘, 即可得到结果. | ||
|
||
// 得到质数 | ||
const int max_num = n + 1; | ||
std::vector<bool> 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<int> 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. 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); | ||
} | ||
} |