結果

問題 No.1006 Share an Integer
ユーザー tzyvrntzyvrn
提出日時 2020-03-07 13:23:20
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 8 ms / 2,000 ms
コード長 3,421 bytes
コンパイル時間 12,997 ms
コンパイル使用メモリ 392,820 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-14 12:26:46
合計ジャッジ時間 14,232 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,820 KB
testcase_03 AC 1 ms
6,820 KB
testcase_04 AC 1 ms
6,820 KB
testcase_05 AC 1 ms
6,820 KB
testcase_06 AC 1 ms
6,816 KB
testcase_07 AC 1 ms
6,820 KB
testcase_08 AC 1 ms
6,816 KB
testcase_09 AC 1 ms
6,816 KB
testcase_10 AC 1 ms
6,816 KB
testcase_11 AC 6 ms
6,820 KB
testcase_12 AC 8 ms
6,820 KB
testcase_13 AC 8 ms
6,820 KB
testcase_14 AC 8 ms
6,816 KB
testcase_15 AC 8 ms
6,816 KB
testcase_16 AC 5 ms
6,820 KB
testcase_17 AC 6 ms
6,816 KB
testcase_18 AC 7 ms
6,820 KB
testcase_19 AC 7 ms
6,816 KB
testcase_20 AC 8 ms
6,820 KB
testcase_21 AC 8 ms
6,816 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: variable does not need to be mutable
  --> src/main.rs:21:13
   |
21 |           let mut s = {
   |               ----^
   |               |
   |               help: remove this `mut`
...
66 | /     input! {
67 | |         x: usize,
68 | |     }
   | |_____- in this macro invocation
   |
   = note: `#[warn(unused_mut)]` on by default
   = note: this warning originates in the macro `input` (in Nightly builds, run with -Z macro-backtrace for more info)

ソースコード

diff #

use std::collections::HashMap;
#[allow(unused_imports)]
use std::io::Write;

// {{{1
#[allow(unused)]
macro_rules! debug {
    ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap());
}
#[allow(unused)]
macro_rules! debugln {
    ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap());
}

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}
// }}}

fn main() {
    input! {
        x: usize,
    }

    let solve = Solve::new();

    let sqrt = (x as f64).sqrt() as usize;

    let mut v = vec![];
    let mut min = std::isize::MAX;
    for i in 1..x {
        let a = i;
        let b = x - i;

        if (a as isize - b as isize).abs() > 2 * sqrt as isize + 1 {
            continue;
        }

        let tmp = (solve.f(a) as isize - solve.f(b) as isize).abs();
        if tmp == min {
            v.push((a, b));
        } else if tmp < min {
            min = tmp;
            v = vec![(a, b)];
        }
    }

    for (a, b) in v {
        println!("{} {}", a, b);
    }
}

struct Solve {
    primes: Vec<usize>,
}

impl Solve {
    fn new() -> Solve {
        let n = 2_000_000;
        let primes = prime_sieve((n as f64).sqrt() as usize);

        Solve { primes }
    }

    fn prime_factor(&self, n: usize) -> HashMap<usize, usize> {
        let mut factors = HashMap::new();

        let primes = &self.primes;
        let mut rest = n;
        for &p in primes {
            while rest % p == 0 {
                let count = factors.entry(p).or_insert(0);
                *count += 1;
                rest /= p;
            }
        }
        if rest != 1 {
            factors.insert(rest, 1);
        }

        factors
    }

    fn f(&self, n: usize) -> usize {
        let factors = self.prime_factor(n);
        let mut d = 1;
        for (_, e) in factors {
            d *= e + 1;
        }

        n - d
    }
}

fn prime_sieve(n: usize) -> Vec<usize> {
    let mut table: Vec<usize> = vec![0; (n + 1) as usize];
    let mut primes: Vec<usize> = Vec::new();

    for i in 2..(n + 1) {
        if table[i as usize] == 0 {
            primes.push(i);
            for j in 2..n {
                if i * j > n {
                    break;
                }
                table[(i * j) as usize] = 1
            }
        }
    }

    primes
}
0