結果

問題 No.1006 Share an Integer
ユーザー fukafukatanifukafukatani
提出日時 2020-03-06 22:31:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 2,248 bytes
コンパイル時間 4,680 ms
コンパイル使用メモリ 144,776 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-04 11:20:08
合計ジャッジ時間 5,561 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,388 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 7 ms
4,376 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 5 ms
4,380 KB
testcase_17 AC 5 ms
4,384 KB
testcase_18 AC 7 ms
4,380 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 6 ms
4,380 KB
testcase_21 AC 7 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: function `read_vec` is never used
  --> Main.rs:47:4
   |
47 | fn read_vec<T: std::str::FromStr>() -> Vec<T> {
   |    ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let x = read::<i64>();
    let mut count = vec![0; x as usize + 1];
    count[1] = 1;
    let mut xx = 1;
    while xx * xx <= x {
        xx += 1;
    }
    let primes = get_primes(xx + 1);
    let max_search = min(x / 2 + 1000, x);
    let min_search = max(x / 2 - 1000, 1);
    for i in min_search..max_search {
        count[i as usize] = factorize(i, &primes);
    }
    let f = |n: i64| n - count[n as usize];
    let min_fab = (1..x).map(|i| (f(i) - f(x - i)).abs()).min().unwrap();
    for i in min_search..max_search {
        if (f(i) - f(x - i)).abs() == min_fab {
            println!("{} {}", i, x - i);
        }
    }
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

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

    for i in 2..n + 1 {
        if is_prime[i as usize] {
            primes.push(i);
            let mut j = 2 * i;
            while j < n {
                is_prime[j as usize] = false;
                j += i;
            }
        }
    }
    primes
}

fn factorize(mut num: i64, primes: &Vec<i64>) -> i64 {
    // max_primes >= (num)^(1/2)
    let mut factors = vec![1; primes.len() + 1];
    let mut cur = 0;
    for &p in primes.iter() {
        while num % p == 0 {
            factors[cur] += 1;
            num /= p;
        }
        if num == 1 || p * p > num {
            break;
        }
        cur += 1;
    }
    if num != 1 {
        factors[primes.len()] += 1;
    }
    factors.iter().fold(1, |x, &s| x * (0 + s))
}
0