結果

問題 No.2645 Sum of Divisors?
ユーザー koba-e964koba-e964
提出日時 2024-04-13 10:34:34
言語 Rust
(1.77.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,934 bytes
コンパイル時間 1,047 ms
コンパイル使用メモリ 170,976 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-13 10:34:38
合計ジャッジ時間 2,935 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 42 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 73 ms
6,940 KB
testcase_07 AC 73 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 8 ms
6,944 KB
testcase_25 AC 6 ms
6,940 KB
testcase_26 AC 10 ms
6,944 KB
testcase_27 AC 13 ms
6,944 KB
testcase_28 AC 27 ms
6,940 KB
testcase_29 AC 73 ms
6,940 KB
testcase_30 AC 29 ms
6,940 KB
testcase_31 AC 43 ms
6,940 KB
testcase_32 AC 59 ms
6,944 KB
testcase_33 AC 31 ms
6,940 KB
testcase_34 AC 54 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: function `f_old` is never used
  --> main.rs:28:4
   |
28 | fn f_old(a: i64) -> f64 {
   |    ^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

use std::cmp::*;
use std::io::Read;

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

// TODO: optimize
fn f_old(a: i64) -> f64 {
    let mut cur = 0.0;
    for i in 1..a + 1 {
        cur += 1.0 / i as f64;
    }
    cur
}

// https://yukicoder.me/problems/no/2645 (3.5)
// 式変形すると、\sum_{i=1}^n \sum{j=1}^{floor(n/i)} 1/(ij) である。
// 以下をやる必要がある:
// - b_a := \sum_{j=1}^{a} 1/j を高速に求める
// - sqrt(n) との大小で b_a の加算の方法を変える
// b_a - ln(a + 0.5) は O(1/a^2) のはずなので、それを使って誤差を小さくする
// Tags: sqrt-decomposition, sum-of-divisors, harmonic-series
fn main() {
    const W: usize = 100_000;
    let mut dp = vec![0.0; W];
    for i in 1..W {
        dp[i] = dp[i - 1] + 1.0 / i as f64;
    }
    let f = |x: i64| {
        if x < W as i64 {
            dp[x as usize]
        } else {
            (x as f64 + 0.5).ln() + 0.57721_56649_01532_86060
        }
    };

    let n: i64 = get();
    let mut s = 1;
    while s * s <= n {
        s += 1;
    }
    s -= 1;
    let mut sum = 0.0;
    for i in 1..s + 1 {
        sum += 1.0 / i as f64 * f(n / i);
    }
    for i in 1..s + 1 {
        let lo = max(s, n / (i + 1));
        let hi = n / i;
        if lo < hi {
            sum += (f(hi) - f(lo)) as f64 * f(i);
        }
    }
    println!("{}", sum);
}
0