結果

問題 No.1330 Multiply or Divide
ユーザー phsplsphspls
提出日時 2023-01-16 04:14:28
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,745 bytes
コンパイル時間 1,297 ms
コンパイル使用メモリ 163,672 KB
実行使用メモリ 12,684 KB
最終ジャッジ日時 2023-08-28 17:52:39
合計ジャッジ時間 6,631 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
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:10:9
   |
10 |     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;


const INF: usize = 1usize << 60;

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 val2cnt = a.into_iter().map(|v| {
            let mut cnt = 1usize;
            let mut val = v;
            while val % p == 0 {
                val /= p;
                cnt += 1;
            }
            (val, cnt)
        })
        .collect::<Vec<_>>();
    let val2cnt = val2cnt.into_iter().filter(|&(v, _)| v > 1).collect::<Vec<_>>();
    if val2cnt.is_empty() {
        println!("-1");
        return;
    }
    let limit = m / maxval;
    let mut dp = HashMap::new();
    dp.insert(1usize, 0usize);
    for &(val, cnt) in val2cnt.iter() {
        let mut next_dp = HashMap::new();
        for (&cval, &ccnt) in dp.iter() {
            for use_cnt in 0.. {
                let nkey = cval * val.pow(use_cnt as u32);
                let nkey = if nkey > limit { INF } else { nkey };
                let ncnt = cnt + ccnt * use_cnt;
                let count = *next_dp.get(&nkey).unwrap_or(&INF);
                if count > ncnt {
                    next_dp.insert(nkey, ncnt);
                }
                if nkey == INF { break; }
            }
        }
        dp = next_dp;
    }
    println!("{}", dp.get(&INF).unwrap() + 1);
}
0