結果

問題 No.1330 Multiply or Divide
ユーザー phsplsphspls
提出日時 2023-01-08 18:53:09
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 2,029 bytes
コンパイル時間 1,452 ms
コンパイル使用メモリ 164,972 KB
実行使用メモリ 21,032 KB
最終ジャッジ日時 2023-08-22 04:26:30
合計ジャッジ時間 7,690 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,756 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `n`
  --> Main.rs:18:9
   |
18 |     let n = nmp[0];
   |         ^ help: if this is intentional, prefix it with an underscore: `_n`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted

ソースコード

diff #

use std::collections::{HashMap, VecDeque, HashSet};


fn costmap(val: usize, p: usize) -> (usize, usize) {
    let mut cnt = 1usize;
    let mut val = val;
    while val % p == 0 {
        cnt += 1;
        val /= p;
    }
    (val, cnt)
}

fn main() {
    let mut nmp = String::new();
    std::io::stdin().read_line(&mut nmp).ok();
    let nmp: Vec<usize> = nmp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nmp[0];
    let m = nmp[1];
    let p = nmp[2];
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let maxval = *a.iter().max().unwrap();
    if maxval >= m {
        println!("1");
        return;
    }
    let mut paths = a.iter().map(|&v| costmap(v, p)).filter(|&v| v.0 > 1).collect::<Vec<_>>();
    if paths.is_empty() {
        println!("-1");
        return;
    }
    paths.sort();
    paths.reverse();
    let mut npaths = HashMap::new();
    for &(val, cnt) in paths.iter() {
        npaths.insert(val, cnt);
    }
    let mut result = 1usize << 60;
    let mut dp = HashMap::new();
    dp.insert(maxval, 1usize);
    let mut deque = VecDeque::new();
    deque.push_back((maxval, 1usize));
    while let Some((val, cnt)) = deque.pop_front() {
        let mut updated_keys = HashSet::new();
        for (&aval, &acnt) in npaths.iter() {
            if let Some(&ccnt) = dp.get(&(val + aval)) {
                if ccnt > cnt + acnt && val + aval < m {
                    dp.insert(val*aval, cnt+acnt);
                    updated_keys.insert(val*aval);
                }
            } else if val*aval < m {
                dp.insert(val*aval, cnt+acnt);
                updated_keys.insert(val*aval);
            }
            if val * aval >= m {
                result = result.min(cnt+acnt);
            }
        }
        for v in updated_keys.iter() {
            deque.push_back((*v, *dp.get(v).unwrap()));
        }
    }
    println!("{}", result);
}
0