結果

問題 No.144 エラトステネスのざる
ユーザー cra77756176cra77756176
提出日時 2022-12-17 20:32:08
言語 Rust
(1.77.0)
結果
AC  
実行時間 262 ms / 2,000 ms
コード長 1,478 bytes
コンパイル時間 14,837 ms
コンパイル使用メモリ 396,152 KB
実行使用メモリ 10,456 KB
最終ジャッジ日時 2024-04-28 13:34:52
合計ジャッジ時間 13,911 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 0 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 0 ms
6,944 KB
testcase_04 AC 1 ms
6,948 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 262 ms
10,300 KB
testcase_14 AC 254 ms
10,380 KB
testcase_15 AC 257 ms
10,356 KB
testcase_16 AC 255 ms
10,312 KB
testcase_17 AC 256 ms
10,312 KB
testcase_18 AC 258 ms
10,304 KB
testcase_19 AC 256 ms
10,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn get_primes(max: usize) -> Vec<usize> {
    let mut is_prime = vec![true; max + 1];
    is_prime[0] = false;
    is_prime[1] = false;

    for n in 2..=max {
        if !is_prime[n] {
            continue;
        }
        if n * n > max {
            break;
        }
        for i in ((n * n)..=max).step_by(n) {
            is_prime[i] = false;
        }
    }

    (0..=max).filter(|&n| is_prime[n]).collect()
}

fn get_n_divisors(max: usize, primes: &[usize]) -> Vec<usize> {
    let mut n_divisors = vec![1; max + 1];

    for n in 2..=max {
        let mut n_tmp = n;
        for &p in primes {
            if p * p > n_tmp {
                break;
            }
            if n_tmp % p == 0 {
                let mut n_p = 1;
                while n_tmp % p == 0 {
                    n_tmp /= p;
                    n_p += 1;
                }
                n_divisors[n] *= n_p;
            }
        }
        if n_tmp > 1 {
            n_divisors[n] *= 2;
        }
    }

    n_divisors
}

fn main() {
    let mut xx = String::new();
    std::io::stdin().read_line(&mut xx).ok();
    let xx: Vec<&str> = xx.split_whitespace().collect();

    let n: usize = xx[0].parse().unwrap();
    let p: f64 = xx[1].parse().unwrap();
    let q = 1. - p;

    let primes = get_primes(n);
    let n_divisors = get_n_divisors(n, &primes);

    println!(
        "{}",
        (2..=n)
            .map(|i| q.powi(n_divisors[i] as i32 - 2))
            .sum::<f64>()
    );
}
0