結果
| 問題 |
No.713 素数の和
|
| コンテスト | |
| ユーザー |
mnzn
|
| 提出日時 | 2020-12-03 22:46:03 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,436 bytes |
| コンパイル時間 | 12,860 ms |
| コンパイル使用メモリ | 385,812 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-14 08:06:14 |
| 合計ジャッジ時間 | 13,659 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 WA * 1 |
| other | AC * 3 WA * 3 |
コンパイルメッセージ
warning: method `prime_factors` is never used
--> src/main.rs:37:8
|
18 | impl Eratosthenes {
| ----------------- method in this implementation
...
37 | fn prime_factors(&self, n: u32) -> Vec<u32> {
| ^^^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
ソースコード
use std::io;
use std::str::FromStr;
fn main() {
let stdin = io::stdin();
let mut buf = String::new();
stdin.read_line(&mut buf).ok();
let mut it = buf.split_whitespace().map(|n| usize::from_str(n).unwrap());
let n = it.next().unwrap();
let era = Eratosthenes::new(n as u32);
let ans = (1..n).filter(|x| era.is_prime(*x as u32)).fold(0, |acc, x| acc + x);
println!("{}", ans);
}
struct Eratosthenes {
table: Vec<u32>,
}
impl Eratosthenes {
fn new(max: u32) -> Self {
let mut table: Vec<u32> = (0u32..=max).collect();
for i in (2..=max).step_by(2) {
table[i as usize] = 2;
}
(3u32..)
.step_by(2)
.take_while(|p| p * p <= max as u32)
.for_each(|p| {
if table[p as usize] < p {
return;
}
for x in (p..=max as u32).step_by(p as usize) {
table[x as usize] = p;
}
});
Self { table }
}
fn prime_factors(&self, n: u32) -> Vec<u32> {
let mut res = Vec::new();
let mut now = n as usize;
while self.table[now] != 1 {
res.push(self.table[now]);
now /= self.table[now] as usize;
}
res
}
fn is_prime(&self, n: u32) -> bool {
if n == 1 {
return false;
}
self.table[n as usize] == n
}
}
mnzn