結果

問題 No.1611 Minimum Multiple with Double Divisors
ユーザー fukafukatani
提出日時 2021-07-21 22:36:18
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 2,208 bytes
コンパイル時間 11,454 ms
コンパイル使用メモリ 405,736 KB
実行使用メモリ 412,676 KB
最終ジャッジ日時 2024-07-17 19:36:30
合計ジャッジ時間 24,326 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 2
other AC * 1 WA * 10 TLE * 1 -- * 25
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> src/main.rs:21:17
   |
21 |     'outer: for i in 0..t {
   |                 ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: function `read_vec` is never used
  --> src/main.rs:85:4
   |
85 | fn read_vec<T: std::str::FromStr>() -> Vec<T> {
   |    ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

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

#[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 t = read::<usize>();
    let primes = get_primes(1000000);
    'outer: for i in 0..t {
        let x = read::<i64>();
        let org: i64 = factorize(x, &primes).values().map(|&x| x + 1).product();
        for cand in 2..20 {
            let cur: i64 = factorize(x * cand, &primes)
                .values()
                .map(|&x| x + 1)
                .product();
            if cur == 2 * org {
                debug!(cand);
                println!("{}", x * cand);
                continue 'outer;
            }
        }
    }
}

fn factorize(mut num: i64, primes: &Vec<i64>) -> std::collections::HashMap<i64, i64> {
    // max_primes >= (num)^(1/2)
    let mut dict = std::collections::HashMap::new();
    for &p in primes.iter() {
        while num % p == 0 {
            *dict.entry(p).or_insert(0) += 1;
            num /= p;
        }
        if num == 1 {
            break;
        }
        if p * p > num {
            *dict.entry(num).or_insert(0) += 1;
            break;
        }
    }
    if num != 1 {
        dict.insert(num, 1);
    }
    dict
}

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 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()
}
0